2 Created on 18 oct. 2011
7 from osv import osv, fields
9 class plugin_handler(osv.osv_memory):
10 _name = 'plugin.handler'
12 def _make_url(self, cr, uid, res_id, model, context=None):
14 @param res_id: on which document the message is pushed
15 @param model: name of the document linked with the mail
18 base_url = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url', default='http://localhost:8069', context=context)
20 user = self.pool.get('res.users').browse(cr, uid, uid, context=context)
21 base_url += '/login?db=%s&login=%s&key=%s#id=%s&model=%s' % (cr.dbname, user.login, user.password, res_id, model)
24 def is_installed(self, cr, uid):
27 def partner_get(self, cr, uid, address_email):
28 partner_obj = self.pool.get('res.partner')
29 partner_ids = partner_obj.search(cr, uid, [('email', 'like', address_email)])
30 res_id = partner_ids and partner_ids[0] or 0
31 url = self._make_url(cr, uid, res_id, 'res.partner')
32 return ('res.partner', res_id , url)
34 def document_get(self, cr, uid, email):
36 @param email: email is a standard RFC2822 email message
37 @return Dictionary which contain id and the model name of the document linked with the mail
38 if no document is found the id = 0
39 (model_name, res_id, url, name_get)
41 mail_message_obj = self.pool.get('mail.message')
46 msg = self.pool.get('mail.thread').message_parse(cr, uid, email)
47 parent_id = msg.get('parent_id', False)
48 message_id = msg.get('message_id')
51 msg_ids = mail_message_obj.search(cr, uid, [('message_id','=', message_id)])
52 msg_id = len(msg_ids) and msg_ids[0] or False
53 if not msg_id and parent_id:
56 msg = mail_message_obj.browse(cr, uid, msg_id)
59 url = self._make_url(cr, uid, res_id, model)
60 name = self.pool.get(model).name_get(cr, uid, [res_id])[0][1]
61 return (model,res_id, url,name)
63 def document_type(self, cr, uid, context=None):
65 Return the list of available model to push
66 res.partner is a special case
67 otherwise all model that inherit from mail.thread
68 ['res.partner', 'project.issue']
70 mail_thread_obj = self.pool.get('mail.thread')
71 doc_dict = mail_thread_obj.message_capable_models(cr, uid, context)
72 doc_dict['res.partner'] = "Partner"
73 return doc_dict.items()
75 # Can be used where search record was used
76 def list_document_get(self, cr, uid, model, name):
78 This function return the result of name_search on the object model
79 @param model: the name of the model
80 @param : the name of the document
81 @return : the result of name_search a list of tuple
84 return self.pool.get(model).name_search(cr, uid, name)
86 def push_message(self, cr, uid, model, email, res_id=0):
88 @param email: email is a standard RFC2822 email message
89 @param model: On which model the message is pushed
90 @param thread_id: on which document the message is pushed, if thread_id = 0 a new document is created
91 @return Dictionary which contain model , url and resource id.
93 mail_message = self.pool.get('mail.message')
94 model_obj = self.pool.get(model)
95 msg = self.pool.get('mail.thread').message_parse(cr, uid, email)
96 message_id = msg.get('message-id')
97 mail_ids = mail_message.search(cr, uid, [('message_id','=',message_id),('res_id','=',res_id),('model','=',model)])
99 if message_id and mail_ids :
100 mail_record = mail_message.browse(cr, uid, mail_ids)[0]
101 res_id = mail_record.res_id
102 notify = "Email already pushed"
104 if model == 'res.partner':
105 notify = 'User the Partner button to create a new partner'
107 res_id = model_obj.message_process(cr, uid, model, email)
108 notify = "Mail successfully pushed, a new %s has been created " % model
110 model_obj.message_post(cr, uid, [res_id],
111 body= msg.get('body'),
112 subject= msg.get('subject'),
114 parent_id= msg.get('parent_id'),
115 attachments= msg.get('attachments'))
116 notify = "Mail successfully pushed"
117 url = self._make_url(cr, uid, res_id, model)
118 return (model, res_id, url, notify)
120 def contact_create(self, cr, uid, data, partner_id, context=None):
122 @param data : the data use to create the res.partner
123 [('field_name', value)], field name is required
124 @param partner_id : On which partner the address is attached
125 if partner_id = 0 then create a new partner with the same name that the address
126 @return : the partner_id sended or created, this allow the plugin to open the right partner page
128 partner_obj = self.pool.get('res.partner')
129 dictcreate = dict(data)
131 is_company = partner_obj.browse(cr, uid, partner_id, context=context).is_company
133 dictcreate['parent_id'] = partner_id
134 partner_id = partner_obj.create(cr, uid, dictcreate)
135 url = self._make_url(cr, uid, partner_id, 'res.partner')
136 return ('res.partner', partner_id, url)
138 # Specific to outlook rfc822 is not available so we split in arguments headerd,body,attachemnts
139 def push_message_outlook(self, cr, uid, model, headers, res_id=0, body=False, body_html=False, attachments=False):
140 # ----------------------------------------
142 # construct a fake rfc822 from the separated arguement
144 # use the push_message method
145 #self.push_message(m)
146 # ----------------------------------------
148 # use self.pushmessage only with header and body
149 # add attachemnt yourself after
150 mail_message = self.pool.get('mail.message')
151 ir_attachment_obj = self.pool.get('ir.attachment')
153 msg = self.pool.get('mail.thread').message_parse(cr, uid, headers)
154 message_id = msg.get('message-id')
155 push_mail = self.push_message(cr, uid, model, headers, res_id)
156 res_id = push_mail[1]
158 for name in attachments.keys():
159 attachment_ids = ir_attachment_obj.search(cr, uid, [('res_model', '=', model), ('res_id', '=', res_id), ('datas_fname', '=', name)])
161 attach_ids.append( attachment_ids[0])
163 vals = {"res_model": model, "res_id": res_id, "name": name, "datas" :attachments[name], "datas_fname" : name}
164 attach_ids.append(ir_attachment_obj.create(cr, uid, vals))
165 mail_ids = mail_message.search(cr, uid, [('message_id','=',message_id),('res_id','=',res_id),('model','=',model)])
167 ids = mail_message.write(cr, uid, mail_ids[0], { 'attachment_ids': [(6, 0, attach_ids)],'body':body,'body_html':body_html})
168 url = self._make_url(cr, uid, res_id, model)
169 return (model, res_id, url)