Refactor mobile inventory to use colander/deform

This commit is contained in:
Lance Edgar 2018-02-10 14:07:16 -06:00
parent a65235c0fd
commit cff757fe9e

View file

@ -27,7 +27,6 @@ Views for inventory batches
from __future__ import unicode_literals, absolute_import from __future__ import unicode_literals, absolute_import
import re import re
import decimal
import six import six
@ -38,11 +37,10 @@ from rattail.gpc import GPC
from rattail.util import pretty_quantity from rattail.util import pretty_quantity
import colander import colander
import formencode as fe
from deform import widget as dfwidget from deform import widget as dfwidget
from webhelpers2.html import HTML, tags from webhelpers2.html import HTML, tags
from tailbone import forms, forms2, grids from tailbone import forms2 as forms, grids
from tailbone.views import MasterView from tailbone.views import MasterView
from tailbone.views.batch import BatchMasterView from tailbone.views.batch import BatchMasterView
@ -268,7 +266,7 @@ class InventoryBatchView(BatchMasterView):
modes = self.get_available_modes() modes = self.get_available_modes()
f.set_enum('mode', modes) f.set_enum('mode', modes)
mode_values = [(k, v) for k, v in sorted(modes.items())] mode_values = [(k, v) for k, v in sorted(modes.items())]
f.set_widget('mode', forms2.widgets.PlainSelectWidget(values=mode_values)) f.set_widget('mode', forms.widgets.PlainSelectWidget(values=mode_values))
# complete # complete
if self.creating or batch.executed or not batch.complete: if self.creating or batch.executed or not batch.complete:
@ -364,11 +362,11 @@ class InventoryBatchView(BatchMasterView):
} }
if self.request.has_perm('{}.edit_row'.format(self.get_permission_prefix())): if self.request.has_perm('{}.edit_row'.format(self.get_permission_prefix())):
update_form = forms.SimpleForm(self.request, schema=InventoryForm) update_form = forms.Form(schema=InventoryForm(), request=self.request)
if update_form.validate(): if update_form.validate(newstyle=True):
row = update_form.data['row'] row = update_form.validated['row']
cases = update_form.data['cases'] cases = update_form.validated['cases']
units = update_form.data['units'] units = update_form.validated['units']
if cases: if cases:
row.cases = cases row.cases = cases
row.units = None row.units = None
@ -480,31 +478,23 @@ class InventoryBatchView(BatchMasterView):
"Create new {} with 'zero' mode".format(model_title)) "Create new {} with 'zero' mode".format(model_title))
class ValidBatchRow(forms.validators.ModelValidator): class InventoryBatchRowType(forms.types.ObjectType):
model_class = model.InventoryBatchRow model_class = model.InventoryBatchRow
def _to_python(self, value, state): def deserialize(self, node, cstruct):
row = super(ValidBatchRow, self)._to_python(value, state) row = super(InventoryBatchRowType, 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 Decimal(fe.validators.Number): class InventoryForm(colander.MappingSchema):
def _to_python(self, value, state): row = colander.SchemaNode(InventoryBatchRowType())
try:
return decimal.Decimal(value)
except ValueError:
raise Invalid(self.message('number', state), value, state)
cases = colander.SchemaNode(colander.Decimal(), missing=colander.null)
class InventoryForm(forms.Schema): units = colander.SchemaNode(colander.Decimal(), missing=colander.null)
allow_extra_fields = True
filter_extra_fields = True
row = ValidBatchRow()
cases = Decimal()
units = Decimal()
def includeme(config): def includeme(config):