Merge pull request #417 from gurneyalex/7.0-fix_1311004_account_move_line_index-afe
[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