From 12416eb35f66f4924743f5b4098e281d499375ba Mon Sep 17 00:00:00 2001 From: Christophe Simonis Date: Thu, 26 Mar 2015 17:47:21 +0100 Subject: [PATCH] [FIX] google_*: set a timeout when contacting google servers --- addons/google_account/__init__.py | 3 +-- addons/google_account/google_account.py | 5 +++-- addons/google_drive/google_drive.py | 13 +++++++------ addons/google_spreadsheet/google_spreadsheet.py | 4 ++-- 4 files changed, 13 insertions(+), 12 deletions(-) diff --git a/addons/google_account/__init__.py b/addons/google_account/__init__.py index 640a9747f2d..24a68d8ba78 100644 --- a/addons/google_account/__init__.py +++ b/addons/google_account/__init__.py @@ -21,5 +21,4 @@ import google_account import controllers -# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: - +from .google_account import TIMEOUT # noqa diff --git a/addons/google_account/google_account.py b/addons/google_account/google_account.py index db137b087e6..2cc56d0cba4 100644 --- a/addons/google_account/google_account.py +++ b/addons/google_account/google_account.py @@ -15,6 +15,7 @@ import simplejson import logging _logger = logging.getLogger(__name__) +TIMEOUT = 20 class google_service(osv.osv_memory): _name = 'google.service' @@ -31,7 +32,7 @@ class google_service(osv.osv_memory): data = werkzeug.url_encode(data) try: req = urllib2.Request("https://accounts.google.com/o/oauth2/token", data, headers) - content = urllib2.urlopen(req).read() + content = urllib2.urlopen(req, timeout=TIMEOUT).read() except urllib2.HTTPError: error_msg = "Something went wrong during your token generation. Maybe your Authorization Code is invalid or already expired" raise self.pool.get('res.config.settings').get_config_warning(cr, _(error_msg), context=context) @@ -148,7 +149,7 @@ class google_service(osv.osv_memory): raise ('Method not supported [%s] not in [GET, POST, PUT, PATCH or DELETE]!' % (type)) req.get_method = lambda: type.upper() - request = urllib2.urlopen(req) + request = urllib2.urlopen(req, timeout=TIMEOUT) status = request.getcode() if int(status) in (204, 404): # Page not found, no response diff --git a/addons/google_drive/google_drive.py b/addons/google_drive/google_drive.py index 1a95a5ec1ee..789b5527e31 100644 --- a/addons/google_drive/google_drive.py +++ b/addons/google_drive/google_drive.py @@ -20,6 +20,7 @@ import logging from openerp import SUPERUSER_ID +from openerp.addons.google_account import TIMEOUT from openerp.osv import fields, osv from openerp.tools.translate import _ @@ -81,7 +82,7 @@ class config(osv.Model): headers = {"Content-type": "application/x-www-form-urlencoded", "Accept-Encoding": "gzip, deflate"} try: req = urllib2.Request('https://accounts.google.com/o/oauth2/token', data, headers) - content = urllib2.urlopen(req).read() + content = urllib2.urlopen(req, timeout=TIMEOUT).read() except urllib2.HTTPError: if user_is_admin: model, action_id = self.pool['ir.model.data'].get_object_reference(cr, uid, 'base_setup', 'action_general_configuration') @@ -101,7 +102,7 @@ class config(osv.Model): headers = {"Content-type": "application/x-www-form-urlencoded", "Accept-Encoding": "gzip, deflate"} try: req = urllib2.Request(request_url, None, headers) - parents = urllib2.urlopen(req).read() + parents = urllib2.urlopen(req, timeout=TIMEOUT).read() except urllib2.HTTPError: raise osv.except_osv(_('Warning!'), _("The Google Template cannot be found. Maybe it has been deleted.")) parents_dict = json.loads(parents) @@ -113,7 +114,7 @@ class config(osv.Model): data_json = json.dumps(data) # resp, content = Http().request(request_url, "POST", data_json, headers) req = urllib2.Request(request_url, data_json, headers) - content = urllib2.urlopen(req).read() + content = urllib2.urlopen(req, timeout=TIMEOUT).read() content = json.loads(content) res = {} if content.get('alternateLink'): @@ -128,7 +129,7 @@ class config(osv.Model): data = {'role': 'writer', 'type': 'anyone', 'value': '', 'withLink': True} try: req = urllib2.Request(request_url, json.dumps(data), headers) - urllib2.urlopen(req) + urllib2.urlopen(req, timeout=TIMEOUT) except urllib2.HTTPError: 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) @@ -136,10 +137,10 @@ class config(osv.Model): data = {'role': 'writer', 'type': 'user', 'value': user.email} try: req = urllib2.Request(request_url, json.dumps(data), headers) - urllib2.urlopen(req) + urllib2.urlopen(req, timeout=TIMEOUT) except urllib2.HTTPError: pass - return res + return res def get_google_drive_config(self, cr, uid, res_model, res_id, context=None): ''' diff --git a/addons/google_spreadsheet/google_spreadsheet.py b/addons/google_spreadsheet/google_spreadsheet.py index df63ac5f632..2d062c9a9d2 100644 --- a/addons/google_spreadsheet/google_spreadsheet.py +++ b/addons/google_spreadsheet/google_spreadsheet.py @@ -27,7 +27,7 @@ import werkzeug.urls import urllib2 from openerp.osv import osv -from openerp import SUPERUSER_ID +from openerp.addons.google_account import TIMEOUT _logger = logging.getLogger(__name__) @@ -92,7 +92,7 @@ class config(osv.osv): 'https://spreadsheets.google.com/feeds/cells/%s/od6/private/full/batch?%s' % (spreadsheet_key, werkzeug.url_encode({'v': 3, 'access_token': access_token})), data=request, headers={'content-type': 'application/atom+xml', 'If-Match': '*'}) - urllib2.urlopen(req) + urllib2.urlopen(req, timeout=TIMEOUT) except (urllib2.HTTPError, urllib2.URLError): _logger.warning("An error occured while writting the formula on the Google Spreadsheet.")