Add support for executing ordering batches via API
This commit is contained in:
parent
c145d077cd
commit
7b43164831
|
@ -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)),
|
||||
|
|
|
@ -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):
|
||||
|
|
Loading…
Reference in a new issue