Add some API views for receiving, and vendor autocomplete
lots more to do yet, for those...
This commit is contained in:
parent
afdd294c60
commit
3514c4050e
7 changed files with 409 additions and 130 deletions
|
@ -28,26 +28,22 @@ from __future__ import unicode_literals, absolute_import
|
|||
|
||||
import six
|
||||
|
||||
from rattail.time import localtime
|
||||
from rattail.util import load_object
|
||||
|
||||
from cornice import resource
|
||||
|
||||
from tailbone.api import APIMasterView
|
||||
|
||||
|
||||
class BatchAPIMasterView(APIMasterView):
|
||||
class APIBatchMixin(object):
|
||||
"""
|
||||
Base class for all API views which are meant to handle "batch" *and/or*
|
||||
"batch row" data.
|
||||
"""
|
||||
supports_quick_entry = False
|
||||
supports_toggle_complete = False
|
||||
|
||||
def __init__(self, request, **kwargs):
|
||||
super(BatchAPIMasterView, self).__init__(request, **kwargs)
|
||||
self.handler = self.get_handler()
|
||||
|
||||
@classmethod
|
||||
def get_batch_class(cls):
|
||||
model_class = cls.get_model_class()
|
||||
def get_batch_class(self):
|
||||
model_class = self.get_model_class()
|
||||
if hasattr(model_class, '__batch_class__'):
|
||||
return model_class.__batch_class__
|
||||
return model_class
|
||||
|
@ -74,27 +70,75 @@ class BatchAPIMasterView(APIMasterView):
|
|||
default=self.default_handler_spec)
|
||||
return load_object(spec)(self.rattail_config)
|
||||
|
||||
def quick_entry(self):
|
||||
|
||||
class APIBatchView(APIBatchMixin, APIMasterView):
|
||||
"""
|
||||
Base class for all API views which are meant to handle "batch" *and/or*
|
||||
"batch row" data.
|
||||
"""
|
||||
supports_toggle_complete = False
|
||||
|
||||
def __init__(self, request, **kwargs):
|
||||
super(APIBatchView, self).__init__(request, **kwargs)
|
||||
self.handler = self.get_handler()
|
||||
|
||||
def normalize(self, batch):
|
||||
|
||||
created = batch.created
|
||||
created = localtime(self.rattail_config, created, from_utc=True)
|
||||
created = self.pretty_datetime(created)
|
||||
|
||||
executed = batch.executed
|
||||
if executed:
|
||||
executed = localtime(self.rattail_config, executed, from_utc=True)
|
||||
executed = self.pretty_datetime(executed)
|
||||
|
||||
return {
|
||||
'uuid': batch.uuid,
|
||||
'_str': six.text_type(batch),
|
||||
'id': batch.id,
|
||||
'id_str': batch.id_str,
|
||||
'description': batch.description,
|
||||
'notes': batch.notes,
|
||||
'rowcount': batch.rowcount,
|
||||
'created': created,
|
||||
'created_by_uuid': batch.created_by.uuid,
|
||||
'created_by_display': six.text_type(batch.created_by),
|
||||
'complete': batch.complete,
|
||||
'executed': executed,
|
||||
'executed_by_uuid': batch.executed_by_uuid,
|
||||
'executed_by_display': six.text_type(batch.executed_by or ''),
|
||||
}
|
||||
|
||||
def create_object(self, data):
|
||||
"""
|
||||
View for handling "quick entry" user input, for a batch.
|
||||
Create a new object instance and populate it with the given data.
|
||||
|
||||
Here we'll invoke the handler for actual batch creation, instead of
|
||||
typical logic used for simple records.
|
||||
"""
|
||||
data = self.request.json_body
|
||||
kwargs = dict(data)
|
||||
kwargs['user'] = self.request.user
|
||||
batch = self.handler.make_batch(self.Session(), **kwargs)
|
||||
return batch
|
||||
|
||||
uuid = data['batch_uuid']
|
||||
batch = self.Session.query(self.get_batch_class()).get(uuid)
|
||||
if not batch:
|
||||
raise self.notfound()
|
||||
def update_object(self, batch, data):
|
||||
"""
|
||||
Logic for updating a main object record.
|
||||
|
||||
entry = data['quick_entry']
|
||||
Here we want to make sure we set "created by" to the current user, when
|
||||
creating a new batch.
|
||||
"""
|
||||
# we're only concerned with *new* batches here
|
||||
if not batch.uuid:
|
||||
|
||||
try:
|
||||
row = self.handler.quick_entry(self.Session(), batch, entry)
|
||||
except Exception as error:
|
||||
return {'error': six.text_type(error)}
|
||||
# assign creator; initialize row count
|
||||
batch.created_by_uuid = self.request.user.uuid
|
||||
if batch.rowcount is None:
|
||||
batch.rowcount = 0
|
||||
|
||||
result = self._get(obj=row)
|
||||
result['ok'] = True
|
||||
return result
|
||||
# then go ahead with usual logic
|
||||
return super(APIBatchView, self).update_object(batch, data)
|
||||
|
||||
def mark_complete(self):
|
||||
"""
|
||||
|
@ -130,21 +174,24 @@ class BatchAPIMasterView(APIMasterView):
|
|||
batch.complete = False
|
||||
return self._get(obj=batch)
|
||||
|
||||
@classmethod
|
||||
def defaults(cls, config):
|
||||
cls._batch_defaults(config)
|
||||
|
||||
@classmethod
|
||||
def _batch_defaults(cls, config):
|
||||
route_prefix = cls.get_route_prefix()
|
||||
permission_prefix = cls.get_permission_prefix()
|
||||
collection_url_prefix = cls.get_collection_url_prefix()
|
||||
object_url_prefix = cls.get_object_url_prefix()
|
||||
permission_prefix = cls.get_permission_prefix()
|
||||
|
||||
if cls.supports_quick_entry:
|
||||
|
||||
# quick entry
|
||||
config.add_route('{}.quick_entry'.format(route_prefix), '{}/quick-entry'.format(collection_url_prefix),
|
||||
request_method=('OPTIONS', 'POST'))
|
||||
config.add_view(cls, attr='quick_entry', route_name='{}.quick_entry'.format(route_prefix),
|
||||
permission='{}.edit'.format(permission_prefix),
|
||||
renderer='json')
|
||||
# primary / typical API
|
||||
resource.add_view(cls.collection_get, permission='{}.list'.format(permission_prefix))
|
||||
resource.add_view(cls.collection_post, permission='{}.create'.format(permission_prefix))
|
||||
resource.add_view(cls.get, permission='{}.view'.format(permission_prefix))
|
||||
batch_resource = resource.add_resource(cls, collection_path=collection_url_prefix,
|
||||
path='{}/{{uuid}}'.format(object_url_prefix))
|
||||
config.add_cornice_resource(batch_resource)
|
||||
|
||||
if cls.supports_toggle_complete:
|
||||
|
||||
|
@ -160,3 +207,84 @@ class BatchAPIMasterView(APIMasterView):
|
|||
permission='{}.edit'.format(permission_prefix),
|
||||
renderer='json')
|
||||
|
||||
|
||||
# TODO: deprecate / remove this
|
||||
BatchAPIMasterView = APIBatchView
|
||||
|
||||
|
||||
class APIBatchRowView(APIBatchMixin, APIMasterView):
|
||||
"""
|
||||
Base class for all API views which are meant to handle "batch rows" data.
|
||||
"""
|
||||
supports_quick_entry = False
|
||||
|
||||
def __init__(self, request, **kwargs):
|
||||
super(APIBatchRowView, self).__init__(request, **kwargs)
|
||||
self.handler = self.get_handler()
|
||||
|
||||
def normalize(self, row):
|
||||
batch = row.batch
|
||||
return {
|
||||
'uuid': row.uuid,
|
||||
'_str': six.text_type(row),
|
||||
'_parent_str': six.text_type(batch),
|
||||
'_parent_uuid': batch.uuid,
|
||||
'batch_uuid': batch.uuid,
|
||||
'batch_id': batch.id,
|
||||
'batch_id_str': batch.id_str,
|
||||
'batch_description': batch.description,
|
||||
'sequence': row.sequence,
|
||||
'status_code': row.status_code,
|
||||
'status_display': row.STATUS.get(row.status_code, six.text_type(row.status_code)),
|
||||
}
|
||||
|
||||
def quick_entry(self):
|
||||
"""
|
||||
View for handling "quick entry" user input, for a batch.
|
||||
"""
|
||||
data = self.request.json_body
|
||||
|
||||
uuid = data['batch_uuid']
|
||||
batch = self.Session.query(self.get_batch_class()).get(uuid)
|
||||
if not batch:
|
||||
raise self.notfound()
|
||||
|
||||
entry = data['quick_entry']
|
||||
|
||||
try:
|
||||
row = self.handler.quick_entry(self.Session(), batch, entry)
|
||||
except Exception as error:
|
||||
msg = six.text_type(error)
|
||||
if not msg and isinstance(error, NotImplementedError):
|
||||
msg = "Feature is not implemented"
|
||||
return {'error': msg}
|
||||
|
||||
result = self._get(obj=row)
|
||||
result['ok'] = True
|
||||
return result
|
||||
|
||||
@classmethod
|
||||
def defaults(cls, config):
|
||||
cls._batch_row_defaults(config)
|
||||
|
||||
@classmethod
|
||||
def _batch_row_defaults(cls, config):
|
||||
route_prefix = cls.get_route_prefix()
|
||||
permission_prefix = cls.get_permission_prefix()
|
||||
collection_url_prefix = cls.get_collection_url_prefix()
|
||||
object_url_prefix = cls.get_object_url_prefix()
|
||||
|
||||
resource.add_view(cls.collection_get, permission='{}.view'.format(permission_prefix))
|
||||
resource.add_view(cls.get, permission='{}.view'.format(permission_prefix))
|
||||
rows_resource = resource.add_resource(cls, collection_path=collection_url_prefix,
|
||||
path='{}/{{uuid}}'.format(object_url_prefix))
|
||||
config.add_cornice_resource(rows_resource)
|
||||
|
||||
if cls.supports_quick_entry:
|
||||
|
||||
# quick entry
|
||||
config.add_route('{}.quick_entry'.format(route_prefix), '{}/quick-entry'.format(collection_url_prefix),
|
||||
request_method=('OPTIONS', 'POST'))
|
||||
config.add_view(cls, attr='quick_entry', route_name='{}.quick_entry'.format(route_prefix),
|
||||
permission='{}.edit'.format(permission_prefix),
|
||||
renderer='json')
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue