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