Add support for making new product on-the-fly during mobile ordering

let's face it, that will be necessary sometimes.  this feature still needs some
work before can be called complete though...
This commit is contained in:
Lance Edgar 2018-03-06 19:29:15 -06:00
parent 6ec0ddb94e
commit 652f51d484
7 changed files with 83 additions and 10 deletions

View file

@ -1045,6 +1045,7 @@ class MasterView(View):
"""
defaults = {
'request': self.request,
'mobile': True,
'readonly': self.viewing,
'model_class': getattr(self, 'model_row_class', None),
'action_url': self.request.current_route_url(_query=None),

View file

@ -47,6 +47,7 @@ class PurchasingBatchView(BatchMasterView):
model_class = model.PurchaseBatch
model_row_class = model.PurchaseBatchRow
default_handler_spec = 'rattail.batch.purchase:PurchaseBatchHandler'
supports_new_product = False
grid_columns = [
'id',
@ -701,6 +702,42 @@ class PurchasingBatchView(BatchMasterView):
# else:
# f.remove_field('product')
def mobile_new_product(self):
"""
View which allows user to create a new Product and add a row for it to
the Purchasing Batch.
"""
batch = self.get_instance()
batch_url = self.get_action_url('view', batch, mobile=True)
form = forms.Form(schema=self.make_new_product_schema(),
request=self.request,
mobile=True,
cancel_url=batch_url)
if form.validate(newstyle=True):
product = model.Product()
product.item_id = form.validated['item_id']
product.description = form.validated['description']
row = self.model_row_class()
row.product = product
self.handler.add_row(batch, row)
self.Session.flush()
return self.redirect(self.get_row_action_url('edit', row, mobile=True))
return self.render_to_response('new_product', {
'form': form,
'dform': form.make_deform_form(),
'instance_title': self.get_instance_title(batch),
'instance_url': batch_url,
}, mobile=True)
def make_new_product_schema(self):
"""
Must return a ``colander.Schema`` instance for use with the form in the
:meth:`mobile_new_product()` view.
"""
return NewProduct()
# def item_lookup(self, value, field=None):
# """
# Try to locate a single product using ``value`` as a lookup code.
@ -785,8 +822,24 @@ class PurchasingBatchView(BatchMasterView):
config.add_view(cls, attr='eligible_purchases', route_name='{}.eligible_purchases'.format(route_prefix),
renderer='json', permission='{}.view'.format(permission_prefix))
# add new product
if cls.supports_new_product:
config.add_tailbone_permission(permission_prefix, '{}.new_product'.format(permission_prefix),
"Create new Product when adding row to {}".format(model_title))
config.add_route('mobile.{}.new_product'.format(route_prefix), '{}/{{{}}}/new-product'.format(url_prefix, model_key))
config.add_view(cls, attr='mobile_new_product', route_name='mobile.{}.new_product'.format(route_prefix),
permission='{}.new_product'.format(permission_prefix))
@classmethod
def defaults(cls, config):
cls._purchasing_defaults(config)
cls._batch_defaults(config)
cls._defaults(config)
class NewProduct(colander.Schema):
item_id = colander.SchemaNode(colander.String())
description = colander.SchemaNode(colander.String())