本文整理汇总了Python中pyramid.renderers.JSON类的典型用法代码示例。如果您正苦于以下问题:Python JSON类的具体用法?Python JSON怎么用?Python JSON使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了JSON类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: main
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.bind = engine
config = Configurator(settings=settings)
json_renderer = JSON()
def dataframe_adapter(df, request):
return df.to_dict(orient="records")
json_renderer.add_adapter(DataFrame, dataframe_adapter)
config.add_renderer('json', json_renderer)
config.include('pyramid_jinja2')
config.include('pyramid_tm')
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('home', '/')
config.add_route('about', '/about')
config.add_route('search', '/search/{name}')
config.add_route('dropdown', '/dropdown/{namekey}')
config.scan()
return config.make_wsgi_app()
示例2: main
def main(global_config, **settings):
config = Configurator(settings=settings)
config.include('pyramid_chameleon')
config.registry.db = DatabaseClient()
config.registry.dvr = DVR()
def db(request):
return config.registry.db
def dvr(request):
return config.registry.dvr
config.add_request_method(db, 'db', reify=True)
config.add_request_method(dvr, 'dvr', reify=True)
json_renderer = JSON()
def datetime_adapter(obj, request):
return obj.isoformat()
json_renderer.add_adapter(datetime.datetime, datetime_adapter)
json_renderer.add_adapter(datetime.date, datetime_adapter)
config.add_renderer('json', json_renderer)
config.add_route('index', '/')
config.add_route('recordings', 'recordings')
config.add_route('recording', 'recording/{id}')
config.add_route('delete_recording', 'delete_recording/{id}')
config.add_route('skip_recording', 'skip_recording/{id}')
config.add_route('enable_recording', 'enable_recording/{id}')
config.add_route('season_passes', 'season_passes')
config.add_route('add_season_pass', 'add_season_pass')
config.add_route('search', 'search')
config.scan('.views')
return config.make_wsgi_app()
示例3: main
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
settings['sqlalchemy.url'] = settings['cn.dialect'] + quote_plus(settings['sqlalchemy.url'])
engine = engine_from_config(settings, 'sqlalchemy.')
dbConfig['url'] = settings['sqlalchemy.url']
DBSession.configure(bind=engine)
Base.metadata.bind = engine
Base.metadata.create_all(engine)
Base.metadata.reflect(views=True, extend_existing=False)
config = Configurator(settings=settings)
# Add renderer for datetime objects
json_renderer = JSON()
json_renderer.add_adapter(datetime, datetime_adapter)
json_renderer.add_adapter(Decimal, decimal_adapter)
config.add_renderer('json', json_renderer)
# Set up authentication and authorization
includeme(config)
config.set_root_factory(SecurityRoot)
# Set the default permission level to 'read'
config.set_default_permission('read')
config.include('pyramid_tm')
add_routes(config)
config.scan()
return config.make_wsgi_app()
示例4: main
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.bind = engine
config = Configurator(settings=settings)
config.include('cornice')
config.include('pyramid_mako')
json_renderer = JSON()
def datetime_adapter(obj, request):
return obj.strftime('%Y-%m-%d %H:%M:%S')
json_renderer.add_adapter(datetime.datetime, datetime_adapter)
def dictable_adapter(obj, request):
return obj.asdict()
json_renderer.add_adapter(DictableModel, dictable_adapter)
config.add_renderer('json', json_renderer)
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('home', '/')
config.scan()
return config.make_wsgi_app()
示例5: main
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
config = Configurator(settings=settings)
json_renderer = JSON()
def datetime_adapter(obj, request):
return obj.isoformat()
json_renderer.add_adapter(datetime, datetime_adapter)
config.add_renderer('json', json_renderer)
config.include('pyramid_jinja2')
config.include('.models')
config.include('.routes')
config.set_authentication_policy(MixedTokenAuthenticationPolicy())
config.set_authorization_policy(
AlwaysPassAuthenticatedAuthorizationPolicy()
)
enabled_registration_modules = settings.get(
'pydiditpyramidgateway.enabled_registration_modules'
)
if enabled_registration_modules is not None:
enabled_registration_modules = config.maybe_dotted(
enabled_registration_modules.split(',')
)
for module in enabled_registration_modules:
config.include(module)
config.scan()
pydiditbackend.initialize(json.loads(settings['backend_settings']))
return config.make_wsgi_app()
示例6: main
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application. """
config = Configurator(settings=settings)
#set up beaker session
session_factory = session_factory_from_settings(settings)
config.set_session_factory(session_factory)
# add security tweens
# config.add_tween('aws_demo.tweens.clickjacking.clickjacking_factory')
# config.add_tween('aws_demo.tweens.secure_headers.secure_headers_factory')
config.registry['mailer'] = Mailer.from_settings(settings=settings)
# modify the built in json renderer to serialize dates properly
json_date_renderer = JSON()
json_date_renderer.add_adapter(datetime.datetime, new_datetime_adapter)
# set .html renderer to allow mako templating
config.add_renderer('.html', 'pyramid.mako_templating.renderer_factory')
config.add_renderer('.htm', 'pyramid.mako_templating.renderer_factory')
config.add_renderer('json', json_date_renderer)
config.add_static_view('static', 'static', cache_max_age=3600)
# login / registration
config.add_route('index_view', '/')
config.scan()
return config.make_wsgi_app()
示例7: includeme
def includeme(config):
''' Pyramid includeme '''
json_renderer = JSON(indent=4)
json_renderer.add_adapter(datetime.datetime, datetime_adapter)
json_renderer.add_adapter(datetime.date, datetime_adapter)
config.add_renderer('json', json_renderer)
示例8: main
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
settings['sqlalchemy.url'] = settings['cn.dialect'] + quote_plus(settings['sqlalchemy.url'])
engine = engine_from_config(settings, 'sqlalchemy.')
dbConfig['url'] = settings['sqlalchemy.url']
""" Configuration de la connexion à la BDD """
# DBSession.configure(bind=engine)
Base.metadata.bind = engine
Base.metadata.create_all(engine)
Base.metadata.reflect(views=True)
""" Configuration du serveur pyramid"""
config = Configurator(settings=settings)
# Add renderer for datetime objects
json_renderer = JSON()
json_renderer.add_adapter(datetime, datetime_adapter)
json_renderer.add_adapter(date, date_adapter)
json_renderer.add_adapter(Decimal, decimal_adapter)
json_renderer.add_adapter(bytes, bytes_adapter)
config.add_renderer('json', json_renderer)
config.registry.dbmaker = scoped_session(sessionmaker(bind=engine))
config.add_request_method(db, name='dbsession', reify=True)
# Set up authentication and authorization
config.set_root_factory(SecurityRoot)
config.add_subscriber(add_cors_headers_response_callback, NewRequest)
# Set the default permission level to 'read'
config.set_default_permission('read')
config.include('pyramid_tm')
add_routes(config)
config.scan()
return config.make_wsgi_app()
示例9: includeme
def includeme(config):
"""
RPC loader for Pyramid
"""
global _do_xmlrpc, _do_jsonrpc
cfg = config.registry.settings
_do_xmlrpc = asbool(cfg.get('netprofile.rpc.xmlrpc', True))
_do_jsonrpc = asbool(cfg.get('netprofile.rpc.jsonrpc', True))
if _do_xmlrpc:
config.include('pyramid_rpc.xmlrpc')
config.add_xmlrpc_endpoint('api.xmlrpc', '/api/xmlrpc')
if _do_jsonrpc:
config.include('pyramid_rpc.jsonrpc')
renderer = JSON()
def _json_datetime(obj, req):
if obj.tzinfo is None:
obj = obj.replace(tzinfo=tzlocal())
return obj.isoformat()
renderer.add_adapter(dt.datetime, _json_datetime)
renderer.add_adapter(dt.date, lambda obj, req: obj.isoformat())
renderer.add_adapter(dt.time, lambda obj, req: obj.isoformat())
renderer.add_adapter(ipaddress.IPv4Address, lambda obj, req: int(obj))
renderer.add_adapter(decimal.Decimal, lambda obj, req: str(obj))
config.add_renderer('jsonrpc', renderer)
config.add_jsonrpc_endpoint('api.jsonrpc', '/api/jsonrpc',
default_renderer='jsonrpc')
config.scan()
示例10: main
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
engine = engine_from_config(settings, 'sqlalchemy.')
DBSession.configure(bind=engine)
Base.metadata.bind = engine
init_model()
session_factory = session_factory_from_settings(settings)
if 'localization' not in settings:
settings['localization'] = 'id_ID.UTF-8'
locale.setlocale(locale.LC_ALL, settings['localization'])
if 'timezone' not in settings:
settings['timezone'] = DefaultTimeZone
config = Configurator(settings=settings,
root_factory='reklame.models.RootFactory',
session_factory=session_factory)
config.include('pyramid_beaker')
config.include('pyramid_chameleon')
authn_policy = AuthTktAuthenticationPolicy('sosecret',
callback=group_finder, hashalg='sha512')
authz_policy = ACLAuthorizationPolicy()
config.set_authentication_policy(authn_policy)
config.set_authorization_policy(authz_policy)
config.add_request_method(get_user, 'user', reify=True)
config.add_request_method(get_title, 'title', reify=True)
config.add_notfound_view(RemoveSlashNotFoundViewFactory())
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_static_view('deform_static', 'deform:static')
config.add_static_view('files', settings['static_files'])
############################################################################
config.include('pyramid_rpc.jsonrpc') # JSON RPC
json_renderer = JSON()
json_renderer.add_adapter(datetime.datetime, lambda v, request: v.isoformat())
json_renderer.add_adapter(datetime.date, lambda v, request: v.isoformat())
config.add_renderer('myjson', json_renderer)
config.add_jsonrpc_endpoint('ws_reklame', '/ws/reklame', default_renderer="myjson")
############################################################################
routes = DBSession.query(Route.kode, Route.path, Route.nama).all()
for route in routes:
config.add_route(route.kode, route.path)
if route.nama:
titles[route.kode] = ' - '.join([main_title, route.nama])
config.scan()
app = config.make_wsgi_app()
from paste.translogger import TransLogger
app = TransLogger(app, setup_console_handler=False)
return app
示例11: patch_json_renderer
def patch_json_renderer():
""" Returns a patched JSON renderer capable of serializing custom
objects.
"""
def datetime_adapter(obj, request):
return int(obj.strftime('%s'))
renderer = JSON()
renderer.add_adapter(datetime.datetime, datetime_adapter)
return renderer
示例12: includeme
def includeme(config):
json_renderer = JSON()
def datetime_adapter(obj, request):
return obj.isoformat()
json_renderer.add_adapter(datetime.datetime, datetime_adapter)
def uuid_adapter(obj, request):
return str(obj)
json_renderer.add_adapter(uuid.UUID, uuid_adapter)
config.add_renderer('json', json_renderer)
示例13: main
def main(global_config, **settings):
"""
This function returns a Pyramid WSGI application.
"""
config = Configurator(settings=settings)
config.add_static_view('static', 'static', cache_max_age=3600)
# Use jinja2 as templating language
config.include('pyramid_jinja2')
# Add a datetime json renderer adapter.
# This snippet comes from:
# http://docs.pylonsproject.org/projects/pyramid/en/master/narr/renderers.html#using-the-add-adapter-method-of-a-custom-json-renderer
json_renderer = JSON()
def datetime_adapter(obj, request):
return obj.isoformat()
json_renderer.add_adapter(datetime.datetime, datetime_adapter)
config.add_renderer('json', json_renderer)
# (route_name, URL)
config.add_route('upcoming_events', 'api/v1.0/events')
config.add_route('home_page', '/')
config.add_route('events_view', 'events')
config.add_route('update_building_alias', 'api/v1.0/update_building_alias')
config.scan()
# db_url is stored in .ini files
db_url = urlparse(settings['mongo_uri'])
# The registry "maps resource types to views, as well as housing
# other application-specific component registrations"
config.registry.db = MongoClient(
host=db_url.hostname
)
# TODO figure out what these do. Taken from Pyramid/mongo tutorial here:
# http://pyramid-cookbook.readthedocs.org/en/latest/database/mongodb.html
def add_db(request):
db = config.registry.db[db_url.path[1:]]
if db_url.username and db_url.password:
print('authenitacintg?')
db.authenticate(db_url.username, db_url.password)
return db
def add_fs(request):
print('adding_fs??')
return GridFS(request.db)
config.add_request_method(add_db, 'db', reify=True)
config.add_request_method(add_fs, 'fs', reify=True)
return config.make_wsgi_app()
示例14: main
def main(global_config, **settings):
engine = engine_from_config(settings, 'sqlalchemy.')
session_factory = sessionmaker(bind=engine)
settings['db.session_factory'] = session_factory
config = Configurator(
settings=settings,
request_factory=RequestWithDB
)
json_renderer = JSON()
json_renderer.add_adapter(datetime.date, date_adapter)
json_renderer.add_adapter(decimal.Decimal, decimal_adapter)
config.add_renderer('json', json_renderer)
config.add_static_view('static', 'static', cache_max_age=0)
# GET /api/exploracaos = Return all exploracaos
# POST /api/exploracaos = Create a new exploracao, 'exp_id' in body
# GET /api/exploracaos/{id} = Return individual exploracao
# PUT /api/exploracaos/{id} = Update exploracao
# DELETE /api/exploracaos/{id} = Delete exploracao
config.add_route('exploracaos', '/api/exploracaos')
config.add_route('exploracaos_id', '/api/exploracaos/{id}')
# GET /api/utentes = Return all utentes
# POST /api/utentes = Create a new utente, 'nome' in body
# GET /api/utentes/{id} = Return individual utente
# PUT /api/utentes/{id} = Update utente
# DELETE /api/utentes/{id} = Delete utente
config.add_route('utentes', '/api/utentes')
config.add_route('utentes_id', '/api/utentes/{id}')
# GET /api/cultivos = Return all cultivos
# PUT /api/utentes/{id} = Update cultivo
config.add_route('cultivos', '/api/cultivos')
config.add_route('cultivos_id', '/api/cultivos/{id}')
# GET /api/settings = Return all settings
# PUT /api/settings/{property} = Update property
config.add_route('settings', '/api/settings')
config.add_route('settings_property', '/api/settings/{property}')
# GET /domains = Return all domains (utentes included)
config.add_route('domains', '/api/domains')
# GET /api/base/fountains = Return a GeoJSON
# POST /api/base/fountains = DELETE the table and insert the features in the zip
config.add_route('base_fountains', '/api/base/fountains')
config.scan()
return config.make_wsgi_app()
示例15: make_app
def make_app(settings):
config = Configurator(settings=settings)
config.include("app.routes")
# override: json renderer
json_renderer = JSON()
def datetime_adapter(obj, request):
return obj.isoformat()
json_renderer.add_adapter(datetime.datetime, datetime_adapter)
config.add_renderer('json', json_renderer)
return config.make_wsgi_app()