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 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')

View File

@ -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):

View File

@ -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)

View File

@ -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)