From 8bc3ffd234cc93ccf6455221f9fe57eef5e9385c Mon Sep 17 00:00:00 2001 From: Denis Ledoux Date: Thu, 17 Oct 2013 16:25:45 +0200 Subject: [PATCH] [FIX]google_drive: permission granted for anyone with the link has been set to readonly, and add write permission to current user (email) only bzr revid: dle@openerp.com-20131017142545-81ab6xbqi5nb6c06 --- addons/google_drive/google_drive.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/addons/google_drive/google_drive.py b/addons/google_drive/google_drive.py index 79b4f867bbd..df0d6a3c5af 100644 --- a/addons/google_drive/google_drive.py +++ b/addons/google_drive/google_drive.py @@ -116,17 +116,26 @@ class config(osv.Model): attach_pool = self.pool.get("ir.attachment") attach_vals = {'res_model': res_model, 'name': name_gdocs, 'res_id': res_id, 'type': 'url', 'url': content['alternateLink']} res['id'] = attach_pool.create(cr, uid, attach_vals) + # Commit in order to attach the document to the current object instance, even if the permissions has not been written. + cr.commit() res['url'] = content['alternateLink'] key = self._get_key_from_url(res['url']) request_url = "https://www.googleapis.com/drive/v2/files/%s/permissions?emailMessage=This+is+a+drive+file+created+by+OpenERP&sendNotificationEmails=false&access_token=%s" % (key, access_token) - data = {'role': 'writer', 'type': 'anyone', 'value': '', 'withLink': True} + data = {'role': 'reader', 'type': 'anyone', 'value': '', 'withLink': True} try: req = urllib2.Request(request_url, json.dumps(data), headers) - content = urllib2.urlopen(req).read() - content = json.loads(content) + urllib2.urlopen(req) except urllib2.HTTPError: - raise self.pool.get('res.config.settings').get_config_warning(cr, _("The permission could not be written on the file"), context=context) - return res + 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) + user = self.pool['res.users'].browse(cr, uid, uid, context=context) + if user.email: + data = {'role': 'writer', 'type': 'user', 'value': user.email} + try: + req = urllib2.Request(request_url, json.dumps(data), headers) + urllib2.urlopen(req) + except urllib2.HTTPError: + raise self.pool.get('res.config.settings').get_config_warning(cr, _("The permission 'writer' for your email '%s' has not been written on the document. Is this email a valid Google Account ?" % user.email), context=context) + return res def get_google_drive_config(self, cr, uid, res_model, res_id, context=None): '''