[FIX] google_*: set a timeout when contacting google servers

This commit is contained in:
Christophe Simonis 2015-03-26 17:47:21 +01:00
parent 95b921d0f7
commit 12416eb35f
4 changed files with 13 additions and 12 deletions

View File

@ -21,5 +21,4 @@
import google_account
import controllers
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
from .google_account import TIMEOUT # noqa

View File

@ -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

View File

@ -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):
'''

View File

@ -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.")