Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions project_required_field_by_stage/README.rst
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/):

- 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.
1 change: 1 addition & 0 deletions project_required_field_by_stage/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
17 changes: 17 additions & 0 deletions project_required_field_by_stage/__manifest__.py
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": [],
}
2 changes: 2 additions & 0 deletions project_required_field_by_stage/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from . import project_task
from . import project_task_type
77 changes: 77 additions & 0 deletions project_required_field_by_stage/models/project_task.py
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 this in self:
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I miss something, but I do not see why the inner loops are necessary. A project can only be in one stage, so why would this be wrong?

    @api.constrains("stage_id")
    def _check_stage_id_(self):
        for this in self:
            required_fields = this.stage_id.required_field_ids
            if not required_fields:
                continue
            for field in required_fields:
                if hasattr(this, field.name) and not getattr(this, field.name):
                    raise UserError(
                        _(  
                            "Field '%(field)s' is mandatory in stage '%(stage)s'."
                        )   
                        % ( 
                            {   
                                "field": field.display_name.split(" (")[0],
                                "stage": this.stage_id.display_name,
                            }   
                        )   
                    ) 

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah yes I agree, I see that stage_id is a Many2one field

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Every record has one stage. we only need to check mandatory fields for that one stage, not all stages every time. thank you @NL66278 , i will simplify this , by removing the loop.

stages = this.stage_id.filtered(lambda x: x.required_field_ids)
for stage in stages:
fields = (
this.env["ir.model.fields"]
.sudo()
.search([("id", "in", stage.required_field_ids.ids)])
)
for rec in this.filtered(lambda x: x.stage_id == stage):
for field in fields:
if hasattr(rec, field.name) and not getattr(rec, field.name):
raise UserError(
_(
"Field '%(field)s' is mandatory in stage '%(stage)s'."
)
% (
{
"field": field.display_name.split(" (")[0],
"stage": stage.display_name,
}
)
)
15 changes: 15 additions & 0 deletions project_required_field_by_stage/models/project_task_type.py
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.",
)
3 changes: 3 additions & 0 deletions project_required_field_by_stage/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* KMEE (https://kmee.com.br/):

* Tiago Amaral
1 change: 1 addition & 0 deletions project_required_field_by_stage/readme/DESCRIPTION.md
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.
8 changes: 8 additions & 0 deletions project_required_field_by_stage/readme/USAGE.md
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.
Loading