diff options
| author | Rémy Voet <ryv@odoo.com> | 2019-12-13 11:48:36 +0000 |
|---|---|---|
| committer | Thibault Delavallée <tde@odoo.com> | 2019-12-16 15:58:16 +0000 |
| commit | ffe1d4047da181f97fafe2a69f64e6f283fbeb0e (patch) | |
| tree | 1d477ff52f6768ada194c9cbe1b1f96c71bba28d /addons/mass_mailing_sms/models | |
| parent | 4c76247a889cde3f9708d12e2f38de0c2c74cc99 (diff) | |
[REF] mass_mailing: move from onchange / default to stored editable computed fields
PURPOSE
Try to move from onchange / default_get to stored editable computed fields.
Normally behavior should be the same (computed or set by user), with support
in create / write + onchange without additional code.
SPECIFICATIONS
Update classic fields with onchange to stored editable computed fields. It
means their value will come either from manual user input, either computed
based on triggers. Purpose is to remove all onchange and default_get when
possible.
Clean fields definition inconsistencies, like default / required on computed
fields. Indeed computed fields should always have a value, maybe coming from
user input. They should not have default / required that are attributes for
classic fields.
LINKS
Task ID 2088577
PR #41877
Enterprise PR odoo/enterprise#7278
Diffstat (limited to 'addons/mass_mailing_sms/models')
| -rw-r--r-- | addons/mass_mailing_sms/models/mailing_mailing.py | 38 |
1 files changed, 14 insertions, 24 deletions
diff --git a/addons/mass_mailing_sms/models/mailing_mailing.py b/addons/mass_mailing_sms/models/mailing_mailing.py index 1738e03dc78..2f51dd7eeca 100644 --- a/addons/mass_mailing_sms/models/mailing_mailing.py +++ b/addons/mass_mailing_sms/models/mailing_mailing.py @@ -22,7 +22,7 @@ class Mailing(models.Model): # mailing options mailing_type = fields.Selection(selection_add=[('sms', 'SMS')]) # sms options - body_plaintext = fields.Text('SMS Body') + body_plaintext = fields.Text('SMS Body', compute='_compute_body_plaintext', store=True, readonly=False) sms_template_id = fields.Many2one('sms.template', string='SMS Template', ondelete='set null') sms_has_insufficient_credit = fields.Boolean( 'Insufficient IAP credits', compute='_compute_sms_has_insufficient_credit', @@ -32,17 +32,20 @@ class Mailing(models.Model): # opt_out_link sms_allow_unsubscribe = fields.Boolean('Include opt-out link', default=False) - @api.onchange('mailing_type') - def _onchange_mailing_type(self): - if self.mailing_type == 'sms' and (not self.medium_id or self.medium_id == self.env.ref('utm.utm_medium_email')): - self.medium_id = self.env.ref('mass_mailing_sms.utm_medium_sms').id - elif self.mailing_type == 'mail' and (not self.medium_id or self.medium_id == self.env.ref('mass_mailing_sms.utm_medium_sms')): - self.medium_id = self.env.ref('utm.utm_medium_email').id + @api.depends('mailing_type') + def _compute_medium_id(self): + super(Mailing, self)._compute_medium_id() + for mailing in self: + if mailing.mailing_type == 'sms' and (not mailing.medium_id or mailing.medium_id == self.env.ref('utm.utm_medium_email')): + mailing.medium_id = self.env.ref('mass_mailing_sms.utm_medium_sms').id + elif mailing.mailing_type == 'mail' and (not mailing.medium_id or mailing.medium_id == self.env.ref('mass_mailing_sms.utm_medium_sms')): + mailing.medium_id = self.env.ref('utm.utm_medium_email').id - @api.onchange('sms_template_id', 'mailing_type') - def _onchange_sms_template_id(self): - if self.mailing_type == 'sms' and self.sms_template_id: - self.body_plaintext = self.sms_template_id.body + @api.depends('sms_template_id', 'mailing_type') + def _compute_body_plaintext(self): + for mailing in self: + if mailing.mailing_type == 'sms' and mailing.sms_template_id: + mailing.body_plaintext = mailing.sms_template_id.body @api.depends('mailing_trace_ids.failure_type') def _compute_sms_has_insufficient_credit(self): @@ -55,19 +58,6 @@ class Mailing(models.Model): mailing.sms_has_insufficient_credit = mailing in mailing_ids # -------------------------------------------------- - # CRUD - # -------------------------------------------------- - - @api.model - def create(self, values): - if values.get('mailing_type') == 'sms': - if not values.get('medium_id'): - values['medium_id'] = self.env.ref('mass_mailing_sms.utm_medium_sms').id - if values.get('sms_template_id') and not values.get('body_plaintext'): - values['body_plaintext'] = self.env['sms.template'].browse(values['sms_template_id']).body - return super(Mailing, self).create(values) - - # -------------------------------------------------- # BUSINESS / VIEWS ACTIONS # -------------------------------------------------- |
