sale html to form
[odoo/odoo.git] / addons / plugin / plugin_handler.py
1 '''
2 Created on 18 oct. 2011
3
4 @author: openerp
5 '''
6
7 from osv import osv, fields
8
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 += '/web/webclient/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         ids = self.pool.get('res.partner.address').search(cr, uid, [('partner_id', '!=', False), ('email', 'like', address_email)])
30         res_id = ids and self.pool.get('res.partner.address').browse(cr, uid, ids[0]).partner_id.id or 0
31         url = self._make_url(cr, uid, res_id, 'res.partner')
32         return ('res.partner', res_id , url)
33
34     def document_get(self, cr, uid, email):
35         """
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) 
40         """
41         mail_message_obj = self.pool.get('mail.message')
42         model = ""
43         res_id = 0
44         url = ""
45         name = ""
46         msg = mail_message_obj.parse_message(email)
47         references = [msg.get('message-id')]
48         refs =  msg.get('references',False)
49         if refs:
50             references.extend(refs.split())
51         msg_ids = mail_message_obj.search(cr, uid, [('message_id','in',references)])
52         if msg_ids:
53             msg = mail_message_obj.browse(cr, uid, msg_ids[0])
54             res_id = msg.res_id
55             model = msg.model
56             url = self._make_url(cr, uid, res_id, model)
57             name =  self.pool.get(model).name_get(cr, uid, [res_id])[0][1]
58         return (model,res_id, url,name)
59
60
61     def document_type(self, cr, uid, context=None):
62         """
63             Return the list of available model to push
64             res.partner is a special case
65             otherwise all model that inherit from mail.thread
66             ['res.partner', 'project.issue']
67         """
68         mail_thread_obj = self.pool.get('mail.thread')
69         doc_dict = mail_thread_obj.message_capable_models(cr, uid, context)
70         doc_dict['res.partner'] = "Partner"
71         return doc_dict.items()
72
73     # Can be used where search record was used 
74     def list_document_get(self, cr, uid, model, name):
75         """
76             This function return the result of name_search on the object model
77             @param model: the name of the model 
78             @param : the name of the document
79             @return : the result of name_search a list of tuple 
80             [(id, 'name')]
81         """
82         return self.pool.get(model).name_search(cr,uid,name)
83
84     def push_message(self, cr, uid, model, email, res_id=0):
85         """
86             @param email: email is a standard RFC2822 email message
87             @param model: On which model the message is pushed
88             @param thread_id: on which document the message is pushed, if thread_id = 0 a new document is created 
89             @return Dictionary which contain model , url and resource id.
90         """
91         mail_message = self.pool.get('mail.message')
92         model_obj = self.pool.get(model)
93         msg = mail_message.parse_message(email)
94         message_id = msg.get('message-id')
95         mail_ids = mail_message.search(cr, uid, [('message_id','=',message_id),('res_id','=',res_id),('model','=',model)])
96         
97         if message_id and mail_ids :
98             mail_record = mail_message.browse(cr, uid, mail_ids)[0]
99             res_id = mail_record.res_id
100             notify = "Email already pushed"
101         elif res_id == 0:
102             if model == 'res.partner':
103                 notify = 'User the button Partner to create a new partner'
104             else:
105                 res_id = model_obj.message_new(cr, uid, msg)
106                 notify = "Mail succefully pushed, a new %s has been created " % model
107         else:
108             if model == 'res.partner':
109                 model_obj = self.pool.get('mail.thread')
110             res = self.pool.get(model).browse(cr, uid, [res_id])
111             model_obj.message_append_dict(cr, uid, res, msg)
112             notify = "Mail succefully pushed"
113             
114         url = self._make_url(cr, uid, res_id, model)
115         return (model, res_id, url, notify)
116
117     def contact_create(self, cr, uid, data, partner_id):
118         """
119             @param data : the data use to create the res.partner.address
120                 [('field_name', value)], field name is required
121             @param partner_id : On which partner the address is attached 
122              if partner_id = 0 then create a new partner with the same name that the address
123             @return : the partner_id sended or created, this allow the plugin to open the right partner page
124         """
125         partner_obj = self.pool.get('res.partner')
126         dictcreate = dict(data) 
127         if partner_id == 0:
128             partner_id =  partner_obj.create(cr, uid, {'name':dictcreate.get('name')})
129         dictcreate['partner_id'] = partner_id
130         self.pool.get('res.partner.address').create(cr, uid, dictcreate)
131         url = self._make_url(cr, uid, partner_id, 'res.partner')
132         return ('res.partner', partner_id, url)
133
134     # Specific to outlook rfc822 is not available so we split in arguments headerd,body,attachemnts
135     def push_message_outlook(self, cr, uid, model, headers,res_id=0 ,body_text=False, body_html=False, attachments=False):
136         # ----------------------------------------
137         # solution 1
138         # construct a fake rfc822 from the separated arguement
139         #m = email.asdfsadf
140         # use the push_message method
141         #self.push_message(m)
142         # ----------------------------------------
143         # solution 2
144         # use self.pushmessage only with header and body
145         # add attachemnt yourself after
146         mail_message = self.pool.get('mail.message')        
147         ir_attachment_obj = self.pool.get('ir.attachment')
148         attach_ids = []
149         msg = mail_message.parse_message(headers)
150         message_id = msg.get('message-id')    
151         push_mail = self.push_message(cr, uid, model, headers, res_id)
152         res_id = push_mail[1]
153         model =  push_mail[0]            
154         for name in attachments.keys():
155             attachment_ids = ir_attachment_obj.search(cr, uid, [('res_model', '=', model), ('res_id', '=', res_id), ('datas_fname', '=', name)])
156             if attachment_ids:
157                 attach_ids.append( attachment_ids[0])
158             else:
159                 vals = {"res_model": model, "res_id": res_id, "name": name, "datas" :attachments[name], "datas_fname" : name}
160                 attach_ids.append(ir_attachment_obj.create(cr, uid, vals))
161         mail_ids = mail_message.search(cr, uid, [('message_id','=',message_id),('res_id','=',res_id),('model','=',model)])
162         if mail_ids:
163             ids =  mail_message.write(cr, uid,mail_ids[0],{ 'attachment_ids': [(6, 0, attach_ids)],'body_text':body_text,'body_html':body_html})
164         url = self._make_url(cr, uid, res_id, model)
165         return (model, res_id, url)