From 20c4bcf9fc978ee4a9ba81d7e1e471f4ccb4e6e1 Mon Sep 17 00:00:00 2001 From: Olivier Dony Date: Tue, 4 Sep 2012 17:53:52 +0200 Subject: [PATCH] [REVERT] {crm,base}_action_rule: revert previous attempt to fix deprecated schedule_with_attach bzr revid: odo@openerp.com-20120904155352-shna0b9042wfs0gt --- addons/base_action_rule/base_action_rule.py | 72 +++++++++++++++------ addons/crm/crm_action_rule.py | 31 +++++++-- 2 files changed, 78 insertions(+), 25 deletions(-) diff --git a/addons/base_action_rule/base_action_rule.py b/addons/base_action_rule/base_action_rule.py index 9f658b9294c..8c115baed23 100644 --- a/addons/base_action_rule/base_action_rule.py +++ b/addons/base_action_rule/base_action_rule.py @@ -301,10 +301,35 @@ the rule to mark CC(mail to any other person defined in actions)."), } return self.format_body(body % data) - def email_send(self, cr, uid, obj, partner_ids, body, context=None): + def email_send(self, cr, uid, obj, emails, body, emailfrom=None, context=None): + """ send email + @param self: The object pointer + @param cr: the current row, from the database cursor, + @param uid: the current user’s ID for security checks, + @param email: pass the emails + @param emailfrom: Pass name the email From else False + @param context: A standard dictionary for contextual values """ + + if not emailfrom: + emailfrom = tools.config.get('email_from', False) + + if context is None: + context = {} + + mail_message = self.pool.get('mail.message') body = self.format_mail(obj, body) - subject = '[%d] %s' % (obj.id, tools.ustr(obj.name)) - return obj.message_post(cr, uid, obj.id, subject=name, body=body, partner_ids=partner_ids, context=context) + if not emailfrom: + if hasattr(obj, 'user_id') and obj.user_id and obj.user_id.email: + emailfrom = obj.user_id.email + + name = '[%d] %s' % (obj.id, tools.ustr(obj.name)) + emailfrom = tools.ustr(emailfrom) + reply_to = emailfrom + if not emailfrom: + raise osv.except_osv(_('Error!'), + _("No email ID found for your company address.")) + return mail_message.schedule_with_attach(cr, uid, emailfrom, emails, name, body, model='base.action.rule', reply_to=reply_to, res_id=obj.id) + def do_check(self, cr, uid, action, obj, context=None): """ check Action @@ -391,23 +416,32 @@ the rule to mark CC(mail to any other person defined in actions)."), if action.act_method: getattr(model_obj, 'act_method')(cr, uid, [obj.id], action, context) - # Email: find partner_ids - res_partner_obj = self.pool.get('res.partner') - partner_ids = [] - if action.act_mail_to_user and obj._columns.get('user_id') and obj.user_id: - partner_ids.append(obj.user_id.partner_id.id) - if action.act_mail_to_watchers and action.act_email_cc: - partner_ids += [res_partner_obj.find_or_create(cr, uid, mail, context=context) - for mail in action.act_email_cc.split(',') if mail] - if action.act_mail_to_email and action.act_mail_to_email: - partner_ids += [res_partner_obj.find_or_create(cr, uid, mail, context=context) - for mail in action.act_mail_to_email.split(',') if mail] + emails = [] + if hasattr(obj, 'user_id') and action.act_mail_to_user: + if obj.user_id: + emails.append(obj.user_id.email) + + if action.act_mail_to_watchers: + emails += (action.act_email_cc or '').split(',') + if action.act_mail_to_email: + emails += (action.act_mail_to_email or '').split(',') + + locals_for_emails = { + 'user' : self.pool.get('res.users').browse(cr, uid, uid, context=context), + 'obj' : obj, + } + if action.act_email_to: - emails = safe_eval(action.act_email_to, {}, locals_for_emails) - partner_ids += [res_partner_obj.find_or_create(cr, uid, mail, context=context) - for mail in emails if mail] - if partner_ids: - self.email_send(cr, uid, obj, partner_ids, action.act_mail_body, context=context) + emails.append(safe_eval(action.act_email_to, {}, locals_for_emails)) + + emails = filter(None, emails) + if len(emails) and action.act_mail_body: + emails = list(set(emails)) + email_from = safe_eval(action.act_email_from, {}, locals_for_emails) + emails = tools.email_split(','.join(filter(None, emails))) + email_froms = tools.email_split(email_from) + if email_froms: + self.email_send(cr, uid, obj, emails, action.act_mail_body, emailfrom=email_froms[0]) return True def _action(self, cr, uid, ids, objects, scrit=None, context=None): diff --git a/addons/crm/crm_action_rule.py b/addons/crm/crm_action_rule.py index ca6e02d99b5..36700b5b6c2 100644 --- a/addons/crm/crm_action_rule.py +++ b/addons/crm/crm_action_rule.py @@ -45,6 +45,23 @@ class base_action_rule(osv.osv): help="Check this if you want the rule to send an email to the partner."), } + def email_send(self, cr, uid, obj, emails, body, emailfrom=tools.config.get('email_from', False), context=None): + mail_message = self.pool.get('mail.message') + body = self.format_mail(obj, body) + if not emailfrom: + if hasattr(obj, 'user_id') and obj.user_id and obj.user_id.email: + emailfrom = obj.user_id.email + + name = '[%d] %s' % (obj.id, tools.ustr(obj.name)) + emailfrom = tools.ustr(emailfrom) + if hasattr(obj, 'section_id') and obj.section_id and obj.section_id.alias_id: + reply_to = obj.section_id.alias_id.name_get()[0][1] + else: + reply_to = emailfrom + if not emailfrom: + raise osv.except_osv(_('Error!'), _("There is no email for your company address.")) + return mail_message.schedule_with_attach(cr, uid, emailfrom, emails, name, body, model=obj._name, reply_to=reply_to, res_id=obj.id) + def do_check(self, cr, uid, action, obj, context=None): ok = super(base_action_rule, self).do_check(cr, uid, action, obj, context=context) @@ -93,15 +110,17 @@ class base_action_rule(osv.osv): model_obj.write(cr, uid, [obj.id], write, context) super(base_action_rule, self).do_action(cr, uid, action, model_obj, obj, context=context) + emails = [] - # Post a message if act_mail_to_partner and act_mail_body - if not action.act_mail_body or not action.act_mail_to_partner: - return True - if obj._columns.get('email_from') and obj.email_from: - partner_ids = [self.pool.get('res.partner').find_or_create(cr, uid, obj.email_from, context=context)] - self.email_send(cr, uid, obj, partner_ids, action.act_mail_body, context=context) + if hasattr(obj, 'email_from') and action.act_mail_to_partner: + emails.append(obj.email_from) + emails = filter(None, emails) + if len(emails) and action.act_mail_body: + emails = list(set(emails)) + self.email_send(cr, uid, obj, emails, action.act_mail_body) return True + def state_get(self, cr, uid, context=None): """Gets available states for crm""" res = super(base_action_rule, self).state_get(cr, uid, context=context)