Commit bbd99b2a authored by PidgeyL's avatar PidgeyL
Browse files

bugfix for bson date conversion

parent 8012e084
Loading
Loading
Loading
Loading
+13 −5
Original line number Diff line number Diff line
@@ -43,9 +43,15 @@ import lib.Toolkit as tk
from lib.Config import Configuration

def convertDatetime(dct=None):
  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):
@@ -101,6 +107,7 @@ class API():
        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") ):
          # Support JSONP
@@ -126,7 +133,8 @@ class API():
            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