Refactor mobile receiving to use colander/deform

This commit is contained in:
Lance Edgar 2018-02-10 14:00:28 -06:00
parent ec275b2fe0
commit a65235c0fd
2 changed files with 35 additions and 27 deletions

View file

@ -36,8 +36,7 @@ from deform import widget as dfwidget
from pyramid import httpexceptions from pyramid import httpexceptions
from webhelpers2.html import tags from webhelpers2.html import tags
# from tailbone import forms from tailbone import forms2 as forms
from tailbone import forms2
from tailbone.views.batch import BatchMasterView from tailbone.views.batch import BatchMasterView
@ -251,7 +250,7 @@ class PurchasingBatchView(BatchMasterView):
if vendor: if vendor:
vendor_display = six.text_type(vendor) vendor_display = six.text_type(vendor)
vendors_url = self.request.route_url('vendors.autocomplete') vendors_url = self.request.route_url('vendors.autocomplete')
f.set_widget('vendor_uuid', forms2.widgets.JQueryAutocompleteWidget( f.set_widget('vendor_uuid', forms.widgets.JQueryAutocompleteWidget(
field_display=vendor_display, service_url=vendors_url)) field_display=vendor_display, service_url=vendors_url))
f.set_label('vendor_uuid', "Vendor") f.set_label('vendor_uuid', "Vendor")
elif self.editing: elif self.editing:
@ -288,7 +287,7 @@ class PurchasingBatchView(BatchMasterView):
elif self.editing: elif self.editing:
buyer_display = six.text_type(batch.buyer or '') buyer_display = six.text_type(batch.buyer or '')
buyers_url = self.request.route_url('employees.autocomplete') buyers_url = self.request.route_url('employees.autocomplete')
f.set_widget('buyer_uuid', forms2.widgets.JQueryAutocompleteWidget( f.set_widget('buyer_uuid', forms.widgets.JQueryAutocompleteWidget(
field_display=buyer_display, service_url=buyers_url)) field_display=buyer_display, service_url=buyers_url))
f.set_label('buyer_uuid', "Buyer") f.set_label('buyer_uuid', "Buyer")

View file

@ -35,10 +35,11 @@ from rattail.db import model, api
from rattail.gpc import GPC from rattail.gpc import GPC
from rattail.util import pretty_quantity, prettify from rattail.util import pretty_quantity, prettify
import colander
import formencode as fe import formencode as fe
from webhelpers2.html import tags from webhelpers2.html import tags
from tailbone import forms, grids from tailbone import forms2 as forms, grids
from tailbone.views.purchasing import PurchasingBatchView from tailbone.views.purchasing import PurchasingBatchView
@ -289,12 +290,12 @@ class ReceivingBatchView(PurchasingBatchView):
} }
if self.request.has_perm('{}.create_row'.format(self.get_permission_prefix())): if self.request.has_perm('{}.create_row'.format(self.get_permission_prefix())):
update_form = forms.SimpleForm(self.request, schema=ReceivingForm) update_form = forms.Form(schema=ReceivingForm(), request=self.request)
if update_form.validate(): if update_form.validate(newstyle=True):
row = update_form.data['row'] row = update_form.validated['row']
mode = update_form.data['mode'] mode = update_form.validated['mode']
cases = update_form.data['cases'] cases = update_form.validated['cases']
units = update_form.data['units'] units = update_form.validated['units']
if cases: if cases:
setattr(row, 'cases_{}'.format(mode), setattr(row, 'cases_{}'.format(mode),
(getattr(row, 'cases_{}'.format(mode)) or 0) + cases) (getattr(row, 'cases_{}'.format(mode)) or 0) + cases)
@ -305,7 +306,7 @@ class ReceivingBatchView(PurchasingBatchView):
# if mode in ('damaged', 'expired', 'mispick'): # if mode in ('damaged', 'expired', 'mispick'):
if mode in ('damaged', 'expired'): if mode in ('damaged', 'expired'):
self.attach_credit(row, mode, cases, units, self.attach_credit(row, mode, cases, units,
expiration_date=update_form.data['expiration_date'], expiration_date=update_form.validated['expiration_date'],
# discarded=update_form.data['trash'], # discarded=update_form.data['trash'],
# mispick_product=shipped_product) # mispick_product=shipped_product)
) )
@ -376,32 +377,40 @@ class ReceivingBatchView(PurchasingBatchView):
cls._defaults(config) cls._defaults(config)
class ValidBatchRow(forms.validators.ModelValidator): class PurchaseBatchRowType(forms.types.ObjectType):
model_class = model.PurchaseBatchRow model_class = model.PurchaseBatchRow
def _to_python(self, value, state): def deserialize(self, node, cstruct):
row = super(ValidBatchRow, self)._to_python(value, state) row = super(PurchaseBatchRowType, self).deserialize(node, cstruct)
if row.batch.executed: if row and row.batch.executed:
raise fe.Invalid("Batch has already been executed", value, state) raise colander.Invalid(node, "Batch has already been executed")
return row return row
class ReceivingForm(forms.Schema): class ReceivingForm(colander.MappingSchema):
allow_extra_fields = True
filter_extra_fields = True row = colander.SchemaNode(PurchaseBatchRowType())
row = ValidBatchRow()
mode = fe.validators.OneOf(['received', 'damaged', 'expired', mode = colander.SchemaNode(colander.String(),
# 'mispick', validator=colander.OneOf(['received',
]) 'damaged',
'expired',
# 'mispick',
]))
# product = forms.validators.ValidProduct() # product = forms.validators.ValidProduct()
# upc = forms.validators.ValidGPC() # upc = forms.validators.ValidGPC()
# brand_name = fe.validators.String() # brand_name = fe.validators.String()
# description = fe.validators.String() # description = fe.validators.String()
# size = fe.validators.String() # size = fe.validators.String()
# case_quantity = fe.validators.Number() # case_quantity = fe.validators.Number()
cases = fe.validators.Number()
units = fe.validators.Number() cases = colander.SchemaNode(colander.Decimal(), missing=colander.null)
expiration_date = fe.validators.DateValidator()
units = colander.SchemaNode(colander.Decimal(), missing=colander.null)
expiration_date = colander.SchemaNode(colander.Date(), missing=colander.null)
# trash = fe.validators.Bool() # trash = fe.validators.Bool()
# ordered_product = forms.validators.ValidProduct() # ordered_product = forms.validators.ValidProduct()