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
% endif % endif
% if batch.complete and master.mobile_executable and request.has_perm('{}.execute'.format(permission_prefix)): % if batch.complete and master.mobile_executable and request.has_perm('{}.execute'.format(permission_prefix)):
% 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.form(url('mobile.{}.execute'.format(route_prefix), uuid=batch.uuid))}
${h.csrf_token(request)} ${h.csrf_token(request)}
${h.submit('submit', "Execute Batch")} ${h.submit('submit', "Execute Batch")}
${h.end_form()} ${h.end_form()}
% endif % endif
% endif % endif
% endif

View file

@ -898,9 +898,14 @@ class BatchMasterView(MasterView):
def mobile_execute(self): 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() 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 self.executing = True
form = self.make_execute_form(batch) form = self.make_execute_form(batch)
if form.validate(newstyle=True): if form.validate(newstyle=True):
@ -913,20 +918,26 @@ class BatchMasterView(MasterView):
try: try:
result = self.handler.execute(batch, user=self.request.user, **kwargs) result = self.handler.execute(batch, user=self.request.user, **kwargs)
except Exception as err: except Exception as err:
log.exception("failed to execute batch %s: %s", batch.id_str, batch) log.exception("failed to execute %s %s", model_title, batch.id_str)
self.request.session.flash("Failed to execute batch: {}".format(err), 'error') self.request.session.flash(self.execute_error_message(err), 'error')
else: else:
if result: if result:
batch.executed = datetime.datetime.utcnow() batch.executed = datetime.datetime.utcnow()
batch.executed_by = self.request.user 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: else:
log.error("not sure why, but failed to execute batch %s: %s", batch.id_str, batch) 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 batch: {}".format(err), 'error') self.request.session.flash("Failed to execute {}: {}".format(model_title, err), 'error')
return self.redirect(self.get_action_url('view', batch, mobile=True)) return self.redirect(view_url)
self.request.session.flash("Invalid request: {}".format(form.make_deform_form().error), 'error') form.mobile = True
return self.redirect(self.get_action_url('view', batch, 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): def execute_error_message(self, error):
return "Batch execution failed: {}: {}".format(type(error).__name__, error) return "Batch execution failed: {}: {}".format(type(error).__name__, error)