*: account_sale_timesheet, project_hr_expense, project_mrp, project_purchase, sale_project_account This commit fixes issue regarding fields in project form stat buttons. Those fields are evaluated in any cases, and should rather be computed only when the user has the right group. Steps to reproduce : 1) Install project with one of this bridge 2) Connect with a user with only the group_project_admin right 3) Go to the form view 4) Issue: An internal user with the group "Project Administrator" is unable to access the form view of a project because of missing rights on the X model. Desired Behavior ================ The stat button should not be shown and the field not computed. opw-2675514 closes odoo/odoo#78981 X-original-commit: 981bbf59d624c3a7bd22d6d7bf26c09e14835862 Related: odoo/enterprise#21889 Signed-off-by: Laurent Stukkens (ltu) <ltu@odoo.com>
63 lines
2.4 KiB
Python
63 lines
2.4 KiB
Python
# -*- coding: utf-8 -*-
|
|
# Part of Odoo. See LICENSE file for full copyright and licensing details.
|
|
|
|
from odoo import api, fields, models, _
|
|
|
|
|
|
class Project(models.Model):
|
|
_inherit = "project.project"
|
|
|
|
purchase_orders_count = fields.Integer('# Purchase Orders', compute='_compute_purchase_orders_count', groups='purchase.group_purchase_user')
|
|
|
|
@api.depends('analytic_account_id')
|
|
def _compute_purchase_orders_count(self):
|
|
purchase_orders_data = self.env['purchase.order.line'].read_group([
|
|
('account_analytic_id', '!=', False),
|
|
('account_analytic_id', 'in', self.analytic_account_id.ids)
|
|
], ['account_analytic_id', 'order_id:count_distinct'], ['account_analytic_id'])
|
|
mapped_data = dict([(data['account_analytic_id'][0], data['order_id']) for data in purchase_orders_data])
|
|
for project in self:
|
|
project.purchase_orders_count = mapped_data.get(project.analytic_account_id.id, 0)
|
|
|
|
# ----------------------------
|
|
# Actions
|
|
# ----------------------------
|
|
|
|
def action_open_project_purchase_orders(self):
|
|
purchase_orders = self.env['purchase.order'].search([
|
|
('order_line.account_analytic_id', '!=', False),
|
|
('order_line.account_analytic_id', 'in', self.analytic_account_id.ids)
|
|
])
|
|
action_window = {
|
|
'name': _('Purchase Orders'),
|
|
'type': 'ir.actions.act_window',
|
|
'res_model': 'purchase.order',
|
|
'views': [[False, 'tree'], [False, 'form']],
|
|
'domain': [('id', 'in', purchase_orders.ids)],
|
|
'context': {
|
|
'create': False,
|
|
}
|
|
}
|
|
if len(purchase_orders) == 1:
|
|
action_window['views'] = [[False, 'form']]
|
|
action_window['res_id'] = purchase_orders.id
|
|
return action_window
|
|
|
|
# ----------------------------
|
|
# Project Updates
|
|
# ----------------------------
|
|
|
|
def _get_stat_buttons(self):
|
|
buttons = super(Project, self)._get_stat_buttons()
|
|
if self.user_has_groups('purchase.group_purchase_user'):
|
|
buttons.append({
|
|
'icon': 'credit-card',
|
|
'text': _('Purchase Orders'),
|
|
'number': self.purchase_orders_count,
|
|
'action_type': 'object',
|
|
'action': 'action_open_project_purchase_orders',
|
|
'show': self.purchase_orders_count > 0,
|
|
'sequence': 13,
|
|
})
|
|
return buttons
|