Skip to content

Commit

Permalink
[MIG] crm_required_field_by_stage: Migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiago Amaral committed Sep 30, 2024
1 parent 765d9e0 commit 2a8adec
Show file tree
Hide file tree
Showing 8 changed files with 117 additions and 14 deletions.
10 changes: 5 additions & 5 deletions crm_required_field_by_stage/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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|
Expand Down Expand Up @@ -56,7 +56,7 @@ Bug Tracker
Bugs are tracked on `GitHub Issues <https://github.com/OCA/crm/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 <https://github.com/OCA/crm/issues/new?body=module:%20crm_required_field_by_stage%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/crm/issues/new?body=module:%20crm_required_field_by_stage%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Expand Down Expand Up @@ -96,6 +96,6 @@ Current `maintainer <https://odoo-community.org/page/maintainer-role>`__:

|maintainer-alan196|

This module is part of the `OCA/crm <https://github.com/OCA/crm/tree/17.0/crm_required_field_by_stage>`_ project on GitHub.
This module is part of the `OCA/crm <https://github.com/OCA/crm/tree/16.0/crm_required_field_by_stage>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
2 changes: 1 addition & 1 deletion crm_required_field_by_stage/__manifest__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)",
Expand Down
48 changes: 44 additions & 4 deletions crm_required_field_by_stage/models/crm_lead.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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"] = [

Check warning on line 27 in crm_required_field_by_stage/models/crm_lead.py

View check run for this annotation

Codecov / codecov/patch

crm_required_field_by_stage/models/crm_lead.py#L27

Added line #L27 was not covered by tests
"|",
("stage_id", "in", stages_with_field.ids),
] + attrs["required"]
else:
attrs["required"] = [

Check warning on line 32 in crm_required_field_by_stage/models/crm_lead.py

View check run for this annotation

Codecov / codecov/patch

crm_required_field_by_stage/models/crm_lead.py#L32

Added line #L32 was not covered by tests
("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
Expand All @@ -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,
}
)
)
7 changes: 3 additions & 4 deletions crm_required_field_by_stage/static/description/index.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
Expand Down Expand Up @@ -370,7 +369,7 @@ <h1 class="title">Required Fields by Stage in CRM</h1>
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:fd5d37d78ea01679f05c1a52d1e1720c31e2c941682c529f03be2c21bb0c462f
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/crm/tree/17.0/crm_required_field_by_stage"><img alt="OCA/crm" src="https://img.shields.io/badge/github-OCA%2Fcrm-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/crm-17-0/crm-17-0-crm_required_field_by_stage"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/crm&amp;target_branch=17.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p><a class="reference external image-reference" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external image-reference" href="http://www.gnu.org/licenses/lgpl-3.0-standalone.html"><img alt="License: LGPL-3" src="https://img.shields.io/badge/licence-LGPL--3-blue.png" /></a> <a class="reference external image-reference" href="https://github.com/OCA/crm/tree/16.0/crm_required_field_by_stage"><img alt="OCA/crm" src="https://img.shields.io/badge/github-OCA%2Fcrm-lightgray.png?logo=github" /></a> <a class="reference external image-reference" href="https://translation.odoo-community.org/projects/crm-16-0/crm-16-0-crm_required_field_by_stage"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external image-reference" href="https://runboat.odoo-community.org/builds?repo=OCA/crm&amp;target_branch=16.0"><img alt="Try me on Runboat" src="https://img.shields.io/badge/runboat-Try%20me-875A7B.png" /></a></p>
<p>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.</p>
<p><strong>Table of contents</strong></p>
Expand Down Expand Up @@ -405,7 +404,7 @@ <h1><a class="toc-backref" href="#toc-entry-2">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/crm/issues">GitHub Issues</a>.
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
<a class="reference external" href="https://github.com/OCA/crm/issues/new?body=module:%20crm_required_field_by_stage%0Aversion:%2017.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/crm/issues/new?body=module:%20crm_required_field_by_stage%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
Expand Down Expand Up @@ -436,7 +435,7 @@ <h2><a class="toc-backref" href="#toc-entry-6">Maintainers</a></h2>
promote its widespread use.</p>
<p>Current <a class="reference external" href="https://odoo-community.org/page/maintainer-role">maintainer</a>:</p>
<p><a class="reference external image-reference" href="https://github.com/alan196"><img alt="alan196" src="https://github.com/alan196.png?size=40px" /></a></p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/crm/tree/17.0/crm_required_field_by_stage">OCA/crm</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/crm/tree/16.0/crm_required_field_by_stage">OCA/crm</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions crm_required_field_by_stage/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import test_crm_required_field_by_stage
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Copyright 2024 KMEE
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import ast

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)

def test_get_view_required_fields(self):
arch, view = self.crm_lead_1._get_view(view_type="form")
node = arch.xpath("//field[@name='phone']")
self.assertTrue(node)
attrs = ast.literal_eval(node[0].attrib.get("attrs", "{}"))
self.assertIn("required", attrs)
self.assertIn(self.crm_stage_1.id, attrs["required"][0][2])
6 changes: 6 additions & 0 deletions setup/crm_required_field_by_stage/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)

0 comments on commit 2a8adec

Please sign in to comment.