Refactor mobile receiving to use colander/deform
This commit is contained in:
parent
ec275b2fe0
commit
a65235c0fd
|
@ -36,8 +36,7 @@ from deform import widget as dfwidget
|
|||
from pyramid import httpexceptions
|
||||
from webhelpers2.html import tags
|
||||
|
||||
# from tailbone import forms
|
||||
from tailbone import forms2
|
||||
from tailbone import forms2 as forms
|
||||
from tailbone.views.batch import BatchMasterView
|
||||
|
||||
|
||||
|
@ -251,7 +250,7 @@ class PurchasingBatchView(BatchMasterView):
|
|||
if vendor:
|
||||
vendor_display = six.text_type(vendor)
|
||||
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))
|
||||
f.set_label('vendor_uuid', "Vendor")
|
||||
elif self.editing:
|
||||
|
@ -288,7 +287,7 @@ class PurchasingBatchView(BatchMasterView):
|
|||
elif self.editing:
|
||||
buyer_display = six.text_type(batch.buyer or '')
|
||||
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))
|
||||
f.set_label('buyer_uuid', "Buyer")
|
||||
|
||||
|
|
|
@ -35,10 +35,11 @@ from rattail.db import model, api
|
|||
from rattail.gpc import GPC
|
||||
from rattail.util import pretty_quantity, prettify
|
||||
|
||||
import colander
|
||||
import formencode as fe
|
||||
from webhelpers2.html import tags
|
||||
|
||||
from tailbone import forms, grids
|
||||
from tailbone import forms2 as forms, grids
|
||||
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())):
|
||||
update_form = forms.SimpleForm(self.request, schema=ReceivingForm)
|
||||
if update_form.validate():
|
||||
row = update_form.data['row']
|
||||
mode = update_form.data['mode']
|
||||
cases = update_form.data['cases']
|
||||
units = update_form.data['units']
|
||||
update_form = forms.Form(schema=ReceivingForm(), request=self.request)
|
||||
if update_form.validate(newstyle=True):
|
||||
row = update_form.validated['row']
|
||||
mode = update_form.validated['mode']
|
||||
cases = update_form.validated['cases']
|
||||
units = update_form.validated['units']
|
||||
if cases:
|
||||
setattr(row, 'cases_{}'.format(mode),
|
||||
(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'):
|
||||
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'],
|
||||
# mispick_product=shipped_product)
|
||||
)
|
||||
|
@ -376,32 +377,40 @@ class ReceivingBatchView(PurchasingBatchView):
|
|||
cls._defaults(config)
|
||||
|
||||
|
||||
class ValidBatchRow(forms.validators.ModelValidator):
|
||||
class PurchaseBatchRowType(forms.types.ObjectType):
|
||||
model_class = model.PurchaseBatchRow
|
||||
|
||||
def _to_python(self, value, state):
|
||||
row = super(ValidBatchRow, self)._to_python(value, state)
|
||||
if row.batch.executed:
|
||||
raise fe.Invalid("Batch has already been executed", value, state)
|
||||
def deserialize(self, node, cstruct):
|
||||
row = super(PurchaseBatchRowType, self).deserialize(node, cstruct)
|
||||
if row and row.batch.executed:
|
||||
raise colander.Invalid(node, "Batch has already been executed")
|
||||
return row
|
||||
|
||||
|
||||
class ReceivingForm(forms.Schema):
|
||||
allow_extra_fields = True
|
||||
filter_extra_fields = True
|
||||
row = ValidBatchRow()
|
||||
mode = fe.validators.OneOf(['received', 'damaged', 'expired',
|
||||
class ReceivingForm(colander.MappingSchema):
|
||||
|
||||
row = colander.SchemaNode(PurchaseBatchRowType())
|
||||
|
||||
mode = colander.SchemaNode(colander.String(),
|
||||
validator=colander.OneOf(['received',
|
||||
'damaged',
|
||||
'expired',
|
||||
# 'mispick',
|
||||
])
|
||||
]))
|
||||
|
||||
# product = forms.validators.ValidProduct()
|
||||
# upc = forms.validators.ValidGPC()
|
||||
# brand_name = fe.validators.String()
|
||||
# description = fe.validators.String()
|
||||
# size = fe.validators.String()
|
||||
# case_quantity = fe.validators.Number()
|
||||
cases = fe.validators.Number()
|
||||
units = fe.validators.Number()
|
||||
expiration_date = fe.validators.DateValidator()
|
||||
|
||||
cases = colander.SchemaNode(colander.Decimal(), missing=colander.null)
|
||||
|
||||
units = colander.SchemaNode(colander.Decimal(), missing=colander.null)
|
||||
|
||||
expiration_date = colander.SchemaNode(colander.Date(), missing=colander.null)
|
||||
|
||||
# trash = fe.validators.Bool()
|
||||
# ordered_product = forms.validators.ValidProduct()
|
||||
|
||||
|
|
Loading…
Reference in a new issue