From 51142afadd8e031bc8c29b97d87be145ee1c94ba Mon Sep 17 00:00:00 2001 From: Andhitia Rama Date: Mon, 9 Oct 2023 18:06:03 +0700 Subject: [PATCH] [UPD] ssi_risk_analysis * Implementing view template * Restrict risk analysis choice based on partner * Remove risk analysis if partner change --- ssi_risk_analysis/__manifest__.py | 1 + .../models/mixin_risk_analysis.py | 49 +++++++++++++++++++ .../mixin_risk_analysis_templates.xml | 20 ++++++++ 3 files changed, 70 insertions(+) create mode 100644 ssi_risk_analysis/templates/mixin_risk_analysis_templates.xml diff --git a/ssi_risk_analysis/__manifest__.py b/ssi_risk_analysis/__manifest__.py index 1ed2904..6d72d81 100644 --- a/ssi_risk_analysis/__manifest__.py +++ b/ssi_risk_analysis/__manifest__.py @@ -32,6 +32,7 @@ "data/approval_template_data.xml", "data/policy_template_data.xml", "menu.xml", + "templates/mixin_risk_analysis_templates.xml", "views/risk_analysis_type_views.xml", "views/risk_analysis_result_views.xml", "views/risk_analysis_conclusion_views.xml", diff --git a/ssi_risk_analysis/models/mixin_risk_analysis.py b/ssi_risk_analysis/models/mixin_risk_analysis.py index 9ef20a9..062da3e 100644 --- a/ssi_risk_analysis/models/mixin_risk_analysis.py +++ b/ssi_risk_analysis/models/mixin_risk_analysis.py @@ -5,11 +5,25 @@ from odoo import api, fields, models +from odoo.addons.ssi_decorator import ssi_decorator + class MixinRiskAnalysis(models.AbstractModel): _name = "mixin.risk_analysis" + _inherit = [ + "mixin.decorator", + ] _description = "Mixin for Object With Risk Analysis" + _risk_analysis_create_page = False + _risk_analysis_page_xpath = "//page[last()]" + _risk_analysis_partner_field_name = False + allowed_risk_analysis_ids = fields.Many2many( + string="Allowed Risk Analysis", + comodel_name="risk_analysis", + compute="_compute_allowed_risk_analysis_ids", + store=False, + ) risk_analysis_id = fields.Many2one( string="# Risk Analysis", comodel_name="risk_analysis", @@ -23,6 +37,7 @@ class MixinRiskAnalysis(models.AbstractModel): comodel_name="risk_analysis_result", compute="_compute_risk_analysis_result_id", store=True, + compute_sudo=True, ) @api.depends( @@ -36,3 +51,37 @@ def _compute_risk_analysis_result_id(self): if record.risk_analysis_id and record.risk_analysis_id.state == "done": result = record.risk_analysis_id.result_id record.risk_analysis_result_id = result + + def _get_allowed_risk_analysis_ids_trigger(self): + result = [] + if self._risk_analysis_partner_field_name: + result += [self._risk_analysis_partner_field_name] + return result + + @api.depends(lambda self: self._get_allowed_risk_analysis_ids_trigger()) + def _compute_allowed_risk_analysis_ids(self): + for record in self: + result = [] + record.risk_analysis_id = False + if self._risk_analysis_partner_field_name and hasattr( + record, self._risk_analysis_partner_field_name + ): + partner = getattr(record, self._risk_analysis_partner_field_name) + if partner: + criteria = [ + ("partner_id", "=", partner.commercial_partner_id.id), + ("state", "!=", "cancel"), + ] + result = self.env["risk_analysis"].search(criteria).ids + record.allowed_risk_analysis_ids = result + + @ssi_decorator.insert_on_form_view() + def _risk_analysis_insert_form_element(self, view_arch): + if self._risk_analysis_create_page: + view_arch = self._add_view_element( + view_arch=view_arch, + qweb_template_xml_id="ssi_risk_analysis.risk_analysis", + xpath=self._risk_analysis_page_xpath, + position="after", + ) + return view_arch diff --git a/ssi_risk_analysis/templates/mixin_risk_analysis_templates.xml b/ssi_risk_analysis/templates/mixin_risk_analysis_templates.xml new file mode 100644 index 0000000..542d9d0 --- /dev/null +++ b/ssi_risk_analysis/templates/mixin_risk_analysis_templates.xml @@ -0,0 +1,20 @@ + + + +