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 ##############################################################################
30 from tools.translate import _
33 from osv import fields,osv,orm
34 from osv.orm import except_orm
36 from tools import command_re
38 class crm_cases(osv.osv):
44 def message_new(self, cr, uid, msg, context):
46 Automatically calls when new email message arrives
48 @param self: The object pointer
49 @param cr: the current row, from the database cursor,
50 @param uid: the current user’s ID for security checks
53 mailgate_pool = self.pool.get('email.server.tools')
55 subject = msg.get('subject')
56 body = msg.get('body')
57 msg_from = msg.get('from')
58 priority = msg.get('priority')
62 'email_from': msg_from,
63 'email_cc': msg.get('cc'),
67 if msg.get('priority', False):
68 vals['priority'] = priority
70 res = mailgate_pool.get_partner(cr, uid, msg.get('from'))
73 res = self.create(cr, uid, vals, context)
74 cases = self.browse(cr, uid, [res])
75 self._history(cr, uid, cases, _('Receive'), history=True, details=body, email_from=msg_from, message_id=msg.get('id'))
77 attachents = msg.get('attachments', [])
78 for attactment in attachents or []:
81 'datas':binascii.b2a_base64(str(attachents.get(attactment))),
82 'datas_fname': attactment,
83 'description': 'Mail attachment',
84 'res_model': self._name,
87 self.pool.get('ir.attachment').create(cr, uid, data_attach)
91 def message_update(self, cr, uid, ids, vals={}, msg="", default_act='pending', context={}):
93 @param self: The object pointer
94 @param cr: the current row, from the database cursor,
95 @param uid: the current user’s ID for security checks,
96 @param ids: List of update mail’s IDs
99 if isinstance(ids, (str, int, long)):
102 msg_from = msg['from']
104 'description': msg['body']
106 if msg.get('priority', False):
107 vals['priority'] = msg.get('priority')
110 'cost':'planned_cost',
111 'revenue': 'planned_revenue',
112 'probability':'probability'
115 for line in msg['body'].split('\n'):
117 res = command_re.match(line)
118 if res and maps.get(res.group(1).lower(), False):
119 key = maps.get(res.group(1).lower())
120 vls[key] = res.group(2).lower()
123 res = self.write(cr, uid, ids, vals)
124 cases = self.browse(cr, uid, ids)
125 message_id = context.get('references_id', False)
126 self._history(cr, uid, cases, _('Receive'), history=True, details=msg['body'], email_from=msg_from, message_id=message_id)
127 #getattr(self, act)(cr, uid, select)
130 def emails_get(self, cr, uid, ids, context={}):
134 @param self: The object pointer
135 @param cr: the current row, from the database cursor,
136 @param uid: the current user’s ID for security checks,
137 @param ids: List of email’s IDs
138 @param context: A standard dictionary for contextual values
141 if isinstance(ids, (str, int, long)):
145 for case in self.browse(cr, uid, select):
146 user_email = (case.user_id and case.user_id.address_id and case.user_id.address_id.email) or False
147 res += [(user_email, case.email_from, case.email_cc or False, getattr(case,'priority') and case.priority or False)]
148 if isinstance(ids, (str, int, long)):
149 return len(res) and res[0] or False
152 def msg_send(self, cr, uid, id, *args, **argv):
155 @param self: The object pointer
156 @param cr: the current row, from the database cursor,
157 @param uid: the current user’s ID for security checks,
158 @param ids: List of email’s IDs
159 @param *args: Return Tuple Value
160 @param **args: Return Dictionary of Keyword Value