Add support for making new-style batches from products grid query

Just label batches so far, will have to revisit that
This commit is contained in:
Lance Edgar 2016-11-06 23:31:27 -06:00
parent 4cfd3aa00e
commit 0477561ca6
2 changed files with 137 additions and 30 deletions

View file

@ -1,28 +1,74 @@
## -*- coding: utf-8 -*- ## -*- coding: utf-8 -*-
<%inherit file="/base.mako" /> <%inherit file="/base.mako" />
<%def name="title()">Create Products Batch</%def> <%def name="title()">Products: 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>
</%def> </%def>
<div class="form"> <%def name="head_tags()">
${parent.head_tags()}
<script type="text/javascript">
$(function() {
${h.form(request.current_route_url())} $('#batch_type').selectmenu();
$('#make-batch').click(function() {
$(this).button('disable').button('option', 'label', "Working, please wait...");
$(this).parents('form:first').submit();
});
});
</script>
</%def>
<ul id="context-menu">
${self.context_menu_items()}
</ul>
% if supported_batches is not Undefined:
<div class="form">
${h.form(request.current_route_url())}
<div class="field-wrapper">
<label for="batch_type">Batch Type</label>
<div class="field">
${h.select('batch_type', None, supported_batches)}
</div>
</div>
<div class="buttons">
<button type="button" id="make-batch">Create Batch</button>
${h.link_to("Cancel", url('products'), class_='button')}
</div>
${h.end_form()}
<div class="field-wrapper">
<label for="provider">Batch Type</label>
<div class="field">
${h.select('provider', None, providers)}
</div> </div>
</div>
<div class="buttons"> % else: ## legacy mode
${h.submit('create', "Create Batch")}
${h.link_to("Cancel", url('products'), class_='button')}
</div>
${h.end_form()} <div class="form">
</div> ${h.form(request.current_route_url())}
<div class="field-wrapper">
<label for="provider">Batch Type</label>
<div class="field">
${h.select('provider', None, providers)}
</div>
</div>
<div class="buttons">
${h.submit('create', "Create Batch")}
${h.link_to("Cancel", url('products'), class_='button')}
</div>
${h.end_form()}
</div>
% endif

View file

@ -37,6 +37,7 @@ from rattail.db import model, api, auth, Session as RattailSession
from rattail.gpc import GPC from rattail.gpc import GPC
from rattail.threads import Thread from rattail.threads import Thread
from rattail.exceptions import LabelPrintingError from rattail.exceptions import LabelPrintingError
from rattail.util import load_object
import formalchemy as fa import formalchemy as fa
from pyramid import httpexceptions from pyramid import httpexceptions
@ -337,11 +338,77 @@ class ProductsView(MasterView):
'form': form}) 'form': form})
def make_batch(self): def make_batch(self):
"""
View for making a new batch from current product grid query.
"""
# maybe do legacy mode
enabled = self.rattail_config.getlist('rattail.pyramid', 'batches.providers')
if enabled:
supported = batches.get_providers()
providers = dict([(key, supported[key]) for key in enabled if key in supported])
return self.make_batch_legacy(providers)
# okay then, new-style it is
# TODO: make this more configurable surely..?
supported = {
'labels': 'rattail.db.batch.labels.handler:LabelBatchHandler',
}
if self.request.method == 'POST': if self.request.method == 'POST':
provider = self.request.POST.get('provider') batch_key = self.request.POST.get('batch_type')
if provider: if batch_key and batch_key in supported:
provider = batches.get_provider(self.rattail_config, provider) handler_spec = self.rattail_config.get('rattail.batch', '{}.handler'.format(batch_key),
if provider: default=supported[batch_key])
handler = load_object(handler_spec)(self.rattail_config)
progress = SessionProgress(self.request, 'products.batch')
thread = Thread(target=self.make_batch_thread,
args=(handler, self.request.user.uuid, progress))
thread.start()
return self.render_progress({
'key': 'products.batch',
'cancel_url': self.get_index_url(),
'cancel_msg': "Batch creation was canceled.",
})
batch_types = []
for key, spec in supported.iteritems():
handler = load_object(spec)(self.rattail_config)
batch_types.append((key, handler.model_title))
return {'supported_batches': batch_types}
def make_batch_thread(self, handler, user_uuid, progress):
"""
Threat target for making a batch from current products query.
"""
session = RattailSession()
user = session.query(model.User).get(user_uuid)
assert user
products = self.get_effective_query(session)
batch = handler.make_batch(session, created_by=user, products=products, progress=progress)
if not batch:
session.rollback()
session.close()
return
session.commit()
session.refresh(batch)
session.close()
progress.session.load()
progress.session['complete'] = True
progress.session['success_url'] = self.request.route_url('labels.batch.view', uuid=batch.uuid)
progress.session['success_msg'] = 'Batch has been created: {}'.format(batch)
progress.session.save()
def make_batch_legacy(self, providers):
if self.request.method == 'POST':
provider_key = self.request.POST.get('provider')
if provider_key:
provider_factory = providers.get(provider_key)
if provider_factory:
provider = provider_factory(self.rattail_config)
if self.request.POST.get('params') == 'True': if self.request.POST.get('params') == 'True':
provider.set_params(Session(), **self.request.POST) provider.set_params(Session(), **self.request.POST)
@ -356,7 +423,7 @@ class ProductsView(MasterView):
return httpexceptions.HTTPFound(location=url) return httpexceptions.HTTPFound(location=url)
progress = SessionProgress(self.request, 'products.batch') progress = SessionProgress(self.request, 'products.batch')
thread = Thread(target=self.make_batch_thread, args=(provider, progress)) thread = Thread(target=self.make_batch_thread_legacy, args=(provider, progress))
thread.start() thread.start()
kwargs = { kwargs = {
'key': 'products.batch', 'key': 'products.batch',
@ -365,18 +432,12 @@ class ProductsView(MasterView):
} }
return self.render_progress(kwargs) return self.render_progress(kwargs)
enabled = self.rattail_config.get('rattail.pyramid', 'batches.providers') providers = [(p.name, p.description)
if enabled: for p in sorted(providers.itervalues(),
enabled = enabled.split() key=lambda p: p.description)]
providers = []
for provider in batches.iter_providers():
if not enabled or provider.name in enabled:
providers.append((provider.name, provider.description))
return {'providers': providers} return {'providers': providers}
def make_batch_thread(self, provider, progress): def make_batch_thread_legacy(self, provider, progress):
""" """
Threat target for making a batch from current products query. Threat target for making a batch from current products query.
""" """
@ -410,7 +471,7 @@ class ProductsView(MasterView):
"View products marked as deleted") "View products marked as deleted")
# make batch from product query # make batch from product query
config.add_route('products.create_batch', '/products/batch') config.add_route('products.create_batch', '/products/make-batch')
config.add_view(cls, attr='make_batch', route_name='products.create_batch', config.add_view(cls, attr='make_batch', route_name='products.create_batch',
renderer='/products/batch.mako', permission='batches.create') renderer='/products/batch.mako', permission='batches.create')