Skip to content

Commit

Permalink
[ADD] Addon: project_required_field_by_stage
Browse files Browse the repository at this point in the history
  • Loading branch information
Tiago Amaral committed Sep 28, 2024
1 parent 8ed45f4 commit e0829bc
Show file tree
Hide file tree
Showing 22 changed files with 1,089 additions and 0 deletions.
214 changes: 214 additions & 0 deletions project_required_field_by_stage/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,214 @@
===============================
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 file must be max 2-3 paragraphs, and is required.

The goal of this document is to explain quickly the features of this
module: “what” this module does and “what” it is for. ]

Example:

This module extends the functionality of ... to support ... and to allow
users to ...

**Table of contents**

.. contents::
:local:

Use Cases / Context
===================

[ This file is optional but strongly suggested to allow end-users to
evaluate the module's usefulness in their context. ]

BUSINESS NEED: It should explain the “why” of the module:

- what is the business requirement that generated the need to develop
this module
- in which context or use cases this module can be useful (practical
examples are welcome!).

APPROACH: It could also explain the approach to address the mentioned
need.

USEFUL INFORMATION: It can also inform on related modules:

- modules it depends on and their features
- other modules that can work well together with this one
- suggested setups where the module is useful (eg: multicompany,
multi-website)

Installation
============

[ This file must only be present if there are very specific installation
instructions, such as installing non-python dependencies. The audience
is systems administrators. ]

To install this module, you need to:

1. Do this ...

Configuration
=============

[ This file is not always required; it should explain **how to configure
the module before using it**; it is aimed at users with administration
privileges.

Please be detailed on the path to configuration (eg: do you need to
activate developer mode?), describe step by step configurations and the
use of screenshots is strongly recommended.]

To configure this module, you need to:

- Go to *App* > Menu > Menu item
- Activate boolean… > save
- …

Usage
=====

[ This file is required and contains the instructions on **“how”** to
use the module for end-users.

If the module does not have a visible impact on the user interface, just
add the following sentence:

This module does not impact the user interface.

If that’s not the case, please make sure that every usage step is
covered and remember that images speak more than words!]

To use this module, you need to:

- Go to *App* > Menu > Menu item

*insert screenshot!*

- In “Contact” form, add a value to field *xyz* > save

*insert screenshot!*

- The value of *xyz* is now displayed in the list view.

*insert screenshot!*

Known issues / Roadmap
======================

[ Enumerate known caveats and future potential improvements. It is
mostly intended for end-users, and can also help potential new
contributors discovering new features to implement. ]

- ...

Changelog
=========

[ The change log. The goal of this file is to help readers understand
changes between version. The primary audience is end users and
integrators. Purely technical changes such as code refactoring must not
be mentioned here.

This file may contain ONE level of section titles, underlined with the ~
(tilde) character. Other section markers are forbidden and will likely
break the structure of the README.rst or other documents where this
fragment is included. ]

11.0.x.y.z (YYYY-MM-DD)
-----------------------

- [BREAKING] Breaking changes come first.
(`#70 <https://github.com/OCA/repo/issues/70>`__)
- [ADD] New feature. (`#74 <https://github.com/OCA/repo/issues/74>`__)
- [FIX] Correct this. (`#71 <https://github.com/OCA/repo/issues/71>`__)

.. _x.y.z-yyyy-mm-dd-1:

11.0.x.y.z (YYYY-MM-DD)
-----------------------

- ...

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
------------

- Firstname Lastname [email protected] (optional company
website url)
- Second Person [email protected] (optional company website
url)

Other credits
-------------

[ This file is optional and contains additional credits, other than
authors, contributors, and maintainers. ]

The development of this module has been financially supported by:

- Company 1 name
- Company 2 name

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"] = [

Check warning on line 30 in project_required_field_by_stage/models/project_task.py

View check run for this annotation

Codecov / codecov/patch

project_required_field_by_stage/models/project_task.py#L30

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

Check warning on line 35 in project_required_field_by_stage/models/project_task.py

View check run for this annotation

Codecov / codecov/patch

project_required_field_by_stage/models/project_task.py#L35

Added line #L35 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
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,
}
)
)
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.",
)
10 changes: 10 additions & 0 deletions project_required_field_by_stage/readme/CONFIGURE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[ This file is not always required; it should explain **how to configure the module before using it**; it is aimed at users with administration privileges.

Please be detailed on the path to configuration (eg: do you need to activate developer mode?), describe step by step configurations and the use of screenshots is strongly recommended.]


To configure this module, you need to:

- Go to *App* > Menu > Menu item
- Activate boolean… > save
-
16 changes: 16 additions & 0 deletions project_required_field_by_stage/readme/CONTEXT.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[ This file is optional but strongly suggested to allow end-users to evaluate the
module's usefulness in their context. ]

BUSINESS NEED:
It should explain the “why” of the module:
- what is the business requirement that generated the need to develop this module
- in which context or use cases this module can be useful (practical examples are welcome!).

APPROACH:
It could also explain the approach to address the mentioned need.

USEFUL INFORMATION:
It can also inform on related modules:
- modules it depends on and their features
- other modules that can work well together with this one
- suggested setups where the module is useful (eg: multicompany, multi-website)
2 changes: 2 additions & 0 deletions project_required_field_by_stage/readme/CONTRIBUTORS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Firstname Lastname <[email protected]> (optional company website url)
- Second Person <[email protected]> (optional company website url)
7 changes: 7 additions & 0 deletions project_required_field_by_stage/readme/CREDITS.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[ This file is optional and contains additional credits, other than
authors, contributors, and maintainers. ]

The development of this module has been financially supported by:

- Company 1 name
- Company 2 name
7 changes: 7 additions & 0 deletions project_required_field_by_stage/readme/DESCRIPTION.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[ This file must be max 2-3 paragraphs, and is required.

The goal of this document is to explain quickly the features of this module: “what” this module does and “what” it is for. ]

Example:

This module extends the functionality of ... to support ... and to allow users to ...
Loading

0 comments on commit e0829bc

Please sign in to comment.