From 7b43164831af0700ac37a2d25f929b0f174ec396 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 26 Feb 2020 21:29:59 -0600 Subject: [PATCH] Add support for executing ordering batches via API --- tailbone/api/batch/core.py | 28 ++++++++++++++++++++++++++++ tailbone/api/batch/ordering.py | 6 ++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/tailbone/api/batch/core.py b/tailbone/api/batch/core.py index 1ceab308..1f725641 100644 --- a/tailbone/api/batch/core.py +++ b/tailbone/api/batch/core.py @@ -77,6 +77,7 @@ class APIBatchView(APIBatchMixin, APIMasterView): "batch row" data. """ supports_toggle_complete = False + supports_execute = False def __init__(self, request, **kwargs): super(APIBatchView, self).__init__(request, **kwargs) @@ -178,6 +179,22 @@ class APIBatchView(APIBatchMixin, APIMasterView): batch.complete = False 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 def defaults(cls, config): cls._batch_defaults(config) @@ -211,6 +228,15 @@ class APIBatchView(APIBatchMixin, APIMasterView): permission='{}.edit'.format(permission_prefix), 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 BatchAPIMasterView = APIBatchView @@ -238,6 +264,8 @@ class APIBatchRowView(APIBatchMixin, APIMasterView): 'batch_id': batch.id, 'batch_id_str': batch.id_str, 'batch_description': batch.description, + 'batch_complete': batch.complete, + 'batch_executed': bool(batch.executed), 'sequence': row.sequence, 'status_code': row.status_code, 'status_display': row.STATUS.get(row.status_code, six.text_type(row.status_code)), diff --git a/tailbone/api/batch/ordering.py b/tailbone/api/batch/ordering.py index 926a3d70..de8fde0b 100644 --- a/tailbone/api/batch/ordering.py +++ b/tailbone/api/batch/ordering.py @@ -46,6 +46,7 @@ class OrderingBatchViews(APIBatchView): collection_url_prefix = '/ordering-batches' object_url_prefix = '/ordering-batch' supports_toggle_complete = True + supports_execute = True def base_query(self): """ @@ -67,8 +68,9 @@ class OrderingBatchViews(APIBatchView): data['department_uuid'] = batch.department_uuid 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 def create_object(self, data):