Files
odoo_source/addons/project/models/project_collaborator.py
T
Rémy Voet (ryv) c5cb357d90 [IMP] *: add dependencies to display_name field
The previous commit introduced an optimization to reduce the number of
queries and fields fetched when we call `name_search`. But it works much
better when the dependencies of `display_name` contain field names used
in the calculation (on the same record/model).

Then, to improve the performance and the cache coherency, add `depends`
and `depends_context` depending on the custom `_compute_display_name`.
Add only the first level of dependencies (never traverse relational
field) because only these have a positive impact on the previous
optimization and the cost is very low (see `modified`).
About `depends_context`, we don't include `lang` because (when `_` is
used by example) it is unlikely to get the same display_name in the same
request with two different lang.

closes odoo/odoo#122085

Related: odoo/documentation#4639
Related: odoo/enterprise#42599
Related: odoo/upgrade#4780
Signed-off-by: Raphael Collet <rco@odoo.com>
2023-06-28 17:41:19 +02:00

58 lines
2.8 KiB
Python

# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models
class ProjectCollaborator(models.Model):
_name = 'project.collaborator'
_description = 'Collaborators in project shared'
project_id = fields.Many2one('project.project', 'Project Shared', domain=[('privacy_visibility', '=', 'portal')], required=True, readonly=True)
partner_id = fields.Many2one('res.partner', 'Collaborator', required=True, readonly=True)
partner_email = fields.Char(related='partner_id.email')
_sql_constraints = [
('unique_collaborator', 'UNIQUE(project_id, partner_id)', 'A collaborator cannot be selected more than once in the project sharing access. Please remove duplicate(s) and try again.'),
]
@api.depends('project_id', 'partner_id')
def _compute_display_name(self):
for collaborator in self:
collaborator.display_name = f'{collaborator.project_id.display_name} - {collaborator.partner_id.display_name}'
@api.model_create_multi
def create(self, vals_list):
collaborator = self.env['project.collaborator'].search([], limit=1)
project_collaborators = super().create(vals_list)
if not collaborator:
self._toggle_project_sharing_portal_rules(True)
return project_collaborators
def unlink(self):
res = super().unlink()
# Check if it remains at least a collaborator in all shared projects.
collaborator = self.env['project.collaborator'].search([], limit=1)
if not collaborator: # then disable the project sharing feature
self._toggle_project_sharing_portal_rules(False)
return res
@api.model
def _toggle_project_sharing_portal_rules(self, active):
""" Enable/disable project sharing feature
When the first collaborator is added in the model then we need to enable the feature.
In the inverse case, if no collaborator is stored in the model then we disable the feature.
To enable/disable the feature, we just need to enable/disable the ir.model.access and ir.rule
added to portal user that we do not want to give when we know the project sharing is unused.
:param active: contains boolean value, True to enable the project sharing feature, otherwise we disable the feature.
"""
access_project_sharing_portal = self.env.ref('project.access_project_sharing_task_portal').sudo()
if access_project_sharing_portal.active != active:
access_project_sharing_portal.write({'active': active})
task_portal_ir_rule = self.env.ref('project.project_task_rule_portal_project_sharing').sudo()
if task_portal_ir_rule.active != active:
task_portal_ir_rule.write({'active': active})