diff options
| author | Raphael Collet <rco@openerp.com> | 2015-12-21 15:06:48 +0100 |
|---|---|---|
| committer | Raphael Collet <rco@openerp.com> | 2016-01-08 16:22:05 +0100 |
| commit | 7bada82ce37d99ba810cf95a683539203c84501b (patch) | |
| tree | 7ceee802efedb952fdcc6b12e6f4144dc2e43c84 /addons/mail/models/mail_alias.py | |
| parent | 16c26525a72526c2d49f77c56fc30c83d6b67d38 (diff) | |
[IMP] mail: create a mixin class for models `_inherits`ing `mail.alias`
Diffstat (limited to 'addons/mail/models/mail_alias.py')
| -rw-r--r-- | addons/mail/models/mail_alias.py | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/addons/mail/models/mail_alias.py b/addons/mail/models/mail_alias.py index ba1afd02d45..72fadd87b1b 100644 --- a/addons/mail/models/mail_alias.py +++ b/addons/mail/models/mail_alias.py @@ -249,3 +249,71 @@ class Alias(models.Model): 'res_id': self.alias_parent_thread_id, 'type': 'ir.actions.act_window', } + + +class AliasMixin(models.AbstractModel): + """ A mixin for models that inherits mail.alias. This mixin initializes the + alias_id column in database, and manages the expected one-to-one + relation between your model and mail aliases. + """ + _name = 'mail.alias.mixin' + _inherits = {'mail.alias': 'alias_id'} + + alias_id = fields.Many2one('mail.alias', string='Alias', ondelete="restrict", required=True) + + def get_alias_model_name(self, vals): + """ Return the model name for the alias. Incoming emails that are not + replies to existing records will cause the creation of a new record + of this alias model. The value may depend on ``vals``, the dict of + values passed to ``create`` when a record of this model is created. + """ + return None + + def get_alias_values(self): + """ Return values to create an alias, or to write on the alias after its + creation. + """ + return {'alias_parent_thread_id': self.id} + + @api.model + def create(self, vals): + """ Create a record with ``vals``, and create a corresponding alias. """ + record = super(AliasMixin, self.with_context( + alias_model_name=self.get_alias_model_name(vals), + alias_parent_model_name=self._name, + )).create(vals) + record.alias_id.sudo().write(record.get_alias_values()) + return record + + @api.multi + def unlink(self): + """ Delete the given records, and cascade-delete their corresponding alias. """ + aliases = self.mapped('alias_id') + res = super(AliasMixin, self).unlink() + aliases.unlink() + return res + + def _init_column(self, cr, name, context=None): + """ Create aliases for existing rows. """ + super(AliasMixin, self)._init_column(cr, name, context=context) + if name != 'alias_id': + return + + alias_ctx = { + 'alias_model_name': self.get_alias_model_name({}), + 'alias_parent_model_name': self._name, + } + alias_model = self.pool['mail.alias'].browse(cr, SUPERUSER_ID, [], alias_ctx) + + child_ctx = { + 'active_test': False, # retrieve all records + 'prefetch_fields': False, # do not prefetch fields on records + } + child_model = self.browse(cr, SUPERUSER_ID, [], child_ctx) + + for record in child_model.search([('alias_id', '=', False)]): + # create the alias, and link it to the current record + alias = alias_model.create(record.get_alias_values()) + record.with_context({'mail_notrack': True}).alias_id = alias + _logger.info('Mail alias created for %s %s (id %s)', + record._name, record.display_name, record.id) |
