[IMP] : Added context=None on methods used for _constraints and replaced context...
[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 time
23 import re
24 import os
25 import base64
26 import tools
27
28 from tools.translate import _
29 from osv import fields
30 from osv import osv
31 from osv import orm
32 from osv.orm import except_orm
33
34 import crm
35
36 class base_action_rule(osv.osv):
37     """ Base Action Rule """
38     _inherit = 'base.action.rule'
39     _description = 'Action Rules'
40     
41     _columns = {
42         'trg_section_id': fields.many2one('crm.case.section', 'Sales Team'), 
43         'trg_max_history': fields.integer('Maximum Communication History'), 
44         'trg_categ_id':  fields.many2one('crm.case.categ', 'Category'), 
45         'regex_history' : fields.char('Regular Expression on Case History', size=128), 
46         'act_section_id': fields.many2one('crm.case.section', 'Set Team to'), 
47         'act_categ_id': fields.many2one('crm.case.categ', 'Set Category to'), 
48         'act_mail_to_partner': fields.boolean('Mail to Partner', help="Check \
49 this if you want the rule to send an email to the partner."), 
50     }
51     
52
53     def email_send(self, cr, uid, obj, emails, body, emailfrom=tools.config.get('email_from', False), context=None):
54         if not context:
55             context = {}
56         body = self.format_mail(obj, body)
57         if not emailfrom:
58             if hasattr(obj, 'user_id')  and obj.user_id and obj.user_id.address_id and obj.user_id.address_id.email:
59                 emailfrom = obj.user_id.address_id.email
60             
61         name = '[%d] %s' % (obj.id, tools.ustr(obj.name))
62         emailfrom = tools.ustr(emailfrom)
63         if hasattr(obj, 'section_id') and obj.section_id and obj.section_id.reply_to:
64             reply_to = obj.section_id.reply_to
65         else:
66             reply_to = emailfrom
67         if not emailfrom:
68             raise osv.except_osv(_('Error!'), 
69                     _("No E-Mail ID Found for your Company address!"))
70         return tools.email_send(emailfrom, emails, name, body, reply_to=reply_to, openobject_id=str(obj.id))
71     
72     def do_check(self, cr, uid, action, obj, context=None):
73         """ @param self: The object pointer
74         @param cr: the current row, from the database cursor,
75         @param uid: the current user’s ID for security checks,
76         @param context: A standard dictionary for contextual values"""
77         if not context:
78             context = {}
79         ok = super(base_action_rule, self).do_check(cr, uid, action, obj, context=context)
80
81         if hasattr(obj, 'section_id'):
82             ok = ok and (not action.trg_section_id or action.trg_section_id.id==obj.section_id.id)
83         if hasattr(obj, 'categ_id'):
84             ok = ok and (not action.trg_categ_id or action.trg_categ_id.id==obj.categ_id.id)
85
86         #Cheking for history 
87         regex = action.regex_history
88         result_history = True
89         if regex:
90             res = False
91             ptrn = re.compile(str(regex))
92             for history in obj.message_ids:
93                 _result = ptrn.search(str(history.name))
94                 if _result:
95                     res = True
96                     break
97             result_history = res
98         ok = ok and (not regex or result_history)
99
100         res_count = True
101         if action.trg_max_history:
102             res_count = False
103             history_ids = filter(lambda x: x.history, obj.message_ids)
104             if len(history_ids) <= action.trg_max_history:
105                 res_count = True
106         ok = ok and res_count
107         return ok
108
109     def do_action(self, cr, uid, action, model_obj, obj, context=None):
110         """ @param self: The object pointer
111         @param cr: the current row, from the database cursor,
112         @param uid: the current user’s ID for security checks,
113         @param context: A standard dictionary for contextual values """
114         if not context:
115             context = {}
116         res = super(base_action_rule, self).do_action(cr, uid, action, model_obj, obj, context=context)
117         write = {}
118         
119         if hasattr(action, 'act_section_id') and action.act_section_id:
120             obj.section_id = action.act_section_id
121             write['section_id'] = action.act_section_id.id
122
123         if hasattr(obj, 'email_cc') and action.act_email_cc:
124             if '@' in (obj.email_cc or ''):
125                 emails = obj.email_cc.split(",")
126                 if  obj.act_email_cc not in emails:# and '<'+str(action.act_email_cc)+">" not in emails:
127                     write['email_cc'] = obj.email_cc+','+obj.act_email_cc
128             else:
129                 write['email_cc'] = obj.act_email_cc
130
131         # Put state change by rule in communication history
132         if hasattr(obj, 'state') and action.act_state:
133             model_obj._history(cr, uid, [obj], _(action.act_state))
134
135         model_obj.write(cr, uid, [obj.id], write, context)
136         emails = []
137
138         if hasattr(obj, 'email_from') and action.act_mail_to_partner:
139             emails.append(obj.email_from)
140         emails = filter(None, emails)
141         if len(emails) and action.act_mail_body:
142             emails = list(set(emails))
143             self.email_send(cr, uid, obj, emails, action.act_mail_body)
144         return True
145
146
147     def state_get(self, cr, uid, context=None):
148         """Gets available states for crm
149         @param self: The object pointer
150         @param cr: the current row, from the database cursor,
151         @param uid: the current user’s ID for security checks,
152         @param context: A standard dictionary for contextual values """
153         if not context:
154             context = {}
155         res = super(base_action_rule, self).state_get(cr, uid, context=context)
156         return res  + crm.AVAILABLE_STATES
157
158     def priority_get(self, cr, uid, context=None):
159         """@param self: The object pointer
160         @param cr: the current row, from the database cursor,
161         @param uid: the current user’s ID for security checks,
162         @param context: A standard dictionary for contextual values """
163         if not context:
164             context = {}
165         res = super(base_action_rule, self).priority_get(cr, uid, context=context)
166         return res + crm.AVAILABLE_PRIORITIES
167
168 base_action_rule()
169
170 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: