Add support for executing ordering batches via API

This commit is contained in:
Lance Edgar 2020-02-26 21:29:59 -06:00
parent c145d077cd
commit 7b43164831
2 changed files with 32 additions and 2 deletions

View file

@ -77,6 +77,7 @@ class APIBatchView(APIBatchMixin, APIMasterView):
"batch row" data. "batch row" data.
""" """
supports_toggle_complete = False supports_toggle_complete = False
supports_execute = False
def __init__(self, request, **kwargs): def __init__(self, request, **kwargs):
super(APIBatchView, self).__init__(request, **kwargs) super(APIBatchView, self).__init__(request, **kwargs)
@ -178,6 +179,22 @@ class APIBatchView(APIBatchMixin, APIMasterView):
batch.complete = False batch.complete = False
return self._get(obj=batch) return self._get(obj=batch)
def execute(self):
"""
Execute the given batch.
"""
batch = self.get_object()
if batch.executed:
return {'error': "Batch {} has already been executed: {}".format(
batch.id_str, batch.description)}
kwargs = dict(self.request.json_body)
kwargs.pop('user', None)
kwargs.pop('progress', None)
result = self.handler.do_execute(batch, self.request.user, **kwargs)
return {'ok': bool(result), 'batch': self.normalize(batch)}
@classmethod @classmethod
def defaults(cls, config): def defaults(cls, config):
cls._batch_defaults(config) cls._batch_defaults(config)
@ -211,6 +228,15 @@ class APIBatchView(APIBatchMixin, APIMasterView):
permission='{}.edit'.format(permission_prefix), permission='{}.edit'.format(permission_prefix),
renderer='json') renderer='json')
if cls.supports_execute:
# execute
config.add_route('{}.execute'.format(route_prefix), '{}/{{uuid}}/execute'.format(object_url_prefix),
request_method=('OPTIONS', 'POST'))
config.add_view(cls, attr='execute', route_name='{}.execute'.format(route_prefix),
permission='{}.execute'.format(permission_prefix),
renderer='json')
# TODO: deprecate / remove this # TODO: deprecate / remove this
BatchAPIMasterView = APIBatchView BatchAPIMasterView = APIBatchView
@ -238,6 +264,8 @@ class APIBatchRowView(APIBatchMixin, APIMasterView):
'batch_id': batch.id, 'batch_id': batch.id,
'batch_id_str': batch.id_str, 'batch_id_str': batch.id_str,
'batch_description': batch.description, 'batch_description': batch.description,
'batch_complete': batch.complete,
'batch_executed': bool(batch.executed),
'sequence': row.sequence, 'sequence': row.sequence,
'status_code': row.status_code, 'status_code': row.status_code,
'status_display': row.STATUS.get(row.status_code, six.text_type(row.status_code)), 'status_display': row.STATUS.get(row.status_code, six.text_type(row.status_code)),

View file

@ -46,6 +46,7 @@ class OrderingBatchViews(APIBatchView):
collection_url_prefix = '/ordering-batches' collection_url_prefix = '/ordering-batches'
object_url_prefix = '/ordering-batch' object_url_prefix = '/ordering-batch'
supports_toggle_complete = True supports_toggle_complete = True
supports_execute = True
def base_query(self): def base_query(self):
""" """
@ -67,8 +68,9 @@ class OrderingBatchViews(APIBatchView):
data['department_uuid'] = batch.department_uuid data['department_uuid'] = batch.department_uuid
data['department_display'] = six.text_type(batch.department) if batch.department else None data['department_display'] = six.text_type(batch.department) if batch.department else None
data['po_total_calculated_display'] = "${:0.2f}".format(batch.po_total_calculated) if batch.po_total_calculated is not None else None data['po_total_calculated_display'] = "${:0.2f}".format(batch.po_total_calculated or 0)
data['ship_method'] = batch.ship_method
data['notes_to_vendor'] = batch.notes_to_vendor
return data return data
def create_object(self, data): def create_object(self, data):