Add initial API views for inventory batches

This commit is contained in:
Lance Edgar 2020-03-29 16:31:16 -05:00
parent e9fc9ccbf7
commit 0fbc8c9247
2 changed files with 217 additions and 9 deletions

View file

@ -26,6 +26,8 @@ Tailbone Web API - Batch Views
from __future__ import unicode_literals, absolute_import
import logging
import six
from rattail.time import localtime
@ -36,6 +38,9 @@ from cornice import resource, Service
from tailbone.api import APIMasterView2 as APIMasterView
log = logging.getLogger(__name__)
class APIBatchMixin(object):
"""
Base class for all API views which are meant to handle "batch" *and/or*
@ -85,14 +90,11 @@ class APIBatchView(APIBatchMixin, APIMasterView):
def normalize(self, batch):
created = batch.created
created = localtime(self.rattail_config, created, from_utc=True)
created = self.pretty_datetime(created)
created = localtime(self.rattail_config, batch.created, from_utc=True)
executed = batch.executed
if executed:
executed = localtime(self.rattail_config, executed, from_utc=True)
executed = self.pretty_datetime(executed)
executed = None
if batch.executed:
executed = localtime(self.rattail_config, batch.executed, from_utc=True)
return {
'uuid': batch.uuid,
@ -103,14 +105,16 @@ class APIBatchView(APIBatchMixin, APIMasterView):
'notes': batch.notes,
'params': batch.params or {},
'rowcount': batch.rowcount,
'created': created,
'created': six.text_type(created),
'created_display': self.pretty_datetime(created),
'created_by_uuid': batch.created_by.uuid,
'created_by_display': six.text_type(batch.created_by),
'complete': batch.complete,
'status_code': batch.status_code,
'status_display': batch.STATUS.get(batch.status_code,
six.text_type(batch.status_code)),
'executed': executed,
'executed': six.text_type(executed) if executed else None,
'executed_display': self.pretty_datetime(executed) if executed else None,
'executed_by_uuid': batch.executed_by_uuid,
'executed_by_display': six.text_type(batch.executed_by or ''),
}
@ -269,6 +273,28 @@ class APIBatchRowView(APIBatchMixin, APIMasterView):
'status_display': row.STATUS.get(row.status_code, six.text_type(row.status_code)),
}
def update_object(self, row, data):
"""
Supplements the default logic as follows:
Invokes the batch handler's ``refresh_row()`` method after updating the
row's field data per usual.
"""
# update row per usual
row = super(APIBatchRowView, self).update_object(row, data)
# okay now we apply handler refresh logic
self.handler.refresh_row(row)
return row
def delete_object(self, row):
"""
Overrides the default logic as follows:
Delegates deletion of the row to the batch handler.
"""
self.handler.do_remove_row(row)
def quick_entry(self):
"""
View for handling "quick entry" user input, for a batch.
@ -285,6 +311,9 @@ class APIBatchRowView(APIBatchMixin, APIMasterView):
try:
row = self.handler.quick_entry(self.Session(), batch, entry)
except Exception as error:
log.warning("quick entry failed for '%s' batch %s: %s",
self.handler.batch_key, batch.id_str, entry,
exc_info=True)
msg = six.text_type(error)
if not msg and isinstance(error, NotImplementedError):
msg = "Feature is not implemented"