diff --git a/project_required_field_by_stage/README.rst b/project_required_field_by_stage/README.rst new file mode 100644 index 0000000000..d2328c95ad --- /dev/null +++ b/project_required_field_by_stage/README.rst @@ -0,0 +1,93 @@ +=============================== +Project Required Field By Stage +=============================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:f79e2a80b9d910fee41985b78ab214ea48cf1891bfddd0bc0d59da8248fa40d4 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png + :target: https://odoo-community.org/page/development-status + :alt: Beta +.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png + :target: http://www.gnu.org/licenses/agpl-3.0-standalone.html + :alt: License: AGPL-3 +.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fproject-lightgray.png?logo=github + :target: https://github.com/OCA/project/tree/16.0/project_required_field_by_stage + :alt: OCA/project +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/project-16-0/project-16-0-project_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/project&target_branch=16.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module was written to extend the functionality of project task to +require certain fields to be filled out based on the stage of the task. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +To use this module, you need to: + +- Go to *Project > Configuration > Task Stages*. +- Select a stage. +- You will see the new field called required fields. +- Add the fields that you want to be required when the task is in this + stage. +- Save the stage. +- Now, when a task is in this stage, the selected fields will be + required. + +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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* KMEE + +Contributors +------------ + +- KMEE (`https://kmee.com.br/ `__): + + - Tiago Amaral + +Maintainers +----------- + +This module is maintained by the OCA. + +.. image:: https://odoo-community.org/logo.png + :alt: Odoo Community Association + :target: https://odoo-community.org + +OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use. + +This module is part of the `OCA/project `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/project_required_field_by_stage/__init__.py b/project_required_field_by_stage/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/project_required_field_by_stage/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/project_required_field_by_stage/__manifest__.py b/project_required_field_by_stage/__manifest__.py new file mode 100644 index 0000000000..3948f277ed --- /dev/null +++ b/project_required_field_by_stage/__manifest__.py @@ -0,0 +1,17 @@ +# Copyright 2024 KMEE +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +{ + "name": "Project Required Field By Stage", + "summary": """ + KMEE""", + "version": "16.0.1.0.0", + "license": "AGPL-3", + "author": "KMEE,Odoo Community Association (OCA)", + "website": "https://github.com/OCA/project", + "depends": ["project"], + "data": [ + "views/project_task_type.xml", + ], + "demo": [], +} diff --git a/project_required_field_by_stage/models/__init__.py b/project_required_field_by_stage/models/__init__.py new file mode 100644 index 0000000000..62f8791bdc --- /dev/null +++ b/project_required_field_by_stage/models/__init__.py @@ -0,0 +1,2 @@ +from . import project_task +from . import project_task_type diff --git a/project_required_field_by_stage/models/project_task.py b/project_required_field_by_stage/models/project_task.py new file mode 100644 index 0000000000..e1ae00765f --- /dev/null +++ b/project_required_field_by_stage/models/project_task.py @@ -0,0 +1,77 @@ +# Copyright 2024 KMEE +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +import ast +import json as simplejson + +from odoo import _, api, models +from odoo.exceptions import UserError + + +class ProjectTask(models.Model): + + _inherit = "project.task" + + @api.model + def _get_view(self, view_id=None, view_type="form", **options): + arch, view = super()._get_view(view_id, view_type, **options) + stages = self.env["project.task.type"].search( + [("required_field_ids", "!=", False)] + ) + if view.type == "form" and stages: + for field in stages.mapped("required_field_ids"): + stages_with_field = stages.filtered( + lambda stage, field=field: field in stage.required_field_ids + ) + for node in arch.xpath("//field[@name='%s']" % field.name): + 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 + def _get_view_cache_key(self, view_id=None, view_type="form", **options): + """The override of _get_view changing the required fields labels according + to the stage makes the view cache dependent on the stages with required fields.""" + key = super()._get_view_cache_key(view_id, view_type, **options) + return key + tuple( + self.env["project.task.type"] + .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["project.task.type"].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/project_required_field_by_stage/models/project_task_type.py b/project_required_field_by_stage/models/project_task_type.py new file mode 100644 index 0000000000..58bbc01339 --- /dev/null +++ b/project_required_field_by_stage/models/project_task_type.py @@ -0,0 +1,15 @@ +# Copyright 2024 KMEE +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ProjectTaskType(models.Model): + + _inherit = "project.task.type" + + required_field_ids = fields.Many2many( + comodel_name="ir.model.fields", + domain=[("model", "=", "project.task")], + help="Fields that are required when the task is in this stage.", + ) diff --git a/project_required_field_by_stage/readme/CONTRIBUTORS.md b/project_required_field_by_stage/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..61b5ce35d7 --- /dev/null +++ b/project_required_field_by_stage/readme/CONTRIBUTORS.md @@ -0,0 +1,3 @@ +* KMEE (https://kmee.com.br/): + + * Tiago Amaral diff --git a/project_required_field_by_stage/readme/DESCRIPTION.md b/project_required_field_by_stage/readme/DESCRIPTION.md new file mode 100644 index 0000000000..6d88f2cf1c --- /dev/null +++ b/project_required_field_by_stage/readme/DESCRIPTION.md @@ -0,0 +1 @@ +This module was written to extend the functionality of project task to require certain fields to be filled out based on the stage of the task. diff --git a/project_required_field_by_stage/readme/USAGE.md b/project_required_field_by_stage/readme/USAGE.md new file mode 100644 index 0000000000..feb0ce68de --- /dev/null +++ b/project_required_field_by_stage/readme/USAGE.md @@ -0,0 +1,8 @@ +To use this module, you need to: + +- Go to *Project \> Configuration \> Task Stages*. +- Select a stage. +- You will see the new field called required fields. +- Add the fields that you want to be required when the task is in this stage. +- Save the stage. +- Now, when a task is in this stage, the selected fields will be required. diff --git a/project_required_field_by_stage/static/description/icon.png b/project_required_field_by_stage/static/description/icon.png new file mode 100644 index 0000000000..3a0328b516 Binary files /dev/null and b/project_required_field_by_stage/static/description/icon.png differ diff --git a/project_required_field_by_stage/static/description/index.html b/project_required_field_by_stage/static/description/index.html new file mode 100644 index 0000000000..b048d65789 --- /dev/null +++ b/project_required_field_by_stage/static/description/index.html @@ -0,0 +1,442 @@ + + + + + +Project Required Field By Stage + + + +
+

