Refactor mobile inventory to use colander/deform
This commit is contained in:
parent
a65235c0fd
commit
cff757fe9e
|
@ -27,7 +27,6 @@ Views for inventory batches
|
|||
from __future__ import unicode_literals, absolute_import
|
||||
|
||||
import re
|
||||
import decimal
|
||||
|
||||
import six
|
||||
|
||||
|
@ -38,11 +37,10 @@ from rattail.gpc import GPC
|
|||
from rattail.util import pretty_quantity
|
||||
|
||||
import colander
|
||||
import formencode as fe
|
||||
from deform import widget as dfwidget
|
||||
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.batch import BatchMasterView
|
||||
|
||||
|
@ -268,7 +266,7 @@ class InventoryBatchView(BatchMasterView):
|
|||
modes = self.get_available_modes()
|
||||
f.set_enum('mode', modes)
|
||||
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
|
||||
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())):
|
||||
update_form = forms.SimpleForm(self.request, schema=InventoryForm)
|
||||
if update_form.validate():
|
||||
row = update_form.data['row']
|
||||
cases = update_form.data['cases']
|
||||
units = update_form.data['units']
|
||||
update_form = forms.Form(schema=InventoryForm(), request=self.request)
|
||||
if update_form.validate(newstyle=True):
|
||||
row = update_form.validated['row']
|
||||
cases = update_form.validated['cases']
|
||||
units = update_form.validated['units']
|
||||
if cases:
|
||||
row.cases = cases
|
||||
row.units = None
|
||||
|
@ -480,31 +478,23 @@ class InventoryBatchView(BatchMasterView):
|
|||
"Create new {} with 'zero' mode".format(model_title))
|
||||
|
||||
|
||||
class ValidBatchRow(forms.validators.ModelValidator):
|
||||
class InventoryBatchRowType(forms.types.ObjectType):
|
||||
model_class = model.InventoryBatchRow
|
||||
|
||||
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(InventoryBatchRowType, self).deserialize(node, cstruct)
|
||||
if row and row.batch.executed:
|
||||
raise colander.Invalid(node, "Batch has already been executed")
|
||||
return row
|
||||
|
||||
|
||||
class Decimal(fe.validators.Number):
|
||||
class InventoryForm(colander.MappingSchema):
|
||||
|
||||
def _to_python(self, value, state):
|
||||
try:
|
||||
return decimal.Decimal(value)
|
||||
except ValueError:
|
||||
raise Invalid(self.message('number', state), value, state)
|
||||
row = colander.SchemaNode(InventoryBatchRowType())
|
||||
|
||||
cases = colander.SchemaNode(colander.Decimal(), missing=colander.null)
|
||||
|
||||
class InventoryForm(forms.Schema):
|
||||
allow_extra_fields = True
|
||||
filter_extra_fields = True
|
||||
row = ValidBatchRow()
|
||||
cases = Decimal()
|
||||
units = Decimal()
|
||||
units = colander.SchemaNode(colander.Decimal(), missing=colander.null)
|
||||
|
||||
|
||||
def includeme(config):
|
||||
|
|
Loading…
Reference in a new issue