Fix JSON rendering for Cornice API views

also make sure we use Cornice for all API views
This commit is contained in:
Lance Edgar 2023-03-09 14:07:10 -06:00
parent 5aa982c95f
commit 2ebe0401c3
4 changed files with 38 additions and 30 deletions

View file

@ -95,6 +95,7 @@ requires = [
'rattail[db,bouncer]', # 0.5.0 'rattail[db,bouncer]', # 0.5.0
'six', # 1.10.0 'six', # 1.10.0
'sa-filters', # 1.2.0 'sa-filters', # 1.2.0
'simplejson', # 3.18.3
'transaction', # 1.2.0 'transaction', # 1.2.0
'waitress', # 0.8.1 'waitress', # 0.8.1
'WebHelpers2', # 2.0 'WebHelpers2', # 2.0

View file

@ -348,13 +348,12 @@ class APIBatchRowView(APIBatchMixin, APIMasterView):
route_prefix = cls.get_route_prefix() route_prefix = cls.get_route_prefix()
permission_prefix = cls.get_permission_prefix() permission_prefix = cls.get_permission_prefix()
collection_url_prefix = cls.get_collection_url_prefix() collection_url_prefix = cls.get_collection_url_prefix()
object_url_prefix = cls.get_object_url_prefix()
if cls.supports_quick_entry: if cls.supports_quick_entry:
# quick entry # quick entry
config.add_route('{}.quick_entry'.format(route_prefix), '{}/quick-entry'.format(collection_url_prefix), quick_entry = Service(name='{}.quick_entry'.format(route_prefix),
request_method=('OPTIONS', 'POST')) path='{}/quick-entry'.format(collection_url_prefix))
config.add_view(cls, attr='quick_entry', route_name='{}.quick_entry'.format(route_prefix), quick_entry.add_view('POST', 'quick_entry', klass=cls,
permission='{}.edit'.format(permission_prefix), permission='{}.edit'.format(permission_prefix))
renderer='json') config.add_cornice_service(quick_entry)

View file

@ -31,6 +31,7 @@ import humanize
from rattail.db import model from rattail.db import model
from rattail.util import pretty_quantity from rattail.util import pretty_quantity
from cornice import Service
from deform import widget as dfwidget from deform import widget as dfwidget
from tailbone import forms from tailbone import forms
@ -143,26 +144,26 @@ class ReceivingBatchViews(APIBatchView):
collection_url_prefix = cls.get_collection_url_prefix() collection_url_prefix = cls.get_collection_url_prefix()
object_url_prefix = cls.get_object_url_prefix() object_url_prefix = cls.get_object_url_prefix()
# auto-receive # auto_receive
config.add_route('{}.auto_receive'.format(route_prefix), auto_receive = Service(name='{}.auto_receive'.format(route_prefix),
'{}/{{uuid}}/auto-receive'.format(object_url_prefix)) path='{}/{{uuid}}/auto-receive'.format(object_url_prefix))
config.add_view(cls, attr='auto_receive', auto_receive.add_view('GET', 'auto_receive', klass=cls,
route_name='{}.auto_receive'.format(route_prefix), permission='{}.auto_receive'.format(permission_prefix))
permission='{}.auto_receive'.format(permission_prefix), config.add_cornice_service(auto_receive)
renderer='json')
# mark receiving complete # mark_receiving_complete
config.add_route('{}.mark_receiving_complete'.format(route_prefix), '{}/{{uuid}}/mark-receiving-complete'.format(object_url_prefix)) mark_receiving_complete = Service(name='{}.mark_receiving_complete'.format(route_prefix),
config.add_view(cls, attr='mark_receiving_complete', route_name='{}.mark_receiving_complete'.format(route_prefix), path='{}/{{uuid}}/mark-receiving-complete'.format(object_url_prefix))
permission='{}.edit'.format(permission_prefix), mark_receiving_complete.add_view('POST', 'mark_receiving_complete', klass=cls,
renderer='json') permission='{}.edit'.format(permission_prefix))
config.add_cornice_service(mark_receiving_complete)
# eligible purchases # eligible purchases
config.add_route('{}.eligible_purchases'.format(route_prefix), '{}/eligible-purchases'.format(collection_url_prefix), eligible_purchases = Service(name='{}.eligible_purchases'.format(route_prefix),
request_method='GET') path='{}/eligible-purchases'.format(collection_url_prefix))
config.add_view(cls, attr='eligible_purchases', route_name='{}.eligible_purchases'.format(route_prefix), eligible_purchases.add_view('GET', 'eligible_purchases', klass=cls,
permission='{}.create'.format(permission_prefix), permission='{}.create'.format(permission_prefix))
renderer='json') config.add_cornice_service(eligible_purchases)
class ReceivingBatchRowViews(APIBatchRowView): class ReceivingBatchRowViews(APIBatchRowView):
@ -437,11 +438,11 @@ class ReceivingBatchRowViews(APIBatchRowView):
object_url_prefix = cls.get_object_url_prefix() object_url_prefix = cls.get_object_url_prefix()
# receive (row) # receive (row)
config.add_route('{}.receive'.format(route_prefix), '{}/{{uuid}}/receive'.format(object_url_prefix), receive = Service(name='{}.receive'.format(route_prefix),
request_method=('OPTIONS', 'POST')) path='{}/{{uuid}}/receive'.format(object_url_prefix))
config.add_view(cls, attr='receive', route_name='{}.receive'.format(route_prefix), receive.add_view('POST', 'receive', klass=cls,
permission='{}.edit_row'.format(permission_prefix), permission='{}.edit_row'.format(permission_prefix))
renderer='json') config.add_cornice_service(receive)
def defaults(config, **kwargs): def defaults(config, **kwargs):

View file

@ -2,7 +2,7 @@
################################################################################ ################################################################################
# #
# Rattail -- Retail Software Framework # Rattail -- Retail Software Framework
# Copyright © 2010-2021 Lance Edgar # Copyright © 2010-2023 Lance Edgar
# #
# This file is part of Rattail. # This file is part of Rattail.
# #
@ -24,8 +24,9 @@
Tailbone Web API Tailbone Web API
""" """
from __future__ import unicode_literals, absolute_import import simplejson
from cornice.renderer import CorniceRenderer
from pyramid.config import Configurator from pyramid.config import Configurator
from pyramid.authentication import SessionAuthenticationPolicy from pyramid.authentication import SessionAuthenticationPolicy
@ -61,6 +62,12 @@ def make_pyramid_config(settings):
pyramid_config.include('pyramid_tm') pyramid_config.include('pyramid_tm')
pyramid_config.include('cornice') pyramid_config.include('cornice')
# use simplejson to serialize cornice view context; cf.
# https://cornice.readthedocs.io/en/latest/upgrading.html#x-to-5-x
# https://docs.pylonsproject.org/projects/pyramid/en/latest/narr/renderers.html
json_renderer = CorniceRenderer(serializer=simplejson.dumps)
pyramid_config.add_renderer('cornicejson', json_renderer)
# bring in the pyramid_retry logic, if available # bring in the pyramid_retry logic, if available
# TODO: pretty soon we can require this package, hopefully.. # TODO: pretty soon we can require this package, hopefully..
try: try: