diff --git a/ssi_risk_analysis/__manifest__.py b/ssi_risk_analysis/__manifest__.py index a478a5d..e5ce004 100644 --- a/ssi_risk_analysis/__manifest__.py +++ b/ssi_risk_analysis/__manifest__.py @@ -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", diff --git a/ssi_risk_analysis/models/risk_analysis.py b/ssi_risk_analysis/models/risk_analysis.py index b326b9f..e014a81 100644 --- a/ssi_risk_analysis/models/risk_analysis.py +++ b/ssi_risk_analysis/models/risk_analysis.py @@ -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): @@ -14,6 +15,7 @@ class RiskAnalysis(models.Model): "mixin.transaction_done", "mixin.transaction_cancel", "mixin.transaction_terminate", + "mixin.localdict", ] _description = "Risk Analysis" @@ -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", @@ -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 diff --git a/ssi_risk_analysis/models/risk_analysis_type.py b/ssi_risk_analysis/models/risk_analysis_type.py index 0763e71..f028d71 100644 --- a/ssi_risk_analysis/models/risk_analysis_type.py +++ b/ssi_risk_analysis/models/risk_analysis_type.py @@ -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: diff --git a/ssi_risk_analysis/models/risk_analysis_worksheet.py b/ssi_risk_analysis/models/risk_analysis_worksheet.py index 24faf2a..e7556f5 100644 --- a/ssi_risk_analysis/models/risk_analysis_worksheet.py +++ b/ssi_risk_analysis/models/risk_analysis_worksheet.py @@ -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): @@ -16,6 +17,7 @@ class RiskAnalysisWorksheet(models.Model): "mixin.transaction_cancel", "mixin.transaction_terminate", "mixin.custom_info", + "mixin.localdict", ] _approval_from_state = "open" @@ -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): @@ -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 diff --git a/ssi_risk_analysis/models/risk_analysis_worksheet_type.py b/ssi_risk_analysis/models/risk_analysis_worksheet_type.py index 07400f0..ebb5852 100644 --- a/ssi_risk_analysis/models/risk_analysis_worksheet_type.py +++ b/ssi_risk_analysis/models/risk_analysis_worksheet_type.py @@ -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", diff --git a/ssi_risk_analysis/views/risk_analysis_type_views.xml b/ssi_risk_analysis/views/risk_analysis_type_views.xml index 69d93fa..9415fbd 100644 --- a/ssi_risk_analysis/views/risk_analysis_type_views.xml +++ b/ssi_risk_analysis/views/risk_analysis_type_views.xml @@ -43,7 +43,7 @@ - + @@ -53,6 +53,19 @@ + + + diff --git a/ssi_risk_analysis/views/risk_analysis_worksheet_type_views.xml b/ssi_risk_analysis/views/risk_analysis_worksheet_type_views.xml index 14805e5..9c20115 100644 --- a/ssi_risk_analysis/views/risk_analysis_worksheet_type_views.xml +++ b/ssi_risk_analysis/views/risk_analysis_worksheet_type_views.xml @@ -58,6 +58,21 @@ + + + + +