diff --git a/brewman/brewman/__init__.py b/brewman/brewman/__init__.py index d449632f..e5642a7c 100644 --- a/brewman/brewman/__init__.py +++ b/brewman/brewman/__init__.py @@ -6,6 +6,7 @@ from pyramid.authorization import ACLAuthorizationPolicy from pyramid.session import UnencryptedCookieSessionFactoryConfig from brewman.factories import add_route from brewman.models import initialize_sql +from brewman.renderers import json_renderer, CSVRenderer from brewman.security import groupfinder current_table = 1 @@ -30,8 +31,8 @@ def main(global_config, **settings): ) # This changes in pyramid 1.4 to something similar to "config.add_renderer('myjson', JSON(indent=4))" # http://docs.pylonsproject.org/projects/pyramid/en/master/api/renderers.html#pyramid.renderers.JSON - config.add_renderer(name='json', factory='brewman.renderers.my_json_renderer_factory') - config.add_renderer(name='csv', factory='brewman.renderers.CSVRenderer') + config.add_renderer(name='json', factory=json_renderer) + config.add_renderer(name='csv', factory=CSVRenderer) config.add_static_view('icons', 'brewman:static/icons') config.add_static_view('assets', 'brewman:static/assets') diff --git a/brewman/brewman/factories.py b/brewman/brewman/factories.py index c678d871..2d1ec9c4 100644 --- a/brewman/brewman/factories.py +++ b/brewman/brewman/factories.py @@ -2,8 +2,8 @@ from pyramid.security import Everyone from pyramid.security import Authenticated from pyramid.security import Allow from brewman.models.auth import Role -from brewman.models.master import * -from brewman.models.voucher import * + + class RootFactory(object): @property def __acl__(self): diff --git a/brewman/brewman/renderers.py b/brewman/brewman/renderers.py index 37174284..eeac6883 100644 --- a/brewman/brewman/renderers.py +++ b/brewman/brewman/renderers.py @@ -1,7 +1,8 @@ import csv +from decimal import Decimal from io import StringIO -import json -from brewman.views import DecimalEncoder +import uuid +from pyramid.renderers import JSON __author__ = 'tanshu' @@ -32,15 +33,18 @@ class CSVRenderer(object): return csv_data.getvalue() -class my_json_renderer_factory: - def __init__(self, info): - pass +json_renderer = JSON() - def __call__(self, value, system): - request = system.get('request') - if request is not None: - response = request.response - ct = response.content_type - if ct == response.default_content_type: - response.content_type = 'application/json' - return json.dumps(value, cls=DecimalEncoder) +class DecimalAsFloatHack(float): + def __init__(self, d): + self.d = d + def __repr__(self): + # return format(self.d, '.5f') + return str(self.d) + +def decimal_adaptor(obj, request): + return DecimalAsFloatHack(obj) +def uuid_adaptor(obj, request): + return str(obj) +json_renderer.add_adapter(Decimal, decimal_adaptor) +json_renderer.add_adapter(uuid.UUID, uuid_adaptor) diff --git a/brewman/brewman/views/__init__.py b/brewman/brewman/views/__init__.py index cfe978b6..b765bb12 100644 --- a/brewman/brewman/views/__init__.py +++ b/brewman/brewman/views/__init__.py @@ -32,18 +32,3 @@ def app_cache(request): cache_file = pkg_resources.resource_filename(package, resource) return FileResponse(cache_file, request=request) -class DecimalAsFloatHack(float): - def __init__(self, d): - self.d = d - def __repr__(self): - # return format(self.d, '.5f') - return str(self.d) - -class DecimalEncoder(json.JSONEncoder): - def default(self, obj): - if isinstance(obj, Decimal): - return DecimalAsFloatHack(obj) - elif isinstance(obj, uuid.UUID): - return str(obj) - return json.JSONEncoder.default(self, obj) -