From 24fa6cb1a6750648ac4e6a511163040fcc551e00 Mon Sep 17 00:00:00 2001
From: Tiago Amaral
Date: Wed, 25 Sep 2024 08:58:32 -0300
Subject: [PATCH] [MIG] crm_required_field_by_stage: Migration
---
crm_required_field_by_stage/README.rst | 10 ++--
crm_required_field_by_stage/__manifest__.py | 2 +-
.../models/crm_lead.py | 48 +++++++++++++++--
.../static/description/index.html | 7 ++-
crm_required_field_by_stage/tests/__init__.py | 1 +
.../tests/test_crm_required_field_by_stage.py | 51 +++++++++++++++++++
.../odoo/addons/crm_required_field_by_stage | 1 +
setup/crm_required_field_by_stage/setup.py | 6 +++
8 files changed, 112 insertions(+), 14 deletions(-)
create mode 100644 crm_required_field_by_stage/tests/__init__.py
create mode 100644 crm_required_field_by_stage/tests/test_crm_required_field_by_stage.py
create mode 120000 setup/crm_required_field_by_stage/odoo/addons/crm_required_field_by_stage
create mode 100644 setup/crm_required_field_by_stage/setup.py
diff --git a/crm_required_field_by_stage/README.rst b/crm_required_field_by_stage/README.rst
index d6e3e11ecae..89448966b80 100644
--- a/crm_required_field_by_stage/README.rst
+++ b/crm_required_field_by_stage/README.rst
@@ -17,13 +17,13 @@ Required Fields by Stage in CRM
:target: http://www.gnu.org/licenses/lgpl-3.0-standalone.html
:alt: License: LGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fcrm-lightgray.png?logo=github
- :target: https://github.com/OCA/crm/tree/17.0/crm_required_field_by_stage
+ :target: https://github.com/OCA/crm/tree/16.0/crm_required_field_by_stage
:alt: OCA/crm
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
- :target: https://translation.odoo-community.org/projects/crm-17-0/crm-17-0-crm_required_field_by_stage
+ :target: https://translation.odoo-community.org/projects/crm-16-0/crm-16-0-crm_required_field_by_stage
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
- :target: https://runboat.odoo-community.org/builds?repo=OCA/crm&target_branch=17.0
+ :target: https://runboat.odoo-community.org/builds?repo=OCA/crm&target_branch=16.0
:alt: Try me on Runboat
|badge1| |badge2| |badge3| |badge4| |badge5|
@@ -56,7 +56,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues `_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
-`feedback `_.
+`feedback `_.
Do not contact contributors directly about support or help with technical issues.
@@ -96,6 +96,6 @@ Current `maintainer `__:
|maintainer-alan196|
-This module is part of the `OCA/crm `_ project on GitHub.
+This module is part of the `OCA/crm `_ project on GitHub.
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
diff --git a/crm_required_field_by_stage/__manifest__.py b/crm_required_field_by_stage/__manifest__.py
index 7a060cd23b9..9c282c18ef3 100644
--- a/crm_required_field_by_stage/__manifest__.py
+++ b/crm_required_field_by_stage/__manifest__.py
@@ -4,7 +4,7 @@
{
"name": "Required Fields by Stage in CRM",
"summary": "Define required fields by stage in CRM leads",
- "version": "17.0.1.0.0",
+ "version": "16.0.1.0.0",
"category": "Customer Relationship Management",
"website": "https://github.com/OCA/crm",
"author": "Jarsa, Odoo Community Association (OCA)",
diff --git a/crm_required_field_by_stage/models/crm_lead.py b/crm_required_field_by_stage/models/crm_lead.py
index 766d876b1e1..d9c8759c68b 100644
--- a/crm_required_field_by_stage/models/crm_lead.py
+++ b/crm_required_field_by_stage/models/crm_lead.py
@@ -1,7 +1,11 @@
# Copyright 2024 Jarsa
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl.html).
-from odoo import api, models
+import ast
+import json as simplejson
+
+from odoo import _, api, models
+from odoo.exceptions import UserError
class CrmLead(models.Model):
@@ -17,9 +21,20 @@ def _get_view(self, view_id=None, view_type="form", **options):
lambda stage, field=field: field in stage.required_field_ids
)
for node in arch.xpath("//field[@name='%s']" % field.name):
- node.attrib["required"] = "stage_id in [%s]" % ",".join(
- [str(id) for id in stages_with_field.ids]
- )
+ attrs = ast.literal_eval(node.attrib.get("attrs", "{}"))
+ if attrs:
+ if attrs.get("required"):
+ attrs["required"] = [
+ "|",
+ ("stage_id", "in", stages_with_field.ids),
+ ] + attrs["required"]
+ else:
+ attrs["required"] = [
+ ("stage_id", "in", stages_with_field.ids)
+ ]
+ else:
+ attrs["required"] = [("stage_id", "in", stages_with_field.ids)]
+ node.set("attrs", simplejson.dumps(attrs))
return arch, view
@api.model
@@ -32,3 +47,28 @@ def _get_view_cache_key(self, view_id=None, view_type="form", **options):
.search([("required_field_ids", "!=", False)])
.mapped("required_field_ids.name")
)
+
+ @api.constrains("stage_id")
+ def _check_stage_id_(self):
+ for rec in self:
+ stage = self.env["crm.stage"].sudo().search([("id", "=", rec.stage_id.id)])
+ for s in stage:
+ fields = (
+ self.env["ir.model.fields"]
+ .sudo()
+ .search([("id", "in", s.required_field_ids.ids)])
+ )
+ for field in fields:
+ if hasattr(self, "%s" % field.name):
+ if not getattr(self, "%s" % field.name):
+ raise UserError(
+ _(
+ "Field '%(field)s' is mandatory in stage '%(stage)s'."
+ )
+ % (
+ {
+ "field": field.display_name.split(" (")[0],
+ "stage": s.display_name,
+ }
+ )
+ )
diff --git a/crm_required_field_by_stage/static/description/index.html b/crm_required_field_by_stage/static/description/index.html
index 8edabc752de..da4f4d7e087 100644
--- a/crm_required_field_by_stage/static/description/index.html
+++ b/crm_required_field_by_stage/static/description/index.html
@@ -1,4 +1,3 @@
-
@@ -370,7 +369,7 @@ Required Fields by Stage in CRM
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:fd5d37d78ea01679f05c1a52d1e1720c31e2c941682c529f03be2c21bb0c462f
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
-
+
This module was written to extend the functionality of CRM leads to
require certain fields to be filled out based on the stage of the lead.
Table of contents
@@ -405,7 +404,7 @@
Bugs are tracked on GitHub Issues.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
-feedback.
+feedback.
Do not contact contributors directly about support or help with technical issues.
diff --git a/crm_required_field_by_stage/tests/__init__.py b/crm_required_field_by_stage/tests/__init__.py
new file mode 100644
index 00000000000..dd75a3d0b5f
--- /dev/null
+++ b/crm_required_field_by_stage/tests/__init__.py
@@ -0,0 +1 @@
+from . import test_crm_required_field_by_stage
diff --git a/crm_required_field_by_stage/tests/test_crm_required_field_by_stage.py b/crm_required_field_by_stage/tests/test_crm_required_field_by_stage.py
new file mode 100644
index 00000000000..690ed4a5260
--- /dev/null
+++ b/crm_required_field_by_stage/tests/test_crm_required_field_by_stage.py
@@ -0,0 +1,51 @@
+# Copyright 2024 KMEE
+# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
+
+from odoo.exceptions import UserError
+from odoo.tests.common import TransactionCase
+
+
+class TestCrmRequiredFieldByStage(TransactionCase):
+ def setUp(self):
+ super().setUp()
+
+ self.crm_stage_model = self.env["crm.stage"]
+ self.crm_lead_model = self.env["crm.lead"]
+ self.crm_stage_1 = self.crm_stage_model.create(
+ {
+ "name": "CRM Stage 1",
+ "required_field_ids": [
+ (4, self.env.ref("crm.field_crm_lead__phone").id)
+ ],
+ }
+ )
+ self.crm_lead_1 = self.crm_lead_model.create(
+ {
+ "name": "CRM Lead 1",
+ }
+ )
+
+ def test_locking(self):
+ with self.assertRaises(UserError):
+ self.crm_lead_1.write(
+ {
+ "stage_id": self.crm_stage_1.id,
+ }
+ )
+ self.assertTrue(self.crm_lead_1.stage_id)
+ self.crm_lead_1.write(
+ {
+ "phone": "(00) 0000-0000",
+ }
+ )
+ self.crm_lead_1.write(
+ {
+ "stage_id": self.crm_stage_1.id,
+ }
+ )
+ self.assertEqual(self.crm_lead_1.stage_id.id, self.crm_stage_1.id)
+ self.crm_lead_1.write(
+ {
+ "phone": False,
+ }
+ )
diff --git a/setup/crm_required_field_by_stage/odoo/addons/crm_required_field_by_stage b/setup/crm_required_field_by_stage/odoo/addons/crm_required_field_by_stage
new file mode 120000
index 00000000000..0779843e181
--- /dev/null
+++ b/setup/crm_required_field_by_stage/odoo/addons/crm_required_field_by_stage
@@ -0,0 +1 @@
+../../../../crm_required_field_by_stage
\ No newline at end of file
diff --git a/setup/crm_required_field_by_stage/setup.py b/setup/crm_required_field_by_stage/setup.py
new file mode 100644
index 00000000000..28c57bb6403
--- /dev/null
+++ b/setup/crm_required_field_by_stage/setup.py
@@ -0,0 +1,6 @@
+import setuptools
+
+setuptools.setup(
+ setup_requires=['setuptools-odoo'],
+ odoo_addon=True,
+)