From bc4964de13f712188b7082148ea7630ed2b92e58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Thibault=20Delavall=C3=A9e?= Date: Thu, 16 May 2013 17:09:00 +0200 Subject: [PATCH] [IMP] mail: now defines a catchall email alias, used to format the reply_to of notification emails when having a document without alias, or for private discussions using external mail readers. bzr revid: tde@openerp.com-20130516150900-khb1d1ygyuos1wrl --- addons/mail/mail_mail.py | 36 ++++++---- addons/mail/mail_message.py | 2 +- addons/mail/res_config.py | 18 ++++- addons/mail/res_config_view.xml | 4 ++ addons/mail/tests/test_mail_gateway.py | 99 ++++++++++++++++++++++++++ 5 files changed, 144 insertions(+), 15 deletions(-) diff --git a/addons/mail/mail_mail.py b/addons/mail/mail_mail.py index 3c30a950230..89392964ee5 100644 --- a/addons/mail/mail_mail.py +++ b/addons/mail/mail_mail.py @@ -93,31 +93,41 @@ class mail_mail(osv.Model): """ Return a specific reply_to: alias of the document through message_get_reply_to or take the email_from """ + # if value specified: directly return it if values.get('reply_to'): return values.get('reply_to') - email_reply_to = False - # model, res_id: comes from values OR related message - model = values.get('model') - res_id = values.get('res_id') - if values.get('mail_message_id') and (not model or not res_id): + mailgateway = True # tells whether the answer will go through the mailgateway, leading to the formatting of reply_to + ir_config_parameter = self.pool.get("ir.config_parameter") + catchall_domain = ir_config_parameter.get_param(cr, uid, "mail.catchall.domain", context=context) + + # model, res_id, email_from, reply_to: comes from values OR related message + message = None + if values.get('mail_message_id'): message = self.pool.get('mail.message').browse(cr, uid, values.get('mail_message_id'), context=context) - if not model: - model = message.model - if not res_id: - res_id = message.res_id + model = values.get('model', message and message.model or False) + res_id = values.get('res_id', message and message.res_id or False) + email_from = values.get('email_from', message and message.email_from or False) + email_reply_to = message and message.reply_to or False # if model and res_id: try to use ``message_get_reply_to`` that returns the document alias - if model and res_id and hasattr(self.pool[model], 'message_get_reply_to'): + if not email_reply_to and model and res_id and hasattr(self.pool[model], 'message_get_reply_to'): email_reply_to = self.pool[model].message_get_reply_to(cr, uid, [res_id], context=context)[0] + # no alias reply_to -> catchall alias + if not email_reply_to: + catchall_alias = ir_config_parameter.get_param(cr, uid, "mail.catchall.alias", context=context) + if catchall_domain and catchall_alias: + email_reply_to = '%s@%s' % (catchall_alias, catchall_domain) # no alias reply_to -> reply_to will be the email_from, only the email part - if not email_reply_to and values.get('email_from'): - emails = tools.email_split(values.get('email_from')) + if not email_reply_to and email_from: + emails = tools.email_split(email_from) if emails: email_reply_to = emails[0] + if emails[0].split('@')[1] != catchall_domain: + mailgateway = False # format 'Document name ' - if email_reply_to and model and res_id: + if email_reply_to and model and res_id and mailgateway: document_name = self.pool[model].name_get(cr, SUPERUSER_ID, [res_id], context=context)[0] if document_name: # sanitize document name diff --git a/addons/mail/mail_message.py b/addons/mail/mail_message.py index defc12a2f73..2febf96cd6d 100644 --- a/addons/mail/mail_message.py +++ b/addons/mail/mail_message.py @@ -743,7 +743,7 @@ class mail_message(osv.Model): default_starred = context.pop('default_starred', False) # generate message_id, to redirect answers to the right discussion thread if not values.get('message_id') and values.get('reply_to'): - values['message_id'] = tools.generate_tracking_message_id('reply_to-%(model)s' % values) + values['message_id'] = tools.generate_tracking_message_id('reply_to') elif not values.get('message_id') and values.get('res_id') and values.get('model'): values['message_id'] = tools.generate_tracking_message_id('%(res_id)s-%(model)s' % values) elif not values.get('message_id'): diff --git a/addons/mail/res_config.py b/addons/mail/res_config.py index 91606e1c720..b0aef50e76e 100644 --- a/addons/mail/res_config.py +++ b/addons/mail/res_config.py @@ -23,13 +23,18 @@ import urlparse from openerp.osv import osv, fields + class project_configuration(osv.TransientModel): _inherit = 'base.config.settings' _columns = { - 'alias_domain' : fields.char('Alias Domain', + 'alias_domain': fields.char('Alias Domain', help="If you have setup a catch-all email domain redirected to " "the OpenERP server, enter the domain name here."), + 'alias_catchall': fields.char('Catchall Email Alias', + help="Define the default email that will handle replies to document " + "not having their specific email alias defined. Only the left " + "part is required.") } def get_default_alias_domain(self, cr, uid, ids, context=None): @@ -46,3 +51,14 @@ class project_configuration(osv.TransientModel): config_parameters = self.pool.get("ir.config_parameter") for record in self.browse(cr, uid, ids, context=context): config_parameters.set_param(cr, uid, "mail.catchall.domain", record.alias_domain or '', context=context) + + def get_default_alias_catchall(self, cr, uid, ids, context=None): + alias_catchall = self.pool.get("ir.config_parameter").get_param(cr, uid, "mail.catchall.alias", context=context) + if not alias_catchall: + return {} + return {'alias_catchall': alias_catchall} + + def set_alias_catchall(self, cr, uid, ids, context=None): + config_parameters = self.pool.get("ir.config_parameter") + for record in self.browse(cr, uid, ids, context=context): + config_parameters.set_param(cr, uid, "mail.catchall.alias", record.alias_catchall or '', context=context) diff --git a/addons/mail/res_config_view.xml b/addons/mail/res_config_view.xml index d3797ba2dcc..6cbcf569067 100644 --- a/addons/mail/res_config_view.xml +++ b/addons/mail/res_config_view.xml @@ -11,6 +11,10 @@