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 import edbob
from edbob.pyramid import Session from edbob.pyramid import Session
from edbob.pyramid.forms import EnumFieldRenderer from edbob.pyramid.forms import EnumFieldRenderer
from edbob.pyramid.grids.search import BooleanSearchFilter
from edbob.pyramid.progress import SessionProgress from edbob.pyramid.progress import SessionProgress
from edbob.pyramid.views import SearchableAlchemyGridView, CrudView, View from edbob.pyramid.views import SearchableAlchemyGridView, CrudView, View
@ -50,21 +51,37 @@ class BatchesGrid(SearchableAlchemyGridView):
sort = 'id' sort = 'id'
def filter_map(self): 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( return self.make_filter_map(
exact=['id'], exact=['id'],
ilike=['source', 'destination', 'description']) ilike=['source', 'destination', 'description'],
executed={
'is': executed_is,
'nt': executed_isnot,
})
def filter_config(self): def filter_config(self):
return self.make_filter_config( 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): def sort_map(self):
return self.make_sort_map('source', 'id', 'destination', 'description') return self.make_sort_map('source', 'id', 'destination', 'description', 'executed')
def query(self):
q = self.make_query()
q = q.filter(rattail.Batch.executed == None)
return q
def grid(self): def grid(self):
g = self.make_grid() g = self.make_grid()
@ -75,6 +92,7 @@ class BatchesGrid(SearchableAlchemyGridView):
g.destination, g.destination,
g.description, g.description,
g.rowcount.label("Row Count"), g.rowcount.label("Row Count"),
g.executed,
], ],
readonly=True) readonly=True)
if self.request.has_perm('batches.read'): if self.request.has_perm('batches.read'):
@ -109,6 +127,7 @@ class BatchCrud(CrudView):
fs.action_type, fs.action_type,
fs.description, fs.description,
fs.rowcount.label("Row Count").readonly(), fs.rowcount.label("Row Count").readonly(),
fs.executed,
]) ])
return fs return fs
@ -118,19 +137,24 @@ class BatchCrud(CrudView):
class ExecuteBatch(View): class ExecuteBatch(View):
def execute_batch(self, batch): def execute_batch(self, batch, progress):
session = edbob.Session() session = edbob.Session()
batch = session.merge(batch) batch = session.merge(batch)
progress = SessionProgress(self.request.session, 'batch.execute') if not batch.execute(progress):
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:
session.rollback() session.rollback()
session.close() 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): def __call__(self):
uuid = self.request.matchdict['uuid'] uuid = self.request.matchdict['uuid']
@ -138,12 +162,13 @@ class ExecuteBatch(View):
if not batch: if not batch:
return HTTPFound(location=self.request.route_url('batches')) 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() thread.start()
kwargs = { kwargs = {
'key': 'batch.execute', 'key': 'batch.execute',
'cancel_url': self.request.route_url('batch.rows', uuid=batch.uuid), '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) return render_to_response('/progress.mako', kwargs, request=self.request)

View file

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