Add support for executing batch with options, via mobile

This commit is contained in:
Lance Edgar 2018-02-22 11:20:12 -06:00
parent 0a165c5b93
commit 3d79f9fd7d
3 changed files with 38 additions and 13 deletions

View file

@ -0,0 +1,10 @@
## -*- coding: utf-8; -*-
<%inherit file="/mobile/base.mako" />
<%def name="title()">${index_title} &raquo; ${instance_title} &raquo; Execute</%def>
<%def name="page_title()">${h.link_to(index_title, index_url)} &raquo; ${h.link_to(instance_title, instance_url)} &raquo; Execute</%def>
<div class="form-wrapper">
${form.render()|n}
</div><!-- form-wrapper -->

View file

@ -28,9 +28,13 @@ ${parent.body()}
% endif
% endif
% if batch.complete and master.mobile_executable and request.has_perm('{}.execute'.format(permission_prefix)):
${h.form(url('mobile.{}.execute'.format(route_prefix), uuid=batch.uuid))}
${h.csrf_token(request)}
${h.submit('submit', "Execute Batch")}
${h.end_form()}
% if master.has_execution_options(batch):
${h.link_to("Execute Batch", url('mobile.{}.execute'.format(route_prefix), uuid=batch.uuid), class_='ui-btn ui-corner-all')}
% else:
${h.form(url('mobile.{}.execute'.format(route_prefix), uuid=batch.uuid))}
${h.csrf_token(request)}
${h.submit('submit', "Execute Batch")}
${h.end_form()}
% endif
% endif
% endif

View file

@ -898,9 +898,14 @@ class BatchMasterView(MasterView):
def mobile_execute(self):
"""
Execute a batch via mobile, i.e. (for now) blocking with no progress bar.
Mobile view which can prompt user for execution options if applicable,
and/or execute a batch. For now this is done in a "blocking" fashion,
i.e. no progress bar.
"""
batch = self.get_instance()
model_title = self.get_model_title()
instance_title = self.get_instance_title(batch)
view_url = self.get_action_url('view', batch, mobile=True)
self.executing = True
form = self.make_execute_form(batch)
if form.validate(newstyle=True):
@ -913,20 +918,26 @@ class BatchMasterView(MasterView):
try:
result = self.handler.execute(batch, user=self.request.user, **kwargs)
except Exception as err:
log.exception("failed to execute batch %s: %s", batch.id_str, batch)
self.request.session.flash("Failed to execute batch: {}".format(err), 'error')
log.exception("failed to execute %s %s", model_title, batch.id_str)
self.request.session.flash(self.execute_error_message(err), 'error')
else:
if result:
batch.executed = datetime.datetime.utcnow()
batch.executed_by = self.request.user
self.request.session.flash("Batch was executed: {}".format(batch))
self.request.session.flash("{} was executed: {}".format(model_title, instance_title))
else:
log.error("not sure why, but failed to execute batch %s: %s", batch.id_str, batch)
self.request.session.flash("Failed to execute batch: {}".format(err), 'error')
return self.redirect(self.get_action_url('view', batch, mobile=True))
log.error("not sure why, but failed to execute %s %s: %s", model_title, batch.id_str, batch)
self.request.session.flash("Failed to execute {}: {}".format(model_title, err), 'error')
return self.redirect(view_url)
self.request.session.flash("Invalid request: {}".format(form.make_deform_form().error), 'error')
return self.redirect(self.get_action_url('view', batch, mobile=True))
form.mobile = True
form.submit_label = "Execute"
form.cancel_url = view_url
return self.render_to_response('execute', {
'form': form,
'instance_title': instance_title,
'instance_url': view_url,
}, mobile=True)
def execute_error_message(self, error):
return "Batch execution failed: {}: {}".format(type(error).__name__, error)