Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[UPD] ssi_risk_analysis #7

Merged
merged 1 commit into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions ssi_risk_analysis/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"ssi_transaction_cancel_mixin",
"ssi_transaction_terminate_mixin",
"ssi_custom_information_mixin",
"ssi_localdict_mixin",
],
"data": [
"security/ir_module_category_data.xml",
Expand Down
38 changes: 33 additions & 5 deletions ssi_risk_analysis/models/risk_analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl).

from odoo import api, fields, models
from odoo.tools.safe_eval import safe_eval


class RiskAnalysis(models.Model):
Expand All @@ -14,6 +15,7 @@ class RiskAnalysis(models.Model):
"mixin.transaction_done",
"mixin.transaction_cancel",
"mixin.transaction_terminate",
"mixin.localdict",
]
_description = "Risk Analysis"

Expand Down Expand Up @@ -208,13 +210,24 @@ def _get_policy_field(self):
@api.depends(
"manual_result_id",
"result_computation_method",
"item_ids",
"item_ids.result_id",
"item_ids.worksheet_id.state",
"type_id",
)
def _compute_result_id(self):
self.result_id = False
if self.result_computation_method == "manual":
self.result_id = self.manual_result_id
elif self.result_computation_method == "auto":
self.result_id = self.manual_result_id
for record in self:
automatic_result = final_result = False

automatic_result = record._get_automatic_result()

if self.result_computation_method == "manual":
final_result = self.manual_result_id
elif self.result_computation_method == "auto":
final_result = automatic_result

record.automatic_result_id = automatic_result
record.result_id = final_result

@api.onchange(
"type_id",
Expand All @@ -232,3 +245,18 @@ def _reload_risk_item(self):
if self.type_id and self.type_id.item_ids:
for item in self.type_id.item_ids:
item._create_risk_analysis_item(self)

def _get_automatic_result(self):
self.ensure_one()
localdict = self._get_default_localdict()
try:
safe_eval(
self.type_id.result_python_code,
localdict,
mode="exec",
nocopy=True,
)
result = localdict["result"]
except Exception:
result = False
return result
5 changes: 5 additions & 0 deletions ssi_risk_analysis/models/risk_analysis_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ class RiskAnalysisType(models.Model):
compute="_compute_allowed_result_ids",
store=False,
)
result_python_code = fields.Text(
string="Python Code for Result Computation",
default="result = False",
required=True,
)

def _compute_allowed_result_ids(self):
for record in self:
Expand Down
34 changes: 29 additions & 5 deletions ssi_risk_analysis/models/risk_analysis_worksheet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl-3.0-standalone.html).

from odoo import api, fields, models
from odoo.tools.safe_eval import safe_eval


class RiskAnalysisWorksheet(models.Model):
Expand All @@ -16,6 +17,7 @@ class RiskAnalysisWorksheet(models.Model):
"mixin.transaction_cancel",
"mixin.transaction_terminate",
"mixin.custom_info",
"mixin.localdict",
]

_approval_from_state = "open"
Expand Down Expand Up @@ -239,14 +241,21 @@ def _compute_allowed_type_ids(self):
@api.depends(
"manual_result_id",
"result_computation_method",
"type_id",
)
def _compute_result_id(self):
for record in self:
record.result_id = False
if record.result_computation_method == "manual":
record.result_id = record.manual_result_id
elif record.result_computation_method == "auto":
record.result_id = record.manual_result_id
automatic_result = final_result = False

automatic_result = record._get_automatic_result()

if self.result_computation_method == "manual":
final_result = self.manual_result_id
elif self.result_computation_method == "auto":
final_result = automatic_result

record.automatic_result_id = automatic_result
record.result_id = final_result

@api.onchange("risk_analysis_id")
def onchange_item_id(self):
Expand Down Expand Up @@ -282,3 +291,18 @@ def _get_policy_field(self):
]
res += policy_field
return res

def _get_automatic_result(self):
self.ensure_one()
localdict = self._get_default_localdict()
try:
safe_eval(
self.type_id.result_python_code,
localdict,
mode="exec",
nocopy=True,
)
result = localdict["result"]
except Exception:
result = False
return result
5 changes: 5 additions & 0 deletions ssi_risk_analysis/models/risk_analysis_worksheet_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ class RiskAnalysisWorkSheetType(models.Model):
string="Model Technical Name",
related="model_id.model",
)
result_python_code = fields.Text(
string="Python Code for Result Computation",
default="result = False",
required=True,
)

@api.onchange(
"ttype",
Expand Down
15 changes: 14 additions & 1 deletion ssi_risk_analysis/views/risk_analysis_type_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
</field>
</page>
<page name="result" string="Results">
<group name="result" colspan="4" col="2">
<group name="result_1" colspan="4" col="2">
<field name="allowed_result_ids" widget="many2many_tags" />
</group>
<field name="result_ids">
Expand All @@ -53,6 +53,19 @@
<field name="quantitative_value" />
</tree>
</field>
<group
name="result_2"
colspan="2"
col="2"
string="Python Code for Result Computation"
>
<field
name="result_python_code"
widget="ace"
colspan="2"
nolabel="1"
/>
</group>
</page>
</xpath>
</data>
Expand Down
15 changes: 15 additions & 0 deletions ssi_risk_analysis/views/risk_analysis_worksheet_type_views.xml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,21 @@
<field name="model" />
</group>
</page>
<page name="result" string="Results">
<group
name="result_1"
colspan="2"
col="2"
string="Python Code for Result Computation"
>
<field
name="result_python_code"
widget="ace"
colspan="2"
nolabel="1"
/>
</group>
</page>
</xpath>
</data>
</field>
Expand Down
Loading