Skip to content
Snippets Groups Projects
Commit bdea9b7c authored by Gunnar Schaefer's avatar Gunnar Schaefer
Browse files

Fix /config.js endpoint

parent d9ce5e37
No related branches found
No related tags found
No related merge requests found
import sys import sys
import json import json
import pytz
import webapp2 import webapp2
import datetime import datetime
import bson.objectid
import webapp2_extras.routes import webapp2_extras.routes
from . import core from . import core
from . import jobs from . import jobs
from . import util
from . import config from . import config
from handlers import listhandler from handlers import listhandler
from handlers import userhandler from handlers import userhandler
...@@ -115,18 +114,10 @@ routes = [ ...@@ -115,18 +114,10 @@ routes = [
] ]
def custom_json_serializer(obj):
if isinstance(obj, bson.objectid.ObjectId):
return str(obj)
elif isinstance(obj, datetime.datetime):
return pytz.timezone('UTC').localize(obj).isoformat()
raise TypeError(repr(obj) + " is not JSON serializable")
def dispatcher(router, request, response): def dispatcher(router, request, response):
rv = router.default_dispatcher(request, response) rv = router.default_dispatcher(request, response)
if rv is not None: if rv is not None:
response.write(json.dumps(rv, default=custom_json_serializer)) response.write(json.dumps(rv, default=util.custom_json_serializer))
response.headers['Content-Type'] = 'application/json; charset=utf-8' response.headers['Content-Type'] = 'application/json; charset=utf-8'
......
...@@ -26,7 +26,11 @@ class Config(base.RequestHandler): ...@@ -26,7 +26,11 @@ class Config(base.RequestHandler):
return config.get_public_config() return config.get_public_config()
def get_js(self): def get_js(self):
self.response.write('config = ' + json.dumps(self.get(), sort_keys=True, indent=4, separators=(',', ': ')) + ';') self.response.write(
'config = ' +
json.dumps( self.get(), sort_keys=True, indent=4, separators=(',', ': '), default=util.custom_json_serializer,) +
';'
)
class Core(base.RequestHandler): class Core(base.RequestHandler):
......
...@@ -9,6 +9,7 @@ import pymongo ...@@ -9,6 +9,7 @@ import pymongo
import zipfile import zipfile
import datetime import datetime
import mimetypes import mimetypes
import bson.objectid
import tempdir as tempfile import tempdir as tempfile
MIMETYPES = [ MIMETYPES = [
...@@ -87,6 +88,7 @@ def guess_filetype(filepath, mimetype): ...@@ -87,6 +88,7 @@ def guess_filetype(filepath, mimetype):
else: else:
return subtype return subtype
def path_from_hash(hash_): def path_from_hash(hash_):
""" """
create a filepath from a hash create a filepath from a hash
...@@ -98,3 +100,11 @@ def path_from_hash(hash_): ...@@ -98,3 +100,11 @@ def path_from_hash(hash_):
path = [hash_[i] for i in xrange(8)] path = [hash_[i] for i in xrange(8)]
path.append(hash_) path.append(hash_)
return os.path.join(*path) return os.path.join(*path)
def custom_json_serializer(obj):
if isinstance(obj, bson.objectid.ObjectId):
return str(obj)
elif isinstance(obj, datetime.datetime):
return pytz.timezone('UTC').localize(obj).isoformat()
raise TypeError(repr(obj) + " is not JSON serializable")
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment