[MERGE] base.action.rule: better handle non-ASCII data
[odoo/odoo.git] / addons / crm / crm_action_rule.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 import re
23 import tools
24
25 from tools.translate import _
26 from tools import ustr
27 from osv import fields
28 from osv import osv
29
30 import crm
31
32 class base_action_rule(osv.osv):
33     """ Base Action Rule """
34     _inherit = 'base.action.rule'
35     _description = 'Action Rules'
36
37     _columns = {
38         'trg_section_id': fields.many2one('crm.case.section', 'Sales Team'),
39         'trg_max_history': fields.integer('Maximum Communication History'),
40         'trg_categ_id':  fields.many2one('crm.case.categ', 'Category'),
41         'regex_history' : fields.char('Regular Expression on Case History', size=128),
42         'act_section_id': fields.many2one('crm.case.section', 'Set Team to'),
43         'act_categ_id': fields.many2one('crm.case.categ', 'Set Category to'),
44         'act_mail_to_partner': fields.boolean('Mail to Partner',
45                                               help="Check this if you want the rule to send an email to the partner."),
46     }
47
48     def email_send(self, cr, uid, obj, emails, body, emailfrom=tools.config.get('email_from', False), context=None):
49         mail_message = self.pool.get('mail.message')
50         body = self.format_mail(obj, body)
51         if not emailfrom:
52             if hasattr(obj, 'user_id')  and obj.user_id and obj.user_id.user_email:
53                 emailfrom = obj.user_id.user_email
54
55         name = '[%d] %s' % (obj.id, tools.ustr(obj.name))
56         emailfrom = tools.ustr(emailfrom)
57         if hasattr(obj, 'section_id') and obj.section_id and obj.section_id.reply_to:
58             reply_to = obj.section_id.reply_to
59         else:
60             reply_to = emailfrom
61         if not emailfrom:
62             raise osv.except_osv(_('Error!'), _("No E-Mail Found for your Company address!"))
63         return mail_message.schedule_with_attach(cr, uid, emailfrom, emails, name, body, model=obj._name, reply_to=reply_to, res_id=obj.id)
64
65     def do_check(self, cr, uid, action, obj, context=None):
66         ok = super(base_action_rule, self).do_check(cr, uid, action, obj, context=context)
67
68         if hasattr(obj, 'section_id'):
69             ok = ok and (not action.trg_section_id or action.trg_section_id.id == obj.section_id.id)
70         if hasattr(obj, 'categ_id'):
71             ok = ok and (not action.trg_categ_id or action.trg_categ_id.id == obj.categ_id.id)
72
73         #Cheking for history
74         regex = action.regex_history
75         if regex:
76             res = False
77             ptrn = re.compile(ustr(regex))
78             for history in obj.message_ids:
79                 _result = ptrn.search(ustr(history.subject))
80                 if _result:
81                     res = True
82                     break
83             ok = ok and res
84
85         if action.trg_max_history:
86             res_count = False
87             history_ids = filter(lambda x: x.email_from, obj.message_ids)
88             if len(history_ids) <= action.trg_max_history:
89                 res_count = True
90             ok = ok and res_count
91         return ok
92
93     def do_action(self, cr, uid, action, model_obj, obj, context=None):
94         write = {}
95         if hasattr(action, 'act_section_id') and action.act_section_id:
96             obj.section_id = action.act_section_id
97             write['section_id'] = action.act_section_id.id
98
99         if hasattr(obj, 'email_cc') and action.act_email_cc:
100             if '@' in (obj.email_cc or ''):
101                 emails = obj.email_cc.split(",")
102                 if  obj.act_email_cc not in emails:# and '<'+str(action.act_email_cc)+">" not in emails:
103                     write['email_cc'] = obj.email_cc + ',' + obj.act_email_cc
104             else:
105                 write['email_cc'] = obj.act_email_cc
106
107         # Put state change by rule in communication history
108         if hasattr(obj, 'state') and hasattr(obj, 'message_append') and action.act_state:
109             model_obj.message_append(cr, uid, [obj], _(action.act_state))
110
111         model_obj.write(cr, uid, [obj.id], write, context)
112         super(base_action_rule, self).do_action(cr, uid, action, model_obj, obj, context=context)
113         emails = []
114
115         if hasattr(obj, 'email_from') and action.act_mail_to_partner:
116             emails.append(obj.email_from)
117         emails = filter(None, emails)
118         if len(emails) and action.act_mail_body:
119             emails = list(set(emails))
120             self.email_send(cr, uid, obj, emails, action.act_mail_body)
121         return True
122
123
124     def state_get(self, cr, uid, context=None):
125         """Gets available states for crm"""
126         res = super(base_action_rule, self).state_get(cr, uid, context=context)
127         return res + crm.AVAILABLE_STATES
128
129     def priority_get(self, cr, uid, context=None):
130         res = super(base_action_rule, self).priority_get(cr, uid, context=context)
131         return res + crm.AVAILABLE_PRIORITIES
132
133 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: