Fiy python2.7'ism (#566)

The oauth plugin used timedelta.total_seconds which was introduced
in python 2.7 only. To preserve backwards compatability, we simply
calculate the time difference in seconds manually.

I considered monkeypatching total_seconds to the timedelta object,
but it is a built-in type written in C (I believe) and modifying
attributes failed horribly. Switch this to use total_seconds once we
require python 2.7 as minimum version.

Signed-off-by: Sebastian Spaeth <Sebastian@SSpaeth.de>
This commit is contained in:
Sebastian Spaeth 2012-12-14 10:54:53 +01:00
parent a04cd2e596
commit 2f5926a65d

View File

@ -1,3 +1,4 @@
# -*- coding: utf-8 -*-
# GNU MediaGoblin -- federated, autonomous media hosting # GNU MediaGoblin -- federated, autonomous media hosting
# Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS. # Copyright (C) 2011, 2012 MediaGoblin contributors. See AUTHORS.
# #
@ -216,12 +217,15 @@ def access_token(request):
token.client = code.client token.client = code.client
token.save() token.save()
# expire time of token in full seconds
# timedelta.total_seconds is python >= 2.7 or we would use that
td = token.expires - datetime.now()
exp_in = 86400*td.days + td.seconds # just ignore µsec
access_token_data = { access_token_data = {
'access_token': token.token, 'access_token': token.token,
'token_type': 'bearer', 'token_type': 'bearer',
'expires_in': int( 'expires_in': exp_in}
round(
(token.expires - datetime.now()).total_seconds()))}
return json_response(access_token_data, _disable_cors=True) return json_response(access_token_data, _disable_cors=True)
else: else:
return json_response({ return json_response({