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