[MERGE] Forward-port of 7.0 bugfixes up to rev 9976 rev-id: odo@openerp.com-201404111...
[odoo/odoo.git] / addons / plugin / plugin_handler.py
1 '''
2 Created on 18 oct. 2011
3
4 @author: openerp
5 '''
6
7 from openerp.osv import osv
8 from openerp.tools.translate import _
9
10 class plugin_handler(osv.osv_memory):
11     _name = 'plugin.handler'
12
13     def _make_url(self, cr, uid, res_id, model, context=None):
14         """
15             @param res_id: on which document the message is pushed
16             @param model: name of the document linked with the mail
17             @return url
18         """
19         base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url', default='http://localhost:8069', context=context)
20         if base_url:
21             user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
22             base_url += '/login?db=%s&login=%s&key=%s#id=%s&model=%s' % (cr.dbname, user.login, user.password, res_id, model)
23         return base_url
24
25     def is_installed(self, cr, uid):
26         return True
27
28     def partner_get(self, cr, uid, address_email):
29         partner_obj = self.pool.get('res.partner')
30         partner_ids = partner_obj.search(cr, uid, [('email', 'like', address_email)])
31         res_id = partner_ids and partner_ids[0] or 0
32         url = self._make_url(cr, uid, res_id, 'res.partner')
33         return ('res.partner', res_id, url)
34
35     def document_get(self, cr, uid, email):
36         """
37             @param email: email is a standard RFC2822 email message
38             @return Dictionary which contain id and the model name of the document linked with the mail
39                 if no document is found the id = 0
40                 (model_name, res_id, url, name_get)
41         """
42         mail_message_obj = self.pool.get('mail.message')
43         model = ""
44         res_id = 0
45         url = ""
46         name = ""
47         msg = self.pool.get('mail.thread').message_parse(cr, uid, email)
48         parent_id = msg.get('parent_id', False)
49         message_id = msg.get('message_id')
50         msg_id = False
51         if message_id:
52             msg_ids = mail_message_obj.search(cr, uid, [('message_id', '=', message_id)])
53             msg_id = len(msg_ids) and msg_ids[0] or False
54         if not msg_id and parent_id:
55             msg_id = parent_id
56         if msg_id:
57             msg = mail_message_obj.browse(cr, uid, msg_id)
58             res_id = msg.res_id
59             model = msg.model
60             url = self._make_url(cr, uid, res_id, model)
61             name =  self.pool[model].name_get(cr, uid, [res_id])[0][1]
62         return (model, res_id, url, name)
63
64     def document_type(self, cr, uid, context=None):
65         """
66             Return the list of available model to push
67             res.partner is a special case
68             otherwise all model that inherit from mail.thread
69             ['res.partner', 'project.issue']
70         """
71         mail_thread_obj = self.pool.get('mail.thread')
72         doc_dict = mail_thread_obj.message_capable_models(cr, uid, context)
73         doc_dict['res.partner'] = "Partner"
74         return doc_dict.items()
75
76     # Can be used where search record was used
77     def list_document_get(self, cr, uid, model, name):
78         """
79             This function return the result of name_search on the object model
80             @param model: the name of the model
81             @param : the name of the document
82             @return : the result of name_search a list of tuple
83             [(id, 'name')]
84         """
85         return self.pool[model].name_search(cr, uid, name)
86
87     def push_message(self, cr, uid, model, email, res_id=0):
88         """
89             @param email: email is a standard RFC2822 email message
90             @param model: On which model the message is pushed
91             @param thread_id: on which document the message is pushed, if thread_id = 0 a new document is created
92             @return Dictionary which contain model , url and resource id.
93         """
94         mail_message = self.pool.get('mail.message')
95         model_obj = self.pool[model]
96         mail_thread_obj = self.pool.get('mail.thread')
97         msg = mail_thread_obj.message_parse(cr, uid, email)
98         message_id = msg.get('message_id')
99         mail_ids = mail_message.search(cr, uid, [('message_id', '=', message_id), ('res_id', '=', res_id), ('model', '=', model)])
100         if message_id and mail_ids:
101             mail_record = mail_message.browse(cr, uid, mail_ids)[0]
102             res_id = mail_record.res_id
103             notify = _("Email already pushed")
104         elif res_id == 0:
105             if model == 'res.partner':
106                 notify = _('Use the Partner button to create a new partner')
107             else:
108                 res_id = model_obj.message_process(cr, uid, model, email)
109                 notify = _("Mail successfully pushed, a new %s has been created.") % model
110         else:
111             email_from = msg.get('email_from')
112             if not email_from:
113                 author_id = False
114             else:
115                 authors = mail_thread_obj.message_find_partner_from_emails(cr, uid, [res_id], [email_from])
116                 author_id = authors and authors[0].get('partner_id') or False
117
118             model_obj.message_post(cr, uid, [res_id],
119                             body=msg.get('body'),
120                             subject=msg.get('subject'),
121                             type='comment' if model == 'res.partner' else 'email',
122                             parent_id=msg.get('parent_id'),
123                             attachments=msg.get('attachments'),
124                             message_id=message_id,
125                             email_from=email_from,
126                             author_id=author_id)
127             notify = _("Mail successfully pushed")
128         url = self._make_url(cr, uid, res_id, model)
129         return (model, res_id, url, notify)
130
131     def contact_create(self, cr, uid, data, partner_id, context=None):
132         """
133             @param data : the data use to create the res.partner
134                 [('field_name', value)], field name is required
135             @param partner_id : On which partner the address is attached
136              if partner_id = 0 then create a new partner with the same name that the address
137             @return : the partner_id sended or created, this allow the plugin to open the right partner page
138         """
139         partner_obj = self.pool.get('res.partner')
140         dictcreate = dict(data)
141         if partner_id:
142             is_company = partner_obj.browse(cr, uid, partner_id, context=context).is_company
143             if is_company:
144                 dictcreate['parent_id'] = partner_id
145         partner_id = partner_obj.create(cr, uid, dictcreate)
146         url = self._make_url(cr, uid, partner_id, 'res.partner')
147         return ('res.partner', partner_id, url)
148
149     # Specific to outlook rfc822 is not available so we split in arguments headerd,body,attachemnts
150     def push_message_outlook(self, cr, uid, model, headers, res_id=0, body=False, body_html=False, attachments=False):
151         # ----------------------------------------
152         # solution 1
153         # construct a fake rfc822 from the separated arguement
154         #m = email.asdfsadf
155         # use the push_message method
156         #self.push_message(m)
157         # ----------------------------------------
158         # solution 2
159         # use self.pushmessage only with header and body
160         # add attachemnt yourself after
161         mail_message = self.pool.get('mail.message')
162         ir_attachment_obj = self.pool.get('ir.attachment')
163         attach_ids = []
164         msg = self.pool.get('mail.thread').message_parse(cr, uid, headers)
165         message_id = msg.get('message_id')
166         push_mail = self.push_message(cr, uid, model, headers, res_id)
167         res_id = push_mail[1]
168         model = push_mail[0]
169         notify = push_mail[3]
170         for name in attachments.keys():
171             attachment_ids = ir_attachment_obj.search(cr, uid, [('res_model', '=', model), ('res_id', '=', res_id), ('datas_fname', '=', name)])
172             if attachment_ids:
173                 attach_ids.append(attachment_ids[0])
174             else:
175                 vals = {"res_model": model, "res_id": res_id, "name": name, "datas": attachments[name], "datas_fname": name}
176                 attach_ids.append(ir_attachment_obj.create(cr, uid, vals))
177         mail_ids = mail_message.search(cr, uid, [('message_id', '=', message_id), ('res_id', '=', res_id), ('model', '=', model)])
178         if mail_ids:
179             mail_message.write(cr, uid, mail_ids[0], {'attachment_ids': [(6, 0, attach_ids)], 'body': body_html})
180         url = self._make_url(cr, uid, res_id, model)
181         return (model, res_id, url, notify)