-
-
Notifications
You must be signed in to change notification settings - Fork 791
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[16.0][ADD] Addon: project_required_field_by_stage #1341
Open
Tiago370
wants to merge
1
commit into
OCA:16.0
Choose a base branch
from
kmee:16.0-add-project_required_field_by_stage
base: 16.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 <https://github.com/OCA/project/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/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**>`_. | ||
|
||
Do not contact contributors directly about support or help with technical issues. | ||
|
||
Credits | ||
======= | ||
|
||
Authors | ||
------- | ||
|
||
* KMEE | ||
|
||
Contributors | ||
------------ | ||
|
||
- KMEE (`https://kmee.com.br/ <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 <https://github.com/OCA/project/tree/16.0/project_required_field_by_stage>`_ project on GitHub. | ||
|
||
You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from . import models |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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": [], | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
from . import project_task | ||
from . import project_task_type |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -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): | ||||||||
Comment on lines
+65
to
+66
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||
raise UserError( | ||||||||
_( | ||||||||
"Field '%(field)s' is mandatory in stage '%(stage)s'." | ||||||||
) | ||||||||
% ( | ||||||||
{ | ||||||||
"field": field.display_name.split(" (")[0], | ||||||||
"stage": s.display_name, | ||||||||
} | ||||||||
) | ||||||||
) |
15 changes: 15 additions & 0 deletions
15
project_required_field_by_stage/models/project_task_type.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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.", | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
* KMEE (https://kmee.com.br/): | ||
|
||
* Tiago Amaral |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe you can only go through the stages that have required fields assigned.