Add basic "receive" handler logic for receiving API
This commit is contained in:
parent
337422a619
commit
0715bd6321
4 changed files with 150 additions and 44 deletions
|
@ -26,13 +26,23 @@ Tailbone Web API - Receiving Batches
|
|||
|
||||
from __future__ import unicode_literals, absolute_import
|
||||
|
||||
import logging
|
||||
|
||||
import six
|
||||
import humanize
|
||||
|
||||
from rattail import pod
|
||||
from rattail.db import model
|
||||
from rattail.time import make_utc
|
||||
|
||||
from deform import widget as dfwidget
|
||||
|
||||
from tailbone import forms
|
||||
from tailbone.api.batch import APIBatchView, APIBatchRowView
|
||||
from tailbone.forms.receiving import ReceiveRow
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class ReceivingBatchViews(APIBatchView):
|
||||
|
@ -244,10 +254,19 @@ class ReceivingBatchRowViews(APIBatchRowView):
|
|||
data['size'] = row.size
|
||||
data['full_description'] = row.product.full_description if row.product else row.description
|
||||
|
||||
# only provide image url if so configured
|
||||
if self.rattail_config.getbool('rattail.batch', 'purchase.mobile_images', default=True):
|
||||
data['image_url'] = pod.get_image_url(self.rattail_config, row.upc) if row.upc else None
|
||||
|
||||
# unit_uom can vary by product
|
||||
data['unit_uom'] = 'LB' if row.product and row.product.weighed else 'EA'
|
||||
|
||||
data['case_quantity'] = row.case_quantity
|
||||
data['unit_uom'] = 'EA' # TODO
|
||||
data['order_quantities_known'] = batch.order_quantities_known
|
||||
|
||||
data['cases_ordered'] = row.cases_ordered
|
||||
data['units_ordered'] = row.units_ordered
|
||||
|
||||
data['cases_shipped'] = row.cases_shipped
|
||||
data['units_shipped'] = row.units_shipped
|
||||
|
||||
|
@ -294,6 +313,51 @@ class ReceivingBatchRowViews(APIBatchRowView):
|
|||
def get(self):
|
||||
return self._get()
|
||||
|
||||
def receive(self):
|
||||
"""
|
||||
View which handles "receiving" against a particular batch row.
|
||||
"""
|
||||
# first do basic input validation
|
||||
schema = ReceiveRow().bind(session=self.Session())
|
||||
form = forms.Form(schema=schema, request=self.request)
|
||||
# TODO: this seems hacky, but avoids "complex" date value parsing
|
||||
form.set_widget('expiration_date', dfwidget.TextInputWidget())
|
||||
if not form.validate(newstyle=True):
|
||||
log.debug("form did not validate: %s",
|
||||
form.make_deform_form().error)
|
||||
return {'error': "Form did not validate"}
|
||||
|
||||
# fetch / validate row object
|
||||
row = self.Session.query(model.PurchaseBatchRow).get(form.validated['row'])
|
||||
if row is not self.get_object():
|
||||
return {'error': "Specified row does not match the route!"}
|
||||
|
||||
# handler takes care of the row receiving logic for us
|
||||
kwargs = dict(form.validated)
|
||||
del kwargs['row']
|
||||
self.handler.receive_row(row, **kwargs)
|
||||
|
||||
self.Session.flush()
|
||||
return self._get(obj=row)
|
||||
|
||||
@classmethod
|
||||
def defaults(cls, config):
|
||||
cls._batch_row_defaults(config)
|
||||
cls._receiving_batch_row_defaults(config)
|
||||
|
||||
@classmethod
|
||||
def _receiving_batch_row_defaults(cls, config):
|
||||
route_prefix = cls.get_route_prefix()
|
||||
permission_prefix = cls.get_permission_prefix()
|
||||
object_url_prefix = cls.get_object_url_prefix()
|
||||
|
||||
# receive (row)
|
||||
config.add_route('{}.receive'.format(route_prefix), '{}/{{uuid}}/receive'.format(object_url_prefix),
|
||||
request_method=('OPTIONS', 'POST'))
|
||||
config.add_view(cls, attr='receive', route_name='{}.receive'.format(route_prefix),
|
||||
permission='{}.edit_row'.format(permission_prefix),
|
||||
renderer='json')
|
||||
|
||||
|
||||
def includeme(config):
|
||||
ReceivingBatchViews.defaults(config)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue