[IMP] mail(_*): improve compute in batch and remove unnecessary defaults

Purpose of this commit is to clean field definitions by

  * doing compute in batch: especially when the loop can be reduced to a single
    computation / batch assignment (based on groups or config parameter for
    example);
  * remove default when having a compute as computes should completely define
    the field value at any time;

Some side dish code cleaning is performed at the same time: unnecessary
import or dead code removal.

Task-2638444
PR odoo/odoo#76005

Co-Authored-By: Thibault Delavallée <tde@odoo.com>
Co-Authored-By: Victor Feyens <vfe@odoo.com>
This commit is contained in:
Thibault Delavallée
2021-09-08 07:56:02 +00:00
co-authored by Victor Feyens
parent 8194093f80
commit e87b46864c
4 changed files with 20 additions and 26 deletions
+2 -7
View File
@@ -41,9 +41,6 @@ class AccountJournal(models.Model):
def _get_bank_statements_available_sources(self):
return self.__get_bank_statements_available_sources()
def _default_alias_domain(self):
return self.env["ir.config_parameter"].sudo().get_param("mail.catchall.domain")
def _default_invoice_reference_model(self):
"""Get the invoice reference model according to the company's country."""
country_code = self.env.company.country_id.code
@@ -172,7 +169,7 @@ class AccountJournal(models.Model):
alias_id = fields.Many2one('mail.alias', string='Email Alias', help="Send one separate email for each invoice.\n\n"
"Any file extension will be accepted.\n\n"
"Only PDF and XML files will be interpreted by Odoo", copy=False)
alias_domain = fields.Char('Alias domain', compute='_compute_alias_domain', default=_default_alias_domain, compute_sudo=True)
alias_domain = fields.Char('Alias domain', compute='_compute_alias_domain')
alias_name = fields.Char('Alias Name', copy=False, related='alias_id.alias_name', help="It creates draft invoices and bills by sending an email.", readonly=False)
journal_group_ids = fields.Many2many('account.journal.group',
@@ -327,9 +324,7 @@ class AccountJournal(models.Model):
journal.suspense_account_id = False
def _compute_alias_domain(self):
alias_domain = self._default_alias_domain()
for record in self:
record.alias_domain = alias_domain
self.alias_domain = self.env["ir.config_parameter"].sudo().get_param("mail.catchall.domain")
@api.constrains('type_control_ids')
def _constrains_type_control_ids(self):
+2 -7
View File
@@ -33,9 +33,6 @@ class Alias(models.Model):
_rec_name = 'alias_name'
_order = 'alias_model_id, alias_name'
def _default_alias_domain(self):
return self.env["ir.config_parameter"].sudo().get_param("mail.catchall.domain")
alias_name = fields.Char('Alias Name', copy=False, help="The name of the email alias, e.g. 'jobs' if you want to catch emails for <jobs@example.odoo.com>")
alias_model_id = fields.Many2one('ir.model', 'Aliased Model', required=True, ondelete="cascade",
help="The model (Odoo Document Kind) to which this alias "
@@ -57,7 +54,7 @@ class Alias(models.Model):
'Record Thread ID',
help="Optional ID of a thread (record) to which all incoming messages will be attached, even "
"if they did not reply to it. If set, this will disable the creation of new records completely.")
alias_domain = fields.Char('Alias domain', compute='_compute_alias_domain', default=_default_alias_domain)
alias_domain = fields.Char('Alias domain', compute='_compute_alias_domain')
alias_parent_model_id = fields.Many2one(
'ir.model', 'Parent Model',
help="Parent model holding the alias. The model holding the alias reference "
@@ -97,9 +94,7 @@ class Alias(models.Model):
))
def _compute_alias_domain(self):
alias_domain = self._default_alias_domain()
for record in self:
record.alias_domain = alias_domain
self.alias_domain = self.env["ir.config_parameter"].sudo().get_param("mail.catchall.domain")
@api.constrains('alias_defaults')
def _check_alias_defaults(self):
@@ -1,9 +1,7 @@
# -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.
from odoo import api, fields, models, _, tools
from odoo.osv import expression
from odoo import fields, models, _, tools
class MassMailing(models.Model):
@@ -14,8 +12,7 @@ class MassMailing(models.Model):
crm_lead_count = fields.Integer('Leads/Opportunities Count', groups='sales_team.group_sale_salesman', compute='_compute_crm_lead_count')
def _compute_use_leads(self):
for mass_mailing in self:
mass_mailing.use_leads = self.env.user.has_group('crm.group_use_lead')
self.use_leads = self.env.user.has_group('crm.group_use_lead')
def _compute_crm_lead_count(self):
lead_data = self.env['crm.lead'].with_context(active_test=False).read_group(
@@ -3,6 +3,7 @@
from odoo import api, fields, models, _, tools
from odoo.osv import expression
class MassMailing(models.Model):
@@ -15,18 +16,24 @@ class MassMailing(models.Model):
@api.depends('mailing_domain')
def _compute_sale_quotation_count(self):
has_so_access = self.env['sale.order'].check_access_rights('read', raise_exception=False)
if not has_so_access:
self.sale_quotation_count = 0
return
for mass_mailing in self:
mass_mailing.sale_quotation_count = self.env['sale.order'].search_count(mass_mailing._get_sale_utm_domain()) if has_so_access else 0
mass_mailing.sale_quotation_count = self.env['sale.order'].search_count(mass_mailing._get_sale_utm_domain())
@api.depends('mailing_domain')
def _compute_sale_invoiced_amount(self):
if not self.user_has_groups('sales_team.group_sale_salesman') or not self.user_has_groups('account.group_account_invoice'):
self.sale_invoiced_amount = 0
return
for mass_mailing in self:
if self.user_has_groups('sales_team.group_sale_salesman') and self.user_has_groups('account.group_account_invoice'):
domain = mass_mailing._get_sale_utm_domain() + [('state', 'not in', ['draft', 'cancel'])]
moves = self.env['account.move'].search_read(domain, ['amount_untaxed_signed'])
mass_mailing.sale_invoiced_amount = sum(i['amount_untaxed_signed'] for i in moves)
else:
mass_mailing.sale_invoiced_amount = 0
domain = expression.AND([
mass_mailing._get_sale_utm_domain(),
[('state', 'not in', ['draft', 'cancel'])]
])
moves = self.env['account.move'].search_read(domain, ['amount_untaxed_signed'])
mass_mailing.sale_invoiced_amount = sum(i['amount_untaxed_signed'] for i in moves)
def action_redirect_to_quotations(self):
action = self.env["ir.actions.actions"]._for_xml_id("sale.action_quotations_with_onboarding")