本文整理汇总了Python中pyramid.renderers.JSON属性的典型用法代码示例。如果您正苦于以下问题:Python renderers.JSON属性的具体用法?Python renderers.JSON怎么用?Python renderers.JSON使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类pyramid.renderers
的用法示例。
在下文中一共展示了renderers.JSON属性的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_pyramid_custom_json_encoder
# 需要导入模块: from pyramid import renderers [as 别名]
# 或者: from pyramid.renderers import JSON [as 别名]
def test_pyramid_custom_json_encoder(app_config: Configurator):
"""Test we can still use user-defined custom adapter"""
from pyramid.renderers import json_renderer_factory
def serialize_anyclass(obj, request):
assert False # This asserts this method will not be called
json_renderer_factory.add_adapter(NonSerializable, serialize_anyclass)
def other_serializer(obj, request):
return "other_serializer"
my_renderer = JSON()
my_renderer.add_adapter(NonSerializable, other_serializer)
app_config.add_renderer("json", my_renderer)
app = TestApp(app_config.make_wsgi_app())
response = app.get("/extra_claims")
token = str(response.json_body["token"])
response = app.get("/dump_claims", headers={"X-Token": token})
assert response.json_body["extra_claim"] == "other_serializer"
示例2: __init__
# 需要导入模块: from pyramid import renderers [as 别名]
# 或者: from pyramid.renderers import JSON [as 别名]
def __init__(self, api):
self.api = api
self.metadata = {}
# Load mako templating
self.api.config.include('pyramid_mako')
self.api.config.add_renderer('json_sorted', JSON(sort_keys=True))
self.views = [
VIEWS(
attr='openapi_spec',
route_name='specification',
request_method='',
renderer='json_sorted'
),
VIEWS(
attr='swagger_ui',
route_name='',
request_method='',
renderer='pyramid_jsonapi.metadata.OpenAPI:swagger-ui/index.mako'
)
]
示例3: __call__
# 需要导入模块: from pyramid import renderers [as 别名]
# 或者: from pyramid.renderers import JSON [as 别名]
def __call__(self, *args, **kwargs):
json_renderer = None
if self.registry is not None:
json_renderer = self.registry.queryUtility(
IRendererFactory, "json", default=JSONEncoder
)
request = kwargs.get("request")
if not kwargs.get("default") and isinstance(json_renderer, JSON):
self.components = json_renderer.components
kwargs["default"] = self._make_default(request)
return JSONEncoder(*args, **kwargs)
示例4: __json__
# 需要导入模块: from pyramid import renderers [as 别名]
# 或者: from pyramid.renderers import JSON [as 别名]
def __json__(self):
return "This is JSON Serializable"
示例5: test_pyramid_json_encoder_fail
# 需要导入模块: from pyramid import renderers [as 别名]
# 或者: from pyramid.renderers import JSON [as 别名]
def test_pyramid_json_encoder_fail(app):
with pytest.raises(TypeError) as e:
app.get("/extra_claims")
assert "NonSerializable" in str(e.value)
assert "is not JSON serializable" in str(e.value)
示例6: register_json_renderer
# 需要导入模块: from pyramid import renderers [as 别名]
# 或者: from pyramid.renderers import JSON [as 别名]
def register_json_renderer(config):
json_renderer = JSON(indent=4)
json_renderer.add_adapter(Post, lambda p, _: p.__dict__)
config.add_renderer('pretty_json', json_renderer)
示例7: make_app
# 需要导入模块: from pyramid import renderers [as 别名]
# 或者: from pyramid.renderers import JSON [as 别名]
def make_app(server_config):
config = Configurator(
settings=server_config, root_factory=APIFactory, default_permission="access"
)
config.include("pyramid_jinja2")
module_, class_ = server_config["signature_checker"].rsplit(".", maxsplit=1)
signature_checker_cls = getattr(importlib.import_module(module_), class_)
config.registry.signature_checker = signature_checker_cls(server_config["secret"])
authn_policy = AuthTktAuthenticationPolicy(
server_config["cookie_secret"], max_age=2592000
)
authz_policy = ACLAuthorizationPolicy()
config.set_authentication_policy(authn_policy)
config.set_authorization_policy(authz_policy)
json_renderer = JSON(serializer=json.dumps, indent=4)
json_renderer.add_adapter(datetime.datetime, datetime_adapter)
json_renderer.add_adapter(uuid.UUID, uuid_adapter)
config.add_renderer("json", json_renderer)
config.add_subscriber(
"channelstream.subscribers.handle_new_request", "pyramid.events.NewRequest"
)
config.add_request_method("channelstream.utils.handle_cors", "handle_cors")
config.include("channelstream.wsgi_views")
config.scan("channelstream.wsgi_views.server")
config.scan("channelstream.wsgi_views.error_handlers")
config.scan("channelstream.events")
config.include("pyramid_apispec.views")
config.pyramid_apispec_add_explorer(
spec_route_name="openapi_spec",
script_generator="channelstream.utils:swagger_ui_script_template",
permission="admin",
route_args={
"factory": "channelstream.wsgi_views.wsgi_security:AdminAuthFactory"
},
)
app = config.make_wsgi_app()
return app
示例8: __init__
# 需要导入模块: from pyramid import renderers [as 别名]
# 或者: from pyramid.renderers import JSON [as 别名]
def __init__(self, renderer_factory=JSON()):
self.renderer_factory = renderer_factory
示例9: configure_renderers
# 需要导入模块: from pyramid import renderers [as 别名]
# 或者: from pyramid.renderers import JSON [as 别名]
def configure_renderers(config):
json_renderer = JSON(indent=4)
json_renderer.add_adapter(Car, lambda c, _: c.to_dict())
config.add_renderer('json', json_renderer)
示例10: main
# 需要导入模块: from pyramid import renderers [as 别名]
# 或者: from pyramid.renderers import JSON [as 别名]
def main(global_config, **settings):
""" This function returns a Pyramid WSGI application.
"""
# The usual stuff from the pyramid alchemy scaffold.
engine = engine_from_config(settings, 'sqlalchemy.')
models.DBSession.configure(bind=engine)
models.Base.metadata.bind = engine
config = Configurator(settings=settings)
config.add_static_view('static', 'static', cache_max_age=3600)
config.add_route('home', '/')
config.add_route('echo', '/echo/{type}')
config.scan(views)
# Set up the renderer.
renderer = JSON()
renderer.add_adapter(datetime.date, datetime_adapter)
config.add_renderer('json', renderer)
# Lines specific to pyramid_jsonapi.
# Create an API instance.
pj = pyramid_jsonapi.PyramidJSONAPI(
config,
test_settings['models_iterable'][
settings.get('pyramid_jsonapi_tests.models_iterable', 'module')
],
lambda view: models.DBSession
)
# Register a bad filter operator for test purposes.
pj.filter_registry.register('bad_op')
# Create the routes and views automagically.
pj.create_jsonapi_using_magic_and_pixie_dust()
person_view = pj.view_classes[
models.Person
]
person_view.callbacks['after_serialise_object'].appendleft(
person_callback_add_information
)
person_view.allowed_fields = property(person_allowed_fields)
person_view.allowed_object = person_allowed_object
pj.append_callback_set_to_all_views(
'access_control_serialised_objects'
)
# Back to the usual pyramid stuff.
return config.make_wsgi_app()