[FIX] google_*: set a timeout when contacting google servers
This commit is contained in:
parent
95b921d0f7
commit
12416eb35f
|
@ -21,5 +21,4 @@
|
||||||
import google_account
|
import google_account
|
||||||
import controllers
|
import controllers
|
||||||
|
|
||||||
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
|
from .google_account import TIMEOUT # noqa
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ import simplejson
|
||||||
import logging
|
import logging
|
||||||
_logger = logging.getLogger(__name__)
|
_logger = logging.getLogger(__name__)
|
||||||
|
|
||||||
|
TIMEOUT = 20
|
||||||
|
|
||||||
class google_service(osv.osv_memory):
|
class google_service(osv.osv_memory):
|
||||||
_name = 'google.service'
|
_name = 'google.service'
|
||||||
|
@ -31,7 +32,7 @@ class google_service(osv.osv_memory):
|
||||||
data = werkzeug.url_encode(data)
|
data = werkzeug.url_encode(data)
|
||||||
try:
|
try:
|
||||||
req = urllib2.Request("https://accounts.google.com/o/oauth2/token", data, headers)
|
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:
|
except urllib2.HTTPError:
|
||||||
error_msg = "Something went wrong during your token generation. Maybe your Authorization Code is invalid or already expired"
|
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)
|
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))
|
raise ('Method not supported [%s] not in [GET, POST, PUT, PATCH or DELETE]!' % (type))
|
||||||
req.get_method = lambda: type.upper()
|
req.get_method = lambda: type.upper()
|
||||||
|
|
||||||
request = urllib2.urlopen(req)
|
request = urllib2.urlopen(req, timeout=TIMEOUT)
|
||||||
status = request.getcode()
|
status = request.getcode()
|
||||||
|
|
||||||
if int(status) in (204, 404): # Page not found, no response
|
if int(status) in (204, 404): # Page not found, no response
|
||||||
|
|
|
@ -20,6 +20,7 @@
|
||||||
import logging
|
import logging
|
||||||
|
|
||||||
from openerp import SUPERUSER_ID
|
from openerp import SUPERUSER_ID
|
||||||
|
from openerp.addons.google_account import TIMEOUT
|
||||||
from openerp.osv import fields, osv
|
from openerp.osv import fields, osv
|
||||||
from openerp.tools.translate import _
|
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"}
|
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept-Encoding": "gzip, deflate"}
|
||||||
try:
|
try:
|
||||||
req = urllib2.Request('https://accounts.google.com/o/oauth2/token', data, headers)
|
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:
|
except urllib2.HTTPError:
|
||||||
if user_is_admin:
|
if user_is_admin:
|
||||||
model, action_id = self.pool['ir.model.data'].get_object_reference(cr, uid, 'base_setup', 'action_general_configuration')
|
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"}
|
headers = {"Content-type": "application/x-www-form-urlencoded", "Accept-Encoding": "gzip, deflate"}
|
||||||
try:
|
try:
|
||||||
req = urllib2.Request(request_url, None, headers)
|
req = urllib2.Request(request_url, None, headers)
|
||||||
parents = urllib2.urlopen(req).read()
|
parents = urllib2.urlopen(req, timeout=TIMEOUT).read()
|
||||||
except urllib2.HTTPError:
|
except urllib2.HTTPError:
|
||||||
raise osv.except_osv(_('Warning!'), _("The Google Template cannot be found. Maybe it has been deleted."))
|
raise osv.except_osv(_('Warning!'), _("The Google Template cannot be found. Maybe it has been deleted."))
|
||||||
parents_dict = json.loads(parents)
|
parents_dict = json.loads(parents)
|
||||||
|
@ -113,7 +114,7 @@ class config(osv.Model):
|
||||||
data_json = json.dumps(data)
|
data_json = json.dumps(data)
|
||||||
# resp, content = Http().request(request_url, "POST", data_json, headers)
|
# resp, content = Http().request(request_url, "POST", data_json, headers)
|
||||||
req = urllib2.Request(request_url, 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)
|
content = json.loads(content)
|
||||||
res = {}
|
res = {}
|
||||||
if content.get('alternateLink'):
|
if content.get('alternateLink'):
|
||||||
|
@ -128,7 +129,7 @@ class config(osv.Model):
|
||||||
data = {'role': 'writer', 'type': 'anyone', 'value': '', 'withLink': True}
|
data = {'role': 'writer', 'type': 'anyone', 'value': '', 'withLink': True}
|
||||||
try:
|
try:
|
||||||
req = urllib2.Request(request_url, json.dumps(data), headers)
|
req = urllib2.Request(request_url, json.dumps(data), headers)
|
||||||
urllib2.urlopen(req)
|
urllib2.urlopen(req, timeout=TIMEOUT)
|
||||||
except urllib2.HTTPError:
|
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)
|
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)
|
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}
|
data = {'role': 'writer', 'type': 'user', 'value': user.email}
|
||||||
try:
|
try:
|
||||||
req = urllib2.Request(request_url, json.dumps(data), headers)
|
req = urllib2.Request(request_url, json.dumps(data), headers)
|
||||||
urllib2.urlopen(req)
|
urllib2.urlopen(req, timeout=TIMEOUT)
|
||||||
except urllib2.HTTPError:
|
except urllib2.HTTPError:
|
||||||
pass
|
pass
|
||||||
return res
|
return res
|
||||||
|
|
||||||
def get_google_drive_config(self, cr, uid, res_model, res_id, context=None):
|
def get_google_drive_config(self, cr, uid, res_model, res_id, context=None):
|
||||||
'''
|
'''
|
||||||
|
|
|
@ -27,7 +27,7 @@ import werkzeug.urls
|
||||||
import urllib2
|
import urllib2
|
||||||
|
|
||||||
from openerp.osv import osv
|
from openerp.osv import osv
|
||||||
from openerp import SUPERUSER_ID
|
from openerp.addons.google_account import TIMEOUT
|
||||||
|
|
||||||
_logger = logging.getLogger(__name__)
|
_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})),
|
'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,
|
data=request,
|
||||||
headers={'content-type': 'application/atom+xml', 'If-Match': '*'})
|
headers={'content-type': 'application/atom+xml', 'If-Match': '*'})
|
||||||
urllib2.urlopen(req)
|
urllib2.urlopen(req, timeout=TIMEOUT)
|
||||||
except (urllib2.HTTPError, urllib2.URLError):
|
except (urllib2.HTTPError, urllib2.URLError):
|
||||||
_logger.warning("An error occured while writting the formula on the Google Spreadsheet.")
|
_logger.warning("An error occured while writting the formula on the Google Spreadsheet.")
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue