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

@ -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',
# 'mispick',
])
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()