Add min diff threshold param when making price batch from product query

Hopefully this sets the stage for arbitrary batch params here..
This commit is contained in:
Lance Edgar 2017-01-30 16:11:29 -06:00
parent 2ab2dfe26b
commit e452ea1ae9
5 changed files with 107 additions and 57 deletions

View file

@ -43,13 +43,13 @@ class PricingBatchView(BatchMasterView):
route_prefix = 'batch.pricing'
url_prefix = '/batches/pricing'
creatable = False
editable = False
rows_editable = True
def configure_fieldset(self, fs):
fs.configure(
include=[
fs.id,
fs.min_diff_threshold,
fs.created,
fs.created_by,
fs.executed,

View file

@ -40,6 +40,7 @@ from rattail.exceptions import LabelPrintingError
from rattail.util import load_object
from rattail.batch import get_batch_handler
import wtforms
import formalchemy as fa
from pyramid import httpexceptions
from pyramid.renderers import render_to_response
@ -399,38 +400,65 @@ class ProductsView(MasterView):
# okay then, new-style it is
supported = self.get_supported_batches()
batch_options = []
for key, spec in list(supported.items()):
handler = load_object(spec)(self.rattail_config)
handler.spec = spec
supported[key] = handler
batch_options.append((key, handler.get_model_title()))
if self.request.method == 'POST':
batch_key = self.request.POST.get('batch_type')
if batch_key and batch_key in supported:
class MakeBatchForm(wtforms.Form):
batch_type = wtforms.SelectField(choices=batch_options)
form = MakeBatchForm(self.request.POST)
params_forms = {}
for key, handler in supported.items():
make_form = getattr(self, 'make_batch_params_form_{}'.format(key), None)
if make_form:
params_forms[key] = make_form()
if self.request.method == 'POST' and form.validate():
batch_key = form.batch_type.data
params = {}
pform = params_forms.get(batch_key)
if not pform or pform.validate(): # params form must validate if present
if pform:
params = dict((name, pform[name].data) for name in pform._fields)
handler = get_batch_handler(self.rattail_config, batch_key,
default=supported[batch_key])
default=supported[batch_key].spec)
products = self.get_effective_data()
progress = SessionProgress(self.request, 'products.batch')
progress_key = 'products.batch'
progress = SessionProgress(self.request, progress_key)
thread = Thread(target=self.make_batch_thread,
args=(handler, self.request.user.uuid, products, progress))
args=(handler, self.request.user.uuid, products, params, progress))
thread.start()
return self.render_progress({
'key': 'products.batch',
'key': progress_key,
'cancel_url': self.get_index_url(),
'cancel_msg': "Batch creation was canceled.",
})
return {'form': form, 'params_forms': params_forms}
batch_types = []
for key, spec in supported.iteritems():
handler = load_object(spec)(self.rattail_config)
batch_types.append((key, handler.get_model_title()))
def make_batch_params_form_pricing(self):
"""
Returns a wtforms.Form object with param fields for making a new
Pricing Batch.
"""
class PricingParamsForm(wtforms.Form):
min_diff_threshold = wtforms.DecimalField(places=2, validators=[wtforms.validators.optional()])
return {'supported_batches': batch_types}
return PricingParamsForm(self.request.POST)
def make_batch_thread(self, handler, user_uuid, products, progress):
def make_batch_thread(self, handler, user_uuid, products, params, progress):
"""
Threat target for making a batch from current products query.
"""
session = RattailSession()
user = session.query(model.User).get(user_uuid)
assert user
batch = handler.make_batch(session, created_by=user)
params['created_by'] = user
batch = handler.make_batch(session, **params)
batch.products = products.with_session(session).all()
handler.make_initial_rows(batch, progress=progress)
@ -483,7 +511,7 @@ class ProductsView(MasterView):
providers = [(p.name, p.description)
for p in sorted(providers.itervalues(),
key=lambda p: p.description)]
return {'providers': providers}
return {'legacy_mode': True, 'providers': providers}
def make_batch_thread_legacy(self, provider, progress):
"""