Launchpad automatic translations update.
[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     }
45
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)
48
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])
53
54         #Cheking for history
55         regex = action.regex_history
56         if regex:
57             res = False
58             ptrn = re.compile(ustr(regex))
59             for history in obj.message_ids:
60                 _result = ptrn.search(ustr(history.subject))
61                 if _result:
62                     res = True
63                     break
64             ok = ok and res
65
66         if action.trg_max_history:
67             res_count = False
68             history_ids = filter(lambda x: x.email_from, obj.message_ids)
69             if len(history_ids) <= action.trg_max_history:
70                 res_count = True
71             ok = ok and res_count
72         return ok
73
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)
77         write = {}
78         if hasattr(action, 'act_section_id') and action.act_section_id:
79             write['section_id'] = action.act_section_id.id
80
81         if hasattr(action, 'act_categ_id') and action.act_categ_id:
82             write['categ_ids'] = [(4, action.act_categ_id.id)]
83
84         model_obj.write(cr, uid, [obj.id], write, context)
85         return res
86
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
91
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
95
96 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: