Add "min % diff" option for pricing batch from products query

refactor the "batch from query" a bit also, to allow for multiple batch type
options which represent the same underlying batch type.  (thought i needed
that, then realized i didn't, but seems safe to include.)
This commit is contained in:
Lance Edgar 2018-11-25 20:14:49 -06:00
parent d9e5eff23d
commit 3b54ab3e0b
3 changed files with 56 additions and 35 deletions

View file

@ -1,7 +1,7 @@
## -*- coding: utf-8; -*- ## -*- coding: utf-8; -*-
<%inherit file="/base.mako" /> <%inherit file="/base.mako" />
<%def name="title()">Products: Create Batch</%def> <%def name="title()">Create Batch</%def>
<%def name="context_menu_items()"> <%def name="context_menu_items()">
<li>${h.link_to("Back to Products", url('products'))}</li> <li>${h.link_to("Back to Products", url('products'))}</li>

View file

@ -49,6 +49,11 @@ class PricingBatchView(BatchMasterView):
rows_editable = True rows_editable = True
rows_bulk_deletable = True rows_bulk_deletable = True
labels = {
'min_diff_threshold': "Min $ Diff",
'min_diff_percent': "Min % Diff",
}
grid_columns = [ grid_columns = [
'id', 'id',
'description', 'description',
@ -65,6 +70,7 @@ class PricingBatchView(BatchMasterView):
'id', 'id',
'description', 'description',
'min_diff_threshold', 'min_diff_threshold',
'min_diff_percent',
'calculate_for_manual', 'calculate_for_manual',
'notes', 'notes',
'created', 'created',
@ -124,6 +130,11 @@ class PricingBatchView(BatchMasterView):
'status_text', 'status_text',
] ]
def configure_form(self, f):
super(PricingBatchView, self).configure_form(f)
f.set_type('min_diff_threshold', 'currency')
def configure_row_grid(self, g): def configure_row_grid(self, g):
super(PricingBatchView, self).configure_row_grid(g) super(PricingBatchView, self).configure_row_grid(g)

View file

@ -954,11 +954,13 @@ class ProductsView(MasterView):
""" """
supported = self.get_supported_batches() supported = self.get_supported_batches()
batch_options = [] batch_options = []
for key, spec in list(supported.items()): for key, info in list(supported.items()):
handler = load_object(spec)(self.rattail_config) handler = load_object(info['spec'])(self.rattail_config)
handler.spec = spec handler.spec = info['spec']
handler.option_key = key
handler.option_title = info.get('title', handler.get_model_title())
supported[key] = handler supported[key] = handler
batch_options.append((key, handler.get_model_title())) batch_options.append((key, handler.option_title))
schema = colander.SchemaNode( schema = colander.SchemaNode(
colander.Mapping(), colander.Mapping(),
@ -983,41 +985,44 @@ class ProductsView(MasterView):
params_forms[key] = forms.Form(schema=schema, request=self.request) params_forms[key] = forms.Form(schema=schema, request=self.request)
if self.request.method == 'POST': if self.request.method == 'POST':
controls = self.request.POST.items() if form.validate(newstyle=True):
data = form.validate(controls) data = form.validated
batch_key = data['batch_type']
params = {
'description': data['description'],
'notes': data['notes']}
pform = params_forms.get(batch_key)
if pform:
pdata = pform.validate(controls)
for field in pform.schema:
param_name = pform.schema[field.name].param_name
params[param_name] = pdata[field.name]
# TODO: should this be done elsewhere? # collect general params
for name in params: batch_key = data['batch_type']
if params[name] is colander.null: params = {
params[name] = None 'description': data['description'],
'notes': data['notes']}
handler = get_batch_handler(self.rattail_config, batch_key, # collect batch-type-specific params
default=supported[batch_key].spec) pform = params_forms.get(batch_key)
products = self.get_effective_data() if pform and pform.validate(newstyle=True):
progress = SessionProgress(self.request, 'products.batch') pdata = pform.validated
thread = Thread(target=self.make_batch_thread, for field in pform.schema:
args=(handler, self.request.user.uuid, products, params, progress)) param_name = pform.schema[field.name].param_name
thread.start() params[param_name] = pdata[field.name]
return self.render_progress(progress, {
'cancel_url': self.get_index_url(),
'cancel_msg': "Batch creation was canceled.",
})
return { # TODO: should this be done elsewhere?
for name in params:
if params[name] is colander.null:
params[name] = None
handler = supported[batch_key]
products = self.get_effective_data()
progress = SessionProgress(self.request, 'products.batch')
thread = Thread(target=self.make_batch_thread,
args=(handler, self.request.user.uuid, products, params, progress))
thread.start()
return self.render_progress(progress, {
'cancel_url': self.get_index_url(),
'cancel_msg': "Batch creation was canceled.",
})
return self.render_to_response('batch', {
'form': form, 'form': form,
'dform': form.make_deform_form(), # TODO: hacky? at least is explicit.. 'dform': form.make_deform_form(), # TODO: hacky? at least is explicit..
'params_forms': params_forms, 'params_forms': params_forms,
} })
def make_batch_params_schema_pricing(self): def make_batch_params_schema_pricing(self):
""" """
@ -1025,7 +1030,12 @@ class ProductsView(MasterView):
""" """
return colander.SchemaNode( return colander.SchemaNode(
colander.Mapping(), colander.Mapping(),
colander.SchemaNode(colander.Decimal(), name='min_diff_threshold', quant='1.00', missing=colander.null), colander.SchemaNode(colander.Decimal(), name='min_diff_threshold',
quant='1.00', missing=colander.null,
title="Min $ Diff"),
colander.SchemaNode(colander.Decimal(), name='min_diff_percent',
quant='1.00', missing=colander.null,
title="Min % Diff"),
colander.SchemaNode(colander.Boolean(), name='calculate_for_manual'), colander.SchemaNode(colander.Boolean(), name='calculate_for_manual'),
) )