Project Required Field By Stage

+ + +

Beta License: AGPL-3 OCA/project Translate me on Weblate Try me on Runboat

+

This module was written to extend the functionality of project task to +require certain fields to be filled out based on the stage of the task.

+

Table of contents

+ +
+

Usage

+

To use this module, you need to:

+
    +
  • Go to Project > Configuration > Task Stages.
  • +
  • Select a stage.
  • +
  • You will see the new field called required fields.
  • +
  • Add the fields that you want to be required when the task is in this +stage.
  • +
  • Save the stage.
  • +
  • Now, when a task is in this stage, the selected fields will be +required.
  • +
+
+
+

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.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • KMEE
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

OCA, or the Odoo Community Association, is a nonprofit organization whose +mission is to support the collaborative development of Odoo features and +promote its widespread use.

+

This module is part of the OCA/project project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/project_required_field_by_stage/tests/__init__.py b/project_required_field_by_stage/tests/__init__.py new file mode 100644 index 0000000000..afa0d01d72 --- /dev/null +++ b/project_required_field_by_stage/tests/__init__.py @@ -0,0 +1 @@ +from . import test_project_required_field_by_stage diff --git a/project_required_field_by_stage/tests/test_project_required_field_by_stage.py b/project_required_field_by_stage/tests/test_project_required_field_by_stage.py new file mode 100644 index 0000000000..8479e609fd --- /dev/null +++ b/project_required_field_by_stage/tests/test_project_required_field_by_stage.py @@ -0,0 +1,81 @@ +# 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 TestProjectRequiredFieldByStage(TransactionCase): + def setUp(self): + super().setUp() + + self.project_project_model = self.env["project.project"] + self.project_task_type_model = self.env["project.task.type"] + self.project_task_model = self.env["project.task"] + self.res_users_model = self.env["res.users"] + self.project_project_1 = self.project_project_model.create( + { + "name": "Project 1", + } + ) + self.project_task_type_1 = self.project_task_type_model.create( + { + "name": "Project Stage 1", + "project_ids": [(4, self.project_project_1.id)], + } + ) + self.project_task_type_2 = self.project_task_type_model.create( + { + "name": "Project Stage 2", + "required_field_ids": [ + (4, self.env.ref("project.field_project_task__user_ids").id) + ], + "project_ids": [(4, self.project_project_1.id)], + } + ) + + self.project_task_1 = self.project_task_model.create( + { + "name": "Project Task 1", + "project_id": self.project_project_1.id, + "user_ids": False, + "stage_id": self.project_task_type_1.id, + } + ) + self.res_users_1 = self.res_users_model.create( + { + "name": "User 1", + "login": "user@example.com", + "email": "user@example.com", + "active": True, + } + ) + + def test_locking(self): + with self.assertRaises(UserError): + self.project_task_1.write( + { + "stage_id": self.project_task_type_2.id, + } + ) + self.project_task_1.write( + { + "user_ids": [(4, self.res_users_1.id)], + } + ) + self.project_task_1.write( + { + "stage_id": self.project_task_type_2.id, + } + ) + self.assertEqual(self.project_task_1.stage_id.id, self.project_task_type_2.id) + + def test_get_view_required_fields(self): + arch, view = self.project_task_1._get_view(view_type="form") + node = arch.xpath("//field[@name='user_ids']") + self.assertTrue(node) + attrs = ast.literal_eval(node[0].attrib.get("attrs", "{}")) + self.assertIn("required", attrs) + self.assertIn(self.project_task_type_2.id, attrs["required"][0][2]) diff --git a/project_required_field_by_stage/views/project_task_type.xml b/project_required_field_by_stage/views/project_task_type.xml new file mode 100644 index 0000000000..0e36f90e32 --- /dev/null +++ b/project_required_field_by_stage/views/project_task_type.xml @@ -0,0 +1,21 @@ + + + + + project.task.type.form in (project_required_field_by_stage) + project.task.type + + + + + + + + + diff --git a/setup/project_required_field_by_stage/odoo/addons/project_required_field_by_stage b/setup/project_required_field_by_stage/odoo/addons/project_required_field_by_stage new file mode 120000 index 0000000000..5d31c58873 --- /dev/null +++ b/setup/project_required_field_by_stage/odoo/addons/project_required_field_by_stage @@ -0,0 +1 @@ +../../../../project_required_field_by_stage \ No newline at end of file diff --git a/setup/project_required_field_by_stage/setup.py b/setup/project_required_field_by_stage/setup.py new file mode 100644 index 0000000000..28c57bb640 --- /dev/null +++ b/setup/project_required_field_by_stage/setup.py @@ -0,0 +1,6 @@ +import setuptools + +setuptools.setup( + setup_requires=['setuptools-odoo'], + odoo_addon=True, +)