Updated json renderer to pyramid 1.4 format

This commit is contained in:
Tanshu
2013-05-19 17:09:14 +05:30
parent fa95e79e0f
commit 762ff27ffc
4 changed files with 22 additions and 32 deletions

View File

@ -6,6 +6,7 @@ from pyramid.authorization import ACLAuthorizationPolicy
from pyramid.session import UnencryptedCookieSessionFactoryConfig from pyramid.session import UnencryptedCookieSessionFactoryConfig
from brewman.factories import add_route from brewman.factories import add_route
from brewman.models import initialize_sql from brewman.models import initialize_sql
from brewman.renderers import json_renderer, CSVRenderer
from brewman.security import groupfinder from brewman.security import groupfinder
current_table = 1 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))" # 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 # 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='json', factory=json_renderer)
config.add_renderer(name='csv', factory='brewman.renderers.CSVRenderer') config.add_renderer(name='csv', factory=CSVRenderer)
config.add_static_view('icons', 'brewman:static/icons') config.add_static_view('icons', 'brewman:static/icons')
config.add_static_view('assets', 'brewman:static/assets') config.add_static_view('assets', 'brewman:static/assets')

View File

@ -2,8 +2,8 @@ from pyramid.security import Everyone
from pyramid.security import Authenticated from pyramid.security import Authenticated
from pyramid.security import Allow from pyramid.security import Allow
from brewman.models.auth import Role from brewman.models.auth import Role
from brewman.models.master import *
from brewman.models.voucher import *
class RootFactory(object): class RootFactory(object):
@property @property
def __acl__(self): def __acl__(self):

View File

@ -1,7 +1,8 @@
import csv import csv
from decimal import Decimal
from io import StringIO from io import StringIO
import json import uuid
from brewman.views import DecimalEncoder from pyramid.renderers import JSON
__author__ = 'tanshu' __author__ = 'tanshu'
@ -32,15 +33,18 @@ class CSVRenderer(object):
return csv_data.getvalue() return csv_data.getvalue()
class my_json_renderer_factory: json_renderer = JSON()
def __init__(self, info):
pass
def __call__(self, value, system): class DecimalAsFloatHack(float):
request = system.get('request') def __init__(self, d):
if request is not None: self.d = d
response = request.response def __repr__(self):
ct = response.content_type # return format(self.d, '.5f')
if ct == response.default_content_type: return str(self.d)
response.content_type = 'application/json'
return json.dumps(value, cls=DecimalEncoder) 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)

View File

@ -32,18 +32,3 @@ def app_cache(request):
cache_file = pkg_resources.resource_filename(package, resource) cache_file = pkg_resources.resource_filename(package, resource)
return FileResponse(cache_file, request=request) 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)