Skip to content

Commit 8e2d9a3

Browse files
author
Tiago Amaral
committed
[ADD] Addon: project_required_field_by_stage
1 parent 8ed45f4 commit 8e2d9a3

File tree

16 files changed

+769
-0
lines changed

16 files changed

+769
-0
lines changed
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
===============================
2+
Project Required Field By Stage
3+
===============================
4+
5+
..
6+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
7+
!! This file is generated by oca-gen-addon-readme !!
8+
!! changes will be overwritten. !!
9+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
10+
!! source digest: sha256:f79e2a80b9d910fee41985b78ab214ea48cf1891bfddd0bc0d59da8248fa40d4
11+
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
12+
13+
.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
14+
:target: https://odoo-community.org/page/development-status
15+
:alt: Beta
16+
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
17+
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
18+
:alt: License: AGPL-3
19+
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fproject-lightgray.png?logo=github
20+
:target: https://github.com/OCA/project/tree/16.0/project_required_field_by_stage
21+
:alt: OCA/project
22+
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
23+
:target: https://translation.odoo-community.org/projects/project-16-0/project-16-0-project_required_field_by_stage
24+
:alt: Translate me on Weblate
25+
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
26+
:target: https://runboat.odoo-community.org/builds?repo=OCA/project&target_branch=16.0
27+
:alt: Try me on Runboat
28+
29+
|badge1| |badge2| |badge3| |badge4| |badge5|
30+
31+
This module was written to extend the functionality of project task to
32+
require certain fields to be filled out based on the stage of the task.
33+
34+
**Table of contents**
35+
36+
.. contents::
37+
:local:
38+
39+
Usage
40+
=====
41+
42+
To use this module, you need to:
43+
44+
- Go to *Project > Configuration > Task Stages*.
45+
- Select a stage.
46+
- You will see the new field called required fields.
47+
- Add the fields that you want to be required when the task is in this
48+
stage.
49+
- Save the stage.
50+
- Now, when a task is in this stage, the selected fields will be
51+
required.
52+
53+
Bug Tracker
54+
===========
55+
56+
Bugs are tracked on `GitHub Issues <https://github.com/OCA/project/issues>`_.
57+
In case of trouble, please check there if your issue has already been reported.
58+
If you spotted it first, help us to smash it by providing a detailed and welcomed
59+
`feedback <https://github.com/OCA/project/issues/new?body=module:%20project_required_field_by_stage%0Aversion:%2016.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
60+
61+
Do not contact contributors directly about support or help with technical issues.
62+
63+
Credits
64+
=======
65+
66+
Authors
67+
-------
68+
69+
* KMEE
70+
71+
Contributors
72+
------------
73+
74+
- KMEE (`https://kmee.com.br/ <https://kmee.com.br/>`__):
75+
76+
- Tiago Amaral
77+
78+
Maintainers
79+
-----------
80+
81+
This module is maintained by the OCA.
82+
83+
.. image:: https://odoo-community.org/logo.png
84+
:alt: Odoo Community Association
85+
:target: https://odoo-community.org
86+
87+
OCA, or the Odoo Community Association, is a nonprofit organization whose
88+
mission is to support the collaborative development of Odoo features and
89+
promote its widespread use.
90+
91+
This module is part of the `OCA/project <https://github.com/OCA/project/tree/16.0/project_required_field_by_stage>`_ project on GitHub.
92+
93+
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
from . import models
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Copyright 2024 KMEE
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
4+
{
5+
"name": "Project Required Field By Stage",
6+
"summary": """
7+
KMEE""",
8+
"version": "16.0.1.0.0",
9+
"license": "AGPL-3",
10+
"author": "KMEE,Odoo Community Association (OCA)",
11+
"website": "https://github.com/OCA/project",
12+
"depends": ["project"],
13+
"data": [
14+
"views/project_task_type.xml",
15+
],
16+
"demo": [],
17+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from . import project_task
2+
from . import project_task_type
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Copyright 2024 KMEE
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
4+
import ast
5+
import json as simplejson
6+
7+
from odoo import _, api, models
8+
from odoo.exceptions import UserError
9+
10+
11+
class ProjectTask(models.Model):
12+
13+
_inherit = "project.task"
14+
15+
@api.model
16+
def _get_view(self, view_id=None, view_type="form", **options):
17+
arch, view = super()._get_view(view_id, view_type, **options)
18+
stages = self.env["project.task.type"].search(
19+
[("required_field_ids", "!=", False)]
20+
)
21+
if view.type == "form" and stages:
22+
for field in stages.mapped("required_field_ids"):
23+
stages_with_field = stages.filtered(
24+
lambda stage, field=field: field in stage.required_field_ids
25+
)
26+
for node in arch.xpath("//field[@name='%s']" % field.name):
27+
attrs = ast.literal_eval(node.attrib.get("attrs", "{}"))
28+
if attrs:
29+
if attrs.get("required"):
30+
attrs["required"] = [
31+
"|",
32+
("stage_id", "in", stages_with_field.ids),
33+
] + attrs["required"]
34+
else:
35+
attrs["required"] = [
36+
("stage_id", "in", stages_with_field.ids)
37+
]
38+
else:
39+
attrs["required"] = [("stage_id", "in", stages_with_field.ids)]
40+
node.set("attrs", simplejson.dumps(attrs))
41+
return arch, view
42+
43+
@api.model
44+
def _get_view_cache_key(self, view_id=None, view_type="form", **options):
45+
"""The override of _get_view changing the required fields labels according
46+
to the stage makes the view cache dependent on the stages with required fields."""
47+
key = super()._get_view_cache_key(view_id, view_type, **options)
48+
return key + tuple(
49+
self.env["project.task.type"]
50+
.search([("required_field_ids", "!=", False)])
51+
.mapped("required_field_ids.name")
52+
)
53+
54+
@api.constrains("stage_id")
55+
def _check_stage_id_(self):
56+
for rec in self:
57+
stage = self.env["project.task.type"].search([("id", "=", rec.stage_id.id)])
58+
for s in stage:
59+
fields = (
60+
self.env["ir.model.fields"]
61+
.sudo()
62+
.search([("id", "in", s.required_field_ids.ids)])
63+
)
64+
for field in fields:
65+
if hasattr(self, "%s" % field.name):
66+
if not getattr(self, "%s" % field.name):
67+
raise UserError(
68+
_(
69+
"Field '%(field)s' is mandatory in stage '%(stage)s'."
70+
)
71+
% (
72+
{
73+
"field": field.display_name.split(" (")[0],
74+
"stage": s.display_name,
75+
}
76+
)
77+
)
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Copyright 2024 KMEE
2+
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).
3+
4+
from odoo import fields, models
5+
6+
7+
class ProjectTaskType(models.Model):
8+
9+
_inherit = "project.task.type"
10+
11+
required_field_ids = fields.Many2many(
12+
comodel_name="ir.model.fields",
13+
domain=[("model", "=", "project.task")],
14+
help="Fields that are required when the task is in this stage.",
15+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
* KMEE (https://kmee.com.br/):
2+
3+
* Tiago Amaral
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
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.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
To use this module, you need to:
2+
3+
- Go to *Project \> Configuration \> Task Stages*.
4+
- Select a stage.
5+
- You will see the new field called required fields.
6+
- Add the fields that you want to be required when the task is in this stage.
7+
- Save the stage.
8+
- Now, when a task is in this stage, the selected fields will be required.
9.23 KB
Loading

0 commit comments

Comments
 (0)