From 71ab12840bc787ca486202255cd6faa21281b9e1 Mon Sep 17 00:00:00 2001 From: Victor Feyens Date: Tue, 14 Jul 2020 14:29:10 +0000 Subject: [PATCH] [IMP] *: do not specify unwanted defaults in default_get When default_get is called, the wanted fields are specified through the fields_list arg. It is useless to fill the values for unwanted fields. As default_get is called for nearly all records creation, simplifying the default_get overrides: * remove potential wrong side-effects of the values * remove some useless or wrong defaults computations (searches, refs, ...) --- addons/account/models/account_account.py | 2 ++ addons/account/models/account_tax.py | 8 ++++---- addons/account/wizard/account_accrual_accounting.py | 2 ++ addons/account/wizard/account_move_reversal.py | 9 ++++++--- addons/base_gengo/wizard/base_gengo_translations.py | 3 ++- addons/hr_holidays/models/hr_leave.py | 12 ++++++++---- addons/hr_holidays/tests/test_access_rights.py | 2 +- addons/point_of_sale/models/pos_config.py | 4 ++-- addons/sms/wizard/sms_resend.py | 2 +- addons/website/wizard/website_robots.py | 3 ++- .../wizard/crm_forward_to_partner.py | 12 ++++++------ odoo/addons/base/wizard/base_partner_merge.py | 9 ++++++--- odoo/models.py | 13 +++++++++---- 13 files changed, 51 insertions(+), 30 deletions(-) diff --git a/addons/account/models/account_account.py b/addons/account/models/account_account.py index 1622338baf2..b7da1a1ac37 100644 --- a/addons/account/models/account_account.py +++ b/addons/account/models/account_account.py @@ -305,6 +305,8 @@ class AccountAccount(models.Model): """If we're creating a new account through a many2one, there are chances that we typed the account code instead of its name. In that case, switch both fields values. """ + if 'name' not in default_fields and 'code' not in default_fields: + return super().default_get(default_fields) default_name = self._context.get('default_name') default_code = self._context.get('default_code') if default_name and not default_code: diff --git a/addons/account/models/account_tax.py b/addons/account/models/account_tax.py index e9b69319652..19977d46831 100644 --- a/addons/account/models/account_tax.py +++ b/addons/account/models/account_tax.py @@ -98,21 +98,21 @@ class AccountTax(models.Model): ] @api.model - def default_get(self, vals): + def default_get(self, fields_list): # company_id is added so that we are sure to fetch a default value from it to use in repartition lines, below - rslt = super(AccountTax, self).default_get(vals + ['company_id']) + rslt = super(AccountTax, self).default_get(fields_list + ['company_id']) company_id = rslt.get('company_id') company = self.env['res.company'].browse(company_id) - if 'refund_repartition_line_ids' in vals: + if 'refund_repartition_line_ids' in fields_list: # We write on the related country_id field so that the field is recomputed. Without that, it will stay empty until we save the record. rslt['refund_repartition_line_ids'] = [ (0, 0, {'repartition_type': 'base', 'factor_percent': 100.0, 'tag_ids': [], 'company_id': company_id, 'country_id': company.country_id.id}), (0, 0, {'repartition_type': 'tax', 'factor_percent': 100.0, 'tag_ids': [], 'company_id': company_id, 'country_id': company.country_id.id}), ] - if 'invoice_repartition_line_ids' in vals: + if 'invoice_repartition_line_ids' in fields_list: # We write on the related country_id field so that the field is recomputed. Without that, it will stay empty until we save the record. rslt['invoice_repartition_line_ids'] = [ (0, 0, {'repartition_type': 'base', 'factor_percent': 100.0, 'tag_ids': [], 'company_id': company_id, 'country_id': company.country_id.id}), diff --git a/addons/account/wizard/account_accrual_accounting.py b/addons/account/wizard/account_accrual_accounting.py index b48b5d95a4b..2f22ac54e11 100644 --- a/addons/account/wizard/account_accrual_accounting.py +++ b/addons/account/wizard/account_accrual_accounting.py @@ -44,6 +44,8 @@ class AccrualAccountingWizard(models.TransientModel): if self.env.context.get('active_model') != 'account.move.line' or not self.env.context.get('active_ids'): raise UserError(_('This can only be used on journal items')) rec = super(AccrualAccountingWizard, self).default_get(fields) + if not set(fields) & set(['active_move_line_ids', 'company_id', 'account_type']): + return super().default_get(fields) active_move_line_ids = self.env['account.move.line'].browse(self.env.context['active_ids']) rec['active_move_line_ids'] = active_move_line_ids.ids diff --git a/addons/account/wizard/account_move_reversal.py b/addons/account/wizard/account_move_reversal.py index f83fc3a950c..b53d50fd513 100644 --- a/addons/account/wizard/account_move_reversal.py +++ b/addons/account/wizard/account_move_reversal.py @@ -41,9 +41,12 @@ class AccountMoveReversal(models.TransientModel): if any(move.state != "posted" for move in move_ids): raise UserError(_('You can only reverse posted moves.')) - res['company_id'] = move_ids.company_id.id or self.env.company.id - res['move_ids'] = [(6, 0, move_ids.ids)] - res['refund_method'] = (len(move_ids) > 1 or move_ids.move_type == 'entry') and 'cancel' or 'refund' + if 'company_id' in fields: + res['company_id'] = move_ids.company_id.id or self.env.company.id + if 'move_ids' in fields: + res['move_ids'] = [(6, 0, move_ids.ids)] + if 'refund_method' in fields: + res['refund_method'] = (len(move_ids) > 1 or move_ids.move_type == 'entry') and 'cancel' or 'refund' return res @api.depends('move_ids') diff --git a/addons/base_gengo/wizard/base_gengo_translations.py b/addons/base_gengo/wizard/base_gengo_translations.py index 7f070c00135..bf860bb7473 100644 --- a/addons/base_gengo/wizard/base_gengo_translations.py +++ b/addons/base_gengo/wizard/base_gengo_translations.py @@ -31,7 +31,8 @@ class BaseGengoTranslations(models.TransientModel): @api.model def default_get(self, fields): res = super(BaseGengoTranslations, self).default_get(fields) - res['authorized_credentials'], gengo = self.gengo_authentication() + if 'authorize_credentials' in fields: + res['authorized_credentials'], gengo = self.gengo_authentication() if 'lang_id' in fields: res['lang_id'] = get_lang(self.env).id return res diff --git a/addons/hr_holidays/models/hr_leave.py b/addons/hr_holidays/models/hr_leave.py index 9456c3ac33e..c16206aef79 100644 --- a/addons/hr_holidays/models/hr_leave.py +++ b/addons/hr_holidays/models/hr_leave.py @@ -73,11 +73,15 @@ class HolidaysRequest(models.Model): defaults = super(HolidaysRequest, self).default_get(fields_list) defaults = self._default_get_request_parameters(defaults) - LeaveType = self.env['hr.leave.type'].with_context(employee_id=defaults.get('employee_id'), default_date_from=defaults.get('date_from', fields.Datetime.now())) - lt = LeaveType.search([('valid', '=', True)], limit=1) + if 'holiday_status_id' in fields_list and not defaults.get('holiday_status_id'): + lt = self.env['hr.leave.type'].search([('valid', '=', True)], limit=1) - defaults['holiday_status_id'] = lt.id if lt else defaults.get('holiday_status_id') - defaults['state'] = 'confirm' if lt and lt.leave_validation_type != 'no_validation' else 'draft' + if lt: + defaults['holiday_status_id'] = lt.id + + if 'state' in fields_list and not defaults.get('state'): + lt = self.env['hr.leave.type'].browse(defaults.get('holiday_status_id')) + defaults['state'] = 'confirm' if lt and lt.leave_validation_type != 'no_validation' else 'draft' return defaults def _default_get_request_parameters(self, values): diff --git a/addons/hr_holidays/tests/test_access_rights.py b/addons/hr_holidays/tests/test_access_rights.py index 64d6787d57d..a29d658358a 100644 --- a/addons/hr_holidays/tests/test_access_rights.py +++ b/addons/hr_holidays/tests/test_access_rights.py @@ -208,7 +208,7 @@ class TestAcessRightsStates(TestHrHolidaysAccessRightsCommon): def test_holiday_user_draft_his_leave(self): """ Should be able to draft his own leave - whatever the holidays_status_id + whatever the holiday_status_id """ for i, status in enumerate(self.draft_status): values = { diff --git a/addons/point_of_sale/models/pos_config.py b/addons/point_of_sale/models/pos_config.py index f7f1a593826..def4c33c782 100644 --- a/addons/point_of_sale/models/pos_config.py +++ b/addons/point_of_sale/models/pos_config.py @@ -28,8 +28,8 @@ class AccountBankStmtCashWizard(models.Model): @api.model def default_get(self, fields): vals = super(AccountBankStmtCashWizard, self).default_get(fields) - if "is_a_template" in fields and self.env.context.get('default_is_a_template'): - vals['is_a_template'] = True + if 'cashbox_lines_ids' not in fields: + return vals config_id = self.env.context.get('default_pos_id') if config_id: config = self.env['pos.config'].browse(config_id) diff --git a/addons/sms/wizard/sms_resend.py b/addons/sms/wizard/sms_resend.py index f8b5c956977..24d59c1a5ea 100644 --- a/addons/sms/wizard/sms_resend.py +++ b/addons/sms/wizard/sms_resend.py @@ -27,7 +27,7 @@ class SMSResend(models.TransientModel): @api.model def default_get(self, fields): result = super(SMSResend, self).default_get(fields) - if result.get('mail_message_id'): + if 'recipient_ids' in fields and result.get('mail_message_id'): mail_message_id = self.env['mail.message'].browse(result['mail_message_id']) result['recipient_ids'] = [(0, 0, { 'notification_id': notif.id, diff --git a/addons/website/wizard/website_robots.py b/addons/website/wizard/website_robots.py index 2bd61a5721f..7172a3aa6b1 100644 --- a/addons/website/wizard/website_robots.py +++ b/addons/website/wizard/website_robots.py @@ -12,7 +12,8 @@ class WebsiteRobots(models.TransientModel): @api.model def default_get(self, fields): defaults = super(WebsiteRobots, self).default_get(fields) - defaults['content'] = self.env['website'].get_current_website().robots_txt + if 'content' in fields: + defaults['content'] = self.env['website'].get_current_website().robots_txt return defaults def action_save(self): diff --git a/addons/website_crm_partner_assign/wizard/crm_forward_to_partner.py b/addons/website_crm_partner_assign/wizard/crm_forward_to_partner.py index 9267f6300e3..2bcf16789ba 100644 --- a/addons/website_crm_partner_assign/wizard/crm_forward_to_partner.py +++ b/addons/website_crm_partner_assign/wizard/crm_forward_to_partner.py @@ -32,15 +32,15 @@ class CrmLeadForwardToPartner(models.TransientModel): @api.model def default_get(self, fields): - template = self.env.ref('website_crm_partner_assign.email_template_lead_forward_mail', False) - res = super(CrmLeadForwardToPartner, self).default_get(fields) active_ids = self.env.context.get('active_ids') - default_composition_mode = self.env.context.get('default_composition_mode') - res['assignation_lines'] = [] - if template: - res['body'] = template.body_html + if 'body' in fields: + template = self.env.ref('website_crm_partner_assign.email_template_lead_forward_mail', False) + if template: + res['body'] = template.body_html if active_ids: + default_composition_mode = self.env.context.get('default_composition_mode') + res['assignation_lines'] = [] leads = self.env['crm.lead'].browse(active_ids) if default_composition_mode == 'mass_mail': partner_assigned_dict = leads.search_geo_partner() diff --git a/odoo/addons/base/wizard/base_partner_merge.py b/odoo/addons/base/wizard/base_partner_merge.py index 2bd97ff085e..484964e3558 100644 --- a/odoo/addons/base/wizard/base_partner_merge.py +++ b/odoo/addons/base/wizard/base_partner_merge.py @@ -41,9 +41,12 @@ class MergePartnerAutomatic(models.TransientModel): res = super(MergePartnerAutomatic, self).default_get(fields) active_ids = self.env.context.get('active_ids') if self.env.context.get('active_model') == 'res.partner' and active_ids: - res['state'] = 'selection' - res['partner_ids'] = [(6, 0, active_ids)] - res['dst_partner_id'] = self._get_ordered_partner(active_ids)[-1].id + if 'state' in fields: + res['state'] = 'selection' + if 'partner_ids' in fields: + res['partner_ids'] = [(6, 0, active_ids)] + if 'dst_partner_id' in fields: + res['dst_partner_id'] = self._get_ordered_partner(active_ids)[-1].id return res # Group by diff --git a/odoo/models.py b/odoo/models.py index 3944401694f..44413e31cb9 100644 --- a/odoo/models.py +++ b/odoo/models.py @@ -1206,16 +1206,21 @@ class BaseModel(MetaModel('DummyModel', (object,), {'_register': False})): @api.model def default_get(self, fields_list): - """ default_get(fields) -> default_values + """ default_get(fields_list) -> default_values Return default values for the fields in ``fields_list``. Default values are determined by the context, user defaults, and the model itself. - :param fields_list: a list of field names - :return: a dictionary mapping each field name to its corresponding - default value, if it has one. + :param list fields_list: names of field whose default is requested + :return: a dictionary mapping field names to their corresponding default values, + if they have a default value. + :rtype: dict + .. note:: + + Unrequested defaults won't be considered, there is no need to return a + value for fields whose names are not in `fields_list`. """ # trigger view init hook self.view_init(fields_list)