tweak batch progress, add 'executed' to grid

This commit is contained in:
Lance Edgar 2012-09-28 07:55:13 -07:00
parent 38e7335ec9
commit d44af94378
2 changed files with 48 additions and 22 deletions

View file

@ -36,6 +36,7 @@ from webhelpers.html import tags
import edbob
from edbob.pyramid import Session
from edbob.pyramid.forms import EnumFieldRenderer
from edbob.pyramid.grids.search import BooleanSearchFilter
from edbob.pyramid.progress import SessionProgress
from edbob.pyramid.views import SearchableAlchemyGridView, CrudView, View
@ -50,21 +51,37 @@ class BatchesGrid(SearchableAlchemyGridView):
sort = 'id'
def filter_map(self):
def executed_is(q, v):
if v == 'True':
return q.filter(rattail.Batch.executed != None)
else:
return q.filter(rattail.Batch.executed == None)
def executed_isnot(q, v):
if v == 'True':
return q.filter(rattail.Batch.executed == None)
else:
return q.filter(rattail.Batch.executed != None)
return self.make_filter_map(
exact=['id'],
ilike=['source', 'destination', 'description'])
ilike=['source', 'destination', 'description'],
executed={
'is': executed_is,
'nt': executed_isnot,
})
def filter_config(self):
return self.make_filter_config(
filter_label_id="ID")
filter_label_id="ID",
filter_factory_executed=BooleanSearchFilter,
include_filter_executed=True,
filter_type_executed='is',
executed='False')
def sort_map(self):
return self.make_sort_map('source', 'id', 'destination', 'description')
def query(self):
q = self.make_query()
q = q.filter(rattail.Batch.executed == None)
return q
return self.make_sort_map('source', 'id', 'destination', 'description', 'executed')
def grid(self):
g = self.make_grid()
@ -75,6 +92,7 @@ class BatchesGrid(SearchableAlchemyGridView):
g.destination,
g.description,
g.rowcount.label("Row Count"),
g.executed,
],
readonly=True)
if self.request.has_perm('batches.read'):
@ -109,6 +127,7 @@ class BatchCrud(CrudView):
fs.action_type,
fs.description,
fs.rowcount.label("Row Count").readonly(),
fs.executed,
])
return fs
@ -118,19 +137,24 @@ class BatchCrud(CrudView):
class ExecuteBatch(View):
def execute_batch(self, batch):
def execute_batch(self, batch, progress):
session = edbob.Session()
batch = session.merge(batch)
progress = SessionProgress(self.request.session, 'batch.execute')
progress.session['success_msg'] = "Batch \"%s\" has been executed." % batch.description
progress.session['success_url'] = self.request.route_url('batches')
if batch.execute(progress):
session.commit()
else:
if not batch.execute(progress):
session.rollback()
session.close()
return
session.commit()
session.refresh(batch)
session.close()
progress.session.load()
progress.session['complete'] = True
progress.session['success_msg'] = "Batch \"%s\" has been executed." % batch.description
progress.session['success_url'] = self.request.route_url('batches')
progress.session.save()
def __call__(self):
uuid = self.request.matchdict['uuid']
@ -138,12 +162,13 @@ class ExecuteBatch(View):
if not batch:
return HTTPFound(location=self.request.route_url('batches'))
thread = threading.Thread(target=self.execute_batch, args=(batch,))
progress = SessionProgress(self.request.session, 'batch.execute')
thread = threading.Thread(target=self.execute_batch, args=(batch, progress))
thread.start()
kwargs = {
'key': 'batch.execute',
'cancel_url': self.request.route_url('batch.rows', uuid=batch.uuid),
'cancel_msg': "Batch execution was cancelled.",
'cancel_msg': "Batch execution was canceled.",
}
return render_to_response('/progress.mako', kwargs, request=self.request)

View file

@ -217,14 +217,13 @@ def print_labels(request):
class CreateProductsBatch(ProductsGrid):
def make_batch(self, provider):
def make_batch(self, provider, progress):
session = edbob.Session()
self._filter_config = self.filter_config()
self._sort_config = self.sort_config()
products = self.make_query(session)
progress = SessionProgress(self.request.session, 'products.batch')
batch = provider.make_batch(session, products, progress)
if not batch:
session.rollback()
@ -236,6 +235,7 @@ class CreateProductsBatch(ProductsGrid):
session.close()
progress.session.load()
progress.session['complete'] = True
progress.session['success_url'] = self.request.route_url('batch', uuid=batch.uuid)
progress.session['success_msg'] = "Batch \"%s\" has been created." % batch.description
progress.session.save()
@ -259,12 +259,13 @@ class CreateProductsBatch(ProductsGrid):
self.request.session['referer'] = self.request.current_route_url()
return HTTPFound(location=url)
thread = threading.Thread(target=self.make_batch, args=(provider,))
progress = SessionProgress(self.request.session, 'products.batch')
thread = threading.Thread(target=self.make_batch, args=(provider, progress))
thread.start()
kwargs = {
'key': 'products.batch',
'cancel_url': self.request.route_url('products'),
'cancel_msg': "Batch creation was cancelled.",
'cancel_msg': "Batch creation was canceled.",
}
return render_to_response('/progress.mako', kwargs, request=self.request)