1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
25 from tools.translate import _
26 from tools import ustr
27 from osv import fields
32 class base_action_rule(osv.osv):
33 """ Base Action Rule """
34 _inherit = 'base.action.rule'
35 _description = 'Action Rules'
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'),
46 def do_check(self, cr, uid, action, obj, context=None):
47 ok = super(base_action_rule, self).do_check(cr, uid, action, obj, context=context)
49 if hasattr(obj, 'section_id'):
50 ok = ok and (not action.trg_section_id or action.trg_section_id.id == obj.section_id.id)
51 if hasattr(obj, 'categ_ids'):
52 ok = ok and (not action.trg_categ_id or action.trg_categ_id.id in [x.id for x in obj.categ_ids])
55 regex = action.regex_history
58 ptrn = re.compile(ustr(regex))
59 for history in obj.message_ids:
60 _result = ptrn.search(ustr(history.subject))
66 if action.trg_max_history:
68 history_ids = filter(lambda x: x.email_from, obj.message_ids)
69 if len(history_ids) <= action.trg_max_history:
74 def do_action(self, cr, uid, action, obj, context=None):
75 res = super(base_action_rule, self).do_action(cr, uid, action, obj, context=context)
76 model_obj = self.pool.get(action.model_id.model)
78 if hasattr(action, 'act_section_id') and action.act_section_id:
79 write['section_id'] = action.act_section_id.id
81 if hasattr(action, 'act_categ_id') and action.act_categ_id:
82 write['categ_ids'] = [(4, action.act_categ_id.id)]
84 model_obj.write(cr, uid, [obj.id], write, context)
87 def state_get(self, cr, uid, context=None):
88 """Gets available states for crm"""
89 res = super(base_action_rule, self).state_get(cr, uid, context=context)
90 return res + crm.AVAILABLE_STATES
92 def priority_get(self, cr, uid, context=None):
93 res = super(base_action_rule, self).priority_get(cr, uid, context=context)
94 return res + crm.AVAILABLE_PRIORITIES
96 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: