Skip to content

Commit

Permalink
bugfix for bson date conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
PidgeyL committed May 3, 2017
1 parent 8012e08 commit bbd99b2
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions web/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,15 @@
from lib.Config import Configuration

def convertDatetime(dct=None):
for key, val in dct.items():
if isinstance(val, datetime.datetime):
dct[key] = val.isoformat()
if isinstance(dct,(list, tuple, set)):
for item in dct:
convertDatetime(item)
elif type(dct) is dict:
for key, val in dct.items():
if isinstance(val, datetime.datetime):
dct[key] = val.isoformat()
if isinstance(val, (dict, list)):
convertDatetime(val)
return dct

class APIError(Exception):
Expand Down Expand Up @@ -101,8 +107,9 @@ def api_wrapper(*args, **kwargs):
error = ({'status': 'error', 'reason': 'Internal server error'}, 500)
# Check if data should be returned as html or data
try:
returnType = 'application/json'
if (request.url_rule.rule.lower().startswith("/api/") or
request.url_rule.rule.lower().endswith(".json") ):
request.url_rule.rule.lower().endswith(".json") ):
# Support JSONP
if request.args.get('callback', False):
data="%s(%s)"%(request.args.get('callback'), data)
Expand All @@ -126,7 +133,8 @@ def api_wrapper(*args, **kwargs):
else:
data = (json.dumps(convertDatetime(dct=data), indent=4, sort_keys=True, default=json_util.default), 200)
return Response(data[0], mimetype=returnType), data[1]
except:
except Exception as e:
print(e)
pass
if error and error[1] == 500: raise(APIError(error[0]['reason']))
return data
Expand Down

0 comments on commit bbd99b2

Please sign in to comment.