[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, ...)
This commit is contained in:
@@ -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:
|
||||
|
||||
@@ -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}),
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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')
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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 = {
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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()
|
||||
|
||||
@@ -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
|
||||
|
||||
+9
-4
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user