[MERGE] from master
[odoo/odoo.git] / addons / google_drive / google_drive.py
1 ##############################################################################
2 #
3 #    OpenERP, Open Source Management Solution
4 #    Copyright (C) 2004-2012 OpenERP SA (<http://www.openerp.com>).
5 #
6 #    This program is free software: you can redistribute it and/or modify
7 #    it under the terms of the GNU Affero General Public License as
8 #    published by the Free Software Foundation, either version 3 of the
9 #    License, or (at your option) any later version.
10 #
11 #    This program is distributed in the hope that it will be useful,
12 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
13 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 #    GNU Affero General Public License for more details.
15 #
16 #    You should have received a copy of the GNU Affero General Public License
17 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 #
19 ##############################################################################
20 import logging
21
22 from openerp import SUPERUSER_ID
23 from openerp.osv import fields, osv
24 from openerp.tools.translate import _
25
26 import werkzeug.urls
27 import urllib2
28 import json
29 import re
30 import openerp
31
32 _logger = logging.getLogger(__name__)
33
34
35 class config(osv.Model):
36     _name = 'google.drive.config'
37     _description = "Google Drive templates config"
38
39     def get_google_drive_url(self, cr, uid, config_id, res_id, template_id, context=None):
40         config = self.browse(cr, SUPERUSER_ID, config_id, context=context)
41         model = config.model_id
42         filter_name = config.filter_id and config.filter_id.name or False
43         record = self.pool.get(model.model).read(cr, uid, [res_id], context=context)[0]
44         record.update({'model': model.name, 'filter': filter_name})
45         name_gdocs = config.name_template
46         try:
47             name_gdocs = name_gdocs % record
48         except:
49             raise osv.except_osv(_('Key Error!'), _("At least one key cannot be found in your Google Drive name pattern"))
50
51         attach_pool = self.pool.get("ir.attachment")
52         attach_ids = attach_pool.search(cr, uid, [('res_model', '=', model.model), ('name', '=', name_gdocs), ('res_id', '=', res_id)])
53         url = False
54         if attach_ids:
55             attachment = attach_pool.browse(cr, uid, attach_ids[0], context)
56             url = attachment.url
57         else:
58             url = self.copy_doc(cr, uid, res_id, template_id, name_gdocs, model.model, context).get('url')
59         return url
60
61     def get_access_token(self, cr, uid, scope=None, context=None):
62         ir_config = self.pool['ir.config_parameter']
63         google_drive_refresh_token = ir_config.get_param(cr, SUPERUSER_ID, 'google_drive_refresh_token')
64         user_is_admin = self.pool['res.users'].has_group(cr, uid, 'base.group_erp_manager')
65         if not google_drive_refresh_token:
66             if user_is_admin:
67                 model, action_id = self.pool['ir.model.data'].get_object_reference(cr, uid, 'base_setup', 'action_general_configuration')
68                 msg = _("You haven't configured 'Authorization Code' generated from google, Please generate and configure it .")
69                 raise openerp.exceptions.RedirectWarning(msg, action_id, _('Go to the configuration panel'))
70             else:
71                 raise osv.except_osv(_('Error!'), _("Google Drive is not yet configured. Please contact your administrator."))
72         google_drive_client_id = ir_config.get_param(cr, SUPERUSER_ID, 'google_drive_client_id')
73         google_drive_client_secret = ir_config.get_param(cr, SUPERUSER_ID, 'google_drive_client_secret')
74         #For Getting New Access Token With help of old Refresh Token
75
76         data = werkzeug.url_encode(dict(client_id=google_drive_client_id,
77                                      refresh_token=google_drive_refresh_token,
78                                      client_secret=google_drive_client_secret,
79                                      grant_type="refresh_token",
80                                      scope=scope or 'https://www.googleapis.com/auth/drive'))
81         headers = {"Content-type": "application/x-www-form-urlencoded", "Accept-Encoding": "gzip, deflate"}
82         try:
83             req = urllib2.Request('https://accounts.google.com/o/oauth2/token', data, headers)
84             content = urllib2.urlopen(req).read()
85         except urllib2.HTTPError:
86             if user_is_admin:
87                 model, action_id = self.pool['ir.model.data'].get_object_reference(cr, uid, 'base_setup', 'action_general_configuration')
88                 msg = _("Something went wrong during the token generation. Please request again an authorization code .")
89                 raise openerp.exceptions.RedirectWarning(msg, action_id, _('Go to the configuration panel'))
90             else:
91                 raise osv.except_osv(_('Error!'), _("Google Drive is not yet configured. Please contact your administrator."))
92         content = json.loads(content)
93         return content.get('access_token')
94
95     def copy_doc(self, cr, uid, res_id, template_id, name_gdocs, res_model, context=None):
96         ir_config = self.pool['ir.config_parameter']
97         google_web_base_url = ir_config.get_param(cr, SUPERUSER_ID, 'web.base.url')
98         access_token = self.get_access_token(cr, uid, context=context)
99         # Copy template in to drive with help of new access token
100         request_url = "https://www.googleapis.com/drive/v2/files/%s?fields=parents/id&access_token=%s" % (template_id, access_token)
101         headers = {"Content-type": "application/x-www-form-urlencoded", "Accept-Encoding": "gzip, deflate"}
102         try:
103             req = urllib2.Request(request_url, None, headers)
104             parents = urllib2.urlopen(req).read()
105         except urllib2.HTTPError:
106             raise osv.except_osv(_('Warning!'), _("The Google Template cannot be found. Maybe it has been deleted."))
107         parents_dict = json.loads(parents)
108
109         record_url = "Click on link to open Record in Odoo\n %s/?db=%s#id=%s&model=%s" % (google_web_base_url, cr.dbname, res_id, res_model)
110         data = {"title": name_gdocs, "description": record_url, "parents": parents_dict['parents']}
111         request_url = "https://www.googleapis.com/drive/v2/files/%s/copy?access_token=%s" % (template_id, access_token)
112         headers = {'Content-type': 'application/json', 'Accept': 'text/plain'}
113         data_json = json.dumps(data)
114         # resp, content = Http().request(request_url, "POST", data_json, headers)
115         req = urllib2.Request(request_url, data_json, headers)
116         content = urllib2.urlopen(req).read()
117         content = json.loads(content)
118         res = {}
119         if content.get('alternateLink'):
120             attach_pool = self.pool.get("ir.attachment")
121             attach_vals = {'res_model': res_model, 'name': name_gdocs, 'res_id': res_id, 'type': 'url', 'url': content['alternateLink']}
122             res['id'] = attach_pool.create(cr, uid, attach_vals)
123             # Commit in order to attach the document to the current object instance, even if the permissions has not been written.
124             cr.commit()
125             res['url'] = content['alternateLink']
126             key = self._get_key_from_url(res['url'])
127             request_url = "https://www.googleapis.com/drive/v2/files/%s/permissions?emailMessage=This+is+a+drive+file+created+by+Odoo&sendNotificationEmails=false&access_token=%s" % (key, access_token)
128             data = {'role': 'writer', 'type': 'anyone', 'value': '', 'withLink': True}
129             try:
130                 req = urllib2.Request(request_url, json.dumps(data), headers)
131                 urllib2.urlopen(req)
132             except urllib2.HTTPError:
133                 raise self.pool.get('res.config.settings').get_config_warning(cr, _("The permission 'reader' for 'anyone with the link' has not been written on the document"), context=context)
134             user = self.pool['res.users'].browse(cr, uid, uid, context=context)
135             if user.email:
136                 data = {'role': 'writer', 'type': 'user', 'value': user.email}
137                 try:
138                     req = urllib2.Request(request_url, json.dumps(data), headers)
139                     urllib2.urlopen(req)
140                 except urllib2.HTTPError:
141                     pass
142         return res 
143
144     def get_google_drive_config(self, cr, uid, res_model, res_id, context=None):
145         '''
146         Function called by the js, when no google doc are yet associated with a record, with the aim to create one. It
147         will first seek for a google.docs.config associated with the model `res_model` to find out what's the template
148         of google doc to copy (this is usefull if you want to start with a non-empty document, a type or a name
149         different than the default values). If no config is associated with the `res_model`, then a blank text document
150         with a default name is created.
151           :param res_model: the object for which the google doc is created
152           :param ids: the list of ids of the objects for which the google doc is created. This list is supposed to have
153             a length of 1 element only (batch processing is not supported in the code, though nothing really prevent it)
154           :return: the config id and config name
155         '''
156         if not res_id:
157             raise osv.except_osv(_('Google Drive Error!'), _("Creating google drive may only be done by one at a time."))
158         # check if a model is configured with a template
159         config_ids = self.search(cr, uid, [('model_id', '=', res_model)], context=context)
160         configs = []
161         for config in self.browse(cr, uid, config_ids, context=context):
162             if config.filter_id:
163                 if (config.filter_id.user_id and config.filter_id.user_id.id != uid):
164                     #Private
165                     continue
166                 domain = [('id', 'in', [res_id])] + eval(config.filter_id.domain)
167                 local_context = context and context.copy() or {}
168                 local_context.update(eval(config.filter_id.context))
169                 google_doc_configs = self.pool.get(config.filter_id.model_id).search(cr, uid, domain, context=local_context)
170                 if google_doc_configs:
171                     configs.append({'id': config.id, 'name': config.name})
172             else:
173                 configs.append({'id': config.id, 'name': config.name})
174         return configs
175
176     def _get_key_from_url(self, url):
177         mo = re.search("(key=|/d/)([A-Za-z0-9-_]+)", url)
178         if mo:
179             return mo.group(2)
180         return None
181
182     def _resource_get(self, cr, uid, ids, name, arg, context=None):
183         result = {}
184         for data in self.browse(cr, uid, ids, context):
185             mo = self._get_key_from_url(data.google_drive_template_url)
186             if mo:
187                 result[data.id] = mo
188             else:
189                 raise osv.except_osv(_('Incorrect URL!'), _("Please enter a valid Google Document URL."))
190         return result
191
192     def _client_id_get(self, cr, uid, ids, name, arg, context=None):
193         result = {}
194         client_id = self.pool['ir.config_parameter'].get_param(cr, SUPERUSER_ID, 'google_drive_client_id')
195         for config_id in ids:
196             result[config_id] = client_id
197         return result
198
199     _columns = {
200         'name': fields.char('Template Name', required=True),
201         'model_id': fields.many2one('ir.model', 'Model', ondelete='set null', required=True),
202         'model': fields.related('model_id', 'model', type='char', string='Model', readonly=True),
203         'filter_id': fields.many2one('ir.filters', 'Filter', domain="[('model_id', '=', model)]"),
204         'google_drive_template_url': fields.char('Template URL', required=True, size=1024),
205         'google_drive_resource_id': fields.function(_resource_get, type="char", string='Resource Id'),
206         'google_drive_client_id': fields.function(_client_id_get, type="char", string='Google Client '),
207         'name_template': fields.char('Google Drive Name Pattern', help='Choose how the new google drive will be named, on google side. Eg. gdoc_%(field_name)s', required=True),
208         'active': fields.boolean('Active'),
209     }
210
211     def onchange_model_id(self, cr, uid, ids, model_id, context=None):
212         res = {}
213         if model_id:
214             model = self.pool['ir.model'].browse(cr, uid, model_id, context=context)
215             res['value'] = {'model': model.model}
216         else:
217             res['value'] = {'filter_id': False, 'model': False}
218         return res
219
220     _defaults = {
221         'name_template': 'Document %(name)s',
222         'active': True,
223     }
224
225     def _check_model_id(self, cr, uid, ids, context=None):
226         config_id = self.browse(cr, uid, ids[0], context=context)
227         if config_id.filter_id and config_id.model_id.model != config_id.filter_id.model_id:
228             return False
229         return True
230
231     _constraints = [
232         (_check_model_id, 'Model of selected filter is not matching with model of current template.', ['model_id', 'filter_id']),
233     ]
234
235     def get_google_scope(self):
236         return 'https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.file'
237
238
239 class base_config_settings(osv.TransientModel):
240     _inherit = "base.config.settings"
241
242     _columns = {
243         'google_drive_authorization_code': fields.char('Authorization Code'),
244         'google_drive_uri': fields.char('URI', readonly=True, help="The URL to generate the authorization code from Google"),
245     }
246     _defaults = {
247         'google_drive_uri': lambda s, cr, uid, c: s.pool['google.service']._get_google_token_uri(cr, uid, 'drive', scope=s.pool['google.drive.config'].get_google_scope(), context=c),
248         'google_drive_authorization_code': lambda s, cr, uid, c: s.pool['ir.config_parameter'].get_param(cr, SUPERUSER_ID, 'google_drive_authorization_code', context=c),
249     }
250
251     def set_google_authorization_code(self, cr, uid, ids, context=None):
252         ir_config_param = self.pool['ir.config_parameter']
253         config = self.browse(cr, uid, ids[0], context)
254         auth_code = config.google_drive_authorization_code
255         if auth_code and auth_code != ir_config_param.get_param(cr, uid, 'google_drive_authorization_code', context=context):
256             refresh_token = self.pool['google.service'].generate_refresh_token(cr, uid, 'drive', config.google_drive_authorization_code, context=context)
257             ir_config_param.set_param(cr, uid, 'google_drive_authorization_code', auth_code, groups=['base.group_system'])
258             ir_config_param.set_param(cr, uid, 'google_drive_refresh_token', refresh_token, groups=['base.group_system'])