diff --git a/tailbone/templates/products/batch.mako b/tailbone/templates/products/batch.mako
index ee44d607..3388bcf6 100644
--- a/tailbone/templates/products/batch.mako
+++ b/tailbone/templates/products/batch.mako
@@ -1,28 +1,74 @@
## -*- coding: utf-8 -*-
<%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="head_tags()">
+ ${parent.head_tags()}
+
+%def>
+
+
+
+% if supported_batches is not Undefined:
+
+
+
+% endif
diff --git a/tailbone/views/products.py b/tailbone/views/products.py
index 14b23a09..b7eb8b7c 100644
--- a/tailbone/views/products.py
+++ b/tailbone/views/products.py
@@ -37,6 +37,7 @@ from rattail.db import model, api, auth, Session as RattailSession
from rattail.gpc import GPC
from rattail.threads import Thread
from rattail.exceptions import LabelPrintingError
+from rattail.util import load_object
import formalchemy as fa
from pyramid import httpexceptions
@@ -337,11 +338,77 @@ class ProductsView(MasterView):
'form': form})
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':
- provider = self.request.POST.get('provider')
- if provider:
- provider = batches.get_provider(self.rattail_config, provider)
- if provider:
+ batch_key = self.request.POST.get('batch_type')
+ if batch_key and batch_key in supported:
+ handler_spec = self.rattail_config.get('rattail.batch', '{}.handler'.format(batch_key),
+ 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':
provider.set_params(Session(), **self.request.POST)
@@ -356,7 +423,7 @@ class ProductsView(MasterView):
return httpexceptions.HTTPFound(location=url)
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()
kwargs = {
'key': 'products.batch',
@@ -365,18 +432,12 @@ class ProductsView(MasterView):
}
return self.render_progress(kwargs)
- enabled = self.rattail_config.get('rattail.pyramid', 'batches.providers')
- if enabled:
- enabled = enabled.split()
-
- providers = []
- for provider in batches.iter_providers():
- if not enabled or provider.name in enabled:
- providers.append((provider.name, provider.description))
-
+ providers = [(p.name, p.description)
+ for p in sorted(providers.itervalues(),
+ key=lambda p: p.description)]
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.
"""
@@ -410,7 +471,7 @@ class ProductsView(MasterView):
"View products marked as deleted")
# 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',
renderer='/products/batch.mako', permission='batches.create')