From a8a4e362a083fb98cd9eb8700ae2340c01e21d34 Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Wed, 21 Feb 2018 18:55:16 -0600 Subject: [PATCH] Add basic mobile support for executing batches no progress, or options, yet.. --- tailbone/templates/mobile/batch/view.mako | 30 +++++++++++++-------- tailbone/views/batch/core.py | 32 +++++++++++++++++++++++ tailbone/views/master.py | 8 +++++- 3 files changed, 58 insertions(+), 12 deletions(-) diff --git a/tailbone/templates/mobile/batch/view.mako b/tailbone/templates/mobile/batch/view.mako index 0770e703..9a24b21e 100644 --- a/tailbone/templates/mobile/batch/view.mako +++ b/tailbone/templates/mobile/batch/view.mako @@ -11,18 +11,26 @@ ${parent.body()} ${grid.render_complete()|n} % endif -% if not batch.executed and request.has_perm('{}.edit'.format(permission_prefix)): - % if batch.complete: - ${h.form(request.route_url('mobile.{}.mark_pending'.format(route_prefix), uuid=batch.uuid))} +% if not batch.executed: + % if request.has_perm('{}.edit'.format(permission_prefix)): + % if batch.complete: + ${h.form(url('mobile.{}.mark_pending'.format(route_prefix), uuid=batch.uuid))} + ${h.csrf_token(request)} + ${h.hidden('mark-pending', value='true')} + ${h.submit('submit', "Mark Batch as Pending")} + ${h.end_form()} + % else: + ${h.form(url('mobile.{}.mark_complete'.format(route_prefix), uuid=batch.uuid))} + ${h.csrf_token(request)} + ${h.hidden('mark-complete', value='true')} + ${h.submit('submit', "Mark Batch as Complete")} + ${h.end_form()} + % 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.hidden('mark-pending', value='true')} - ${h.submit('submit', "Mark Batch as Pending")} - ${h.end_form()} - % else: - ${h.form(request.route_url('mobile.{}.mark_complete'.format(route_prefix), uuid=batch.uuid))} - ${h.csrf_token(request)} - ${h.hidden('mark-complete', value='true')} - ${h.submit('submit', "Mark Batch as Complete")} + ${h.submit('submit', "Execute Batch")} ${h.end_form()} % endif % endif diff --git a/tailbone/views/batch/core.py b/tailbone/views/batch/core.py index 1db69a4f..1d539dd2 100644 --- a/tailbone/views/batch/core.py +++ b/tailbone/views/batch/core.py @@ -892,6 +892,38 @@ class BatchMasterView(MasterView): self.request.session.flash("Invalid request: {}".format(form.make_deform_form().error), 'error') return self.redirect(self.get_action_url('view', batch)) + def mobile_execute(self): + """ + Execute a batch via mobile, i.e. (for now) blocking with no progress bar. + """ + batch = self.get_instance() + self.executing = True + form = self.make_execute_form(batch) + if form.validate(newstyle=True): + kwargs = dict(form.validated) + + # cache options to use as defaults next time + for key, value in form.validated.items(): + self.request.session['batch.{}.execute_option.{}'.format(batch.batch_key, key)] = value + + 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') + else: + if result: + batch.executed = datetime.datetime.utcnow() + batch.executed_by = self.request.user + self.request.session.flash("Batch was executed: {}".format(batch)) + 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)) + + self.request.session.flash("Invalid request: {}".format(form.make_deform_form().error), 'error') + return self.redirect(self.get_action_url('view', batch, mobile=True)) + def execute_error_message(self, error): return "Batch execution failed: {}: {}".format(type(error).__name__, error) diff --git a/tailbone/views/master.py b/tailbone/views/master.py index 1fa8f739..f3d49514 100644 --- a/tailbone/views/master.py +++ b/tailbone/views/master.py @@ -88,6 +88,7 @@ class MasterView(View): mobile_creatable = False mobile_pageable = True mobile_filterable = False + mobile_executable = False listing = False creating = False @@ -2746,12 +2747,17 @@ class MasterView(View): "Edit {0}".format(model_title)) # execute - if cls.executable: + if cls.executable or cls.mobile_executable: config.add_tailbone_permission(permission_prefix, '{}.execute'.format(permission_prefix), "Execute {}".format(model_title)) + if cls.executable: config.add_route('{}.execute'.format(route_prefix), '{}/{{{}}}/execute'.format(url_prefix, model_key)) config.add_view(cls, attr='execute', route_name='{}.execute'.format(route_prefix), permission='{}.execute'.format(permission_prefix)) + if cls.mobile_executable: + config.add_route('mobile.{}.execute'.format(route_prefix), '/mobile{}/{{{}}}/execute'.format(url_prefix, model_key)) + config.add_view(cls, attr='mobile_execute', route_name='mobile.{}.execute'.format(route_prefix), + permission='{}.execute'.format(permission_prefix)) # delete if cls.deletable: