[MERGE]upstream
[odoo/odoo.git] / addons / web_linkedin / web_linkedin.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
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.
11 #
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.
16 #
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/>.
19 #
20 ##############################################################################
21
22 import base64
23 import urllib2
24 from urlparse import urlparse, urlunparse
25
26 import openerp
27 from openerp.osv import fields, osv
28
29 class Binary(openerp.addons.web.http.Controller):
30     _cp_path = "/web_linkedin/binary"
31
32     @openerp.addons.web.http.jsonrequest
33     def url2binary(self, req, url):
34         """Used exclusively to load images from LinkedIn profiles, must not be used for anything else."""
35         req.session.assert_valid(force=True)
36         _scheme, _netloc, path, params, query, fragment = urlparse(url)
37         # media.linkedin.com is the master domain for LinkedIn media (replicated to CDNs),
38         # so forcing it should always work and prevents abusing this method to load arbitrary URLs
39         url = urlunparse(('http', 'media.linkedin.com', path, params, query, fragment))
40         bfile = urllib2.urlopen(url)
41         return base64.b64encode(bfile.read())
42     
43 class web_linkedin_settings(osv.osv_memory):
44     _inherit = 'sale.config.settings'
45     _columns = {
46         'api_key': fields.char(string="API Key", size=50),
47         'server_domain': fields.char(size=100),
48     }
49     
50     def get_default_linkedin(self, cr, uid, fields, context=None):
51         key = self.pool.get("ir.config_parameter").get_param(cr, uid, "web.linkedin.apikey") or ""
52         dom = self.pool.get('ir.config_parameter').get_param(cr, uid, 'web.base.url')
53         return {'api_key': key, 'server_domain': dom,}
54     
55     def set_linkedin(self, cr, uid, ids, context=None):
56         key = self.browse(cr, uid, ids[0], context)["api_key"] or ""
57         self.pool.get("ir.config_parameter").set_param(cr, uid, "web.linkedin.apikey", key)
58
59 class web_linkedin_fields(osv.Model):
60     _inherit = 'res.partner'
61
62     def _get_url(self, cr, uid, ids, name, arg, context=None):
63         res = dict((id, False) for id in ids)
64         for partner in self.browse(cr, uid, ids, context=context):
65             res[partner.id] = partner.linkedin_url
66         return res
67
68     def linkedin_check_similar_partner(self, cr, uid, linkedin_datas, context=None):
69         res = []
70         res_partner = self.pool.get('res.partner')
71         for linkedin_data in linkedin_datas:
72             partner_ids = res_partner.search(cr, uid, ["|", ("linkedin_id", "=", linkedin_data['id']), 
73                     "&", ("linkedin_id", "=", False), 
74                     "|", ("name", "ilike", linkedin_data['firstName'] + "%" + linkedin_data['lastName']), ("name", "ilike", linkedin_data['lastName'] + "%" + linkedin_data['firstName'])], context=context)
75             if partner_ids:
76                 partner = res_partner.read(cr, uid, partner_ids[0], ["image", "mobile", "phone", "parent_id", "name", "email", "function", "linkedin_id"], context=context)
77                 if partner['linkedin_id'] and partner['linkedin_id'] != linkedin_data['id']:
78                     partner.pop('id')
79                 if partner['parent_id']:
80                     partner['parent_id'] = partner['parent_id'][0]
81                 for key, val in partner.items():
82                     if not val:
83                         partner.pop(key)
84                 res.append(partner)
85             else:
86                 res.append({})
87         return res
88
89     _columns = {
90         'linkedin_id': fields.char(string="LinkedIn ID", size=50),
91         'linkedin_url': fields.char(string="LinkedIn url", size=100, store=True),
92         'linkedin_public_url': fields.function(_get_url, type='text', string="LinkedIn url", 
93             help="This url is set automatically when you join the partner with a LinkedIn account."),
94     }