Add support for refreshing multiple batches (results) at once

This commit is contained in:
Lance Edgar 2020-03-02 18:11:13 -06:00
parent 2605f5ab79
commit 0483f47b26
2 changed files with 108 additions and 6 deletions

View file

@ -4,7 +4,7 @@
<%def name="extra_javascript()">
${parent.extra_javascript()}
% if not use_buefy:
% if master.results_executable and request.has_perm('{}.execute_multiple'.format(permission_prefix)):
% if master.results_executable and master.has_perm('execute_multiple'):
<script type="text/javascript">
var has_execution_options = ${'true' if master.has_execution_options(batch) else 'false'};
@ -12,6 +12,17 @@
$(function() {
$('#refresh-results-button').click(function() {
var count = $('.grid-wrapper').gridwrapper('results_count');
if (!count) {
alert("There are no batch results to refresh.");
return;
}
var form = $('form[name="refresh-results"]');
$(this).button('option', 'label', "Refreshing, please wait...").button('disable');
form.submit();
});
$('#execute-results-button').click(function() {
var count = $('.grid-wrapper').gridwrapper('results_count');
if (!count) {
@ -65,7 +76,24 @@
<%def name="grid_tools()">
${parent.grid_tools()}
% if master.results_executable and request.has_perm('{}.execute_multiple'.format(permission_prefix)):
## Refresh Results
% if master.results_refreshable and master.has_perm('refresh'):
% if use_buefy:
<b-button type="is-primary"
disabled
title="TODO: need to implement this for new theme">
Refresh Results
</b-button>
% else:
<button type="button" id="refresh-results-button">
Refresh Results
</button>
% endif
% endif
## Execute Results
% if master.results_executable and master.has_perm('execute_multiple'):
% if use_buefy:
<b-button type="is-primary"
@click="executeResults()"
@ -110,7 +138,7 @@
<%def name="modify_this_page_vars()">
${parent.modify_this_page_vars()}
% if master.results_executable and request.has_perm('{}.execute_multiple'.format(permission_prefix)):
% if master.results_executable and master.has_perm('execute_multiple'):
<script type="text/javascript">
TailboneForm.methods.submit = function() {
@ -150,7 +178,7 @@
<%def name="make_this_page_component()">
${parent.make_this_page_component()}
% if master.results_executable and request.has_perm('{}.execute_multiple'.format(permission_prefix)):
% if master.results_executable and master.has_perm('execute_multiple'):
<script type="text/javascript">
TailboneForm.data = function() { return TailboneFormData }
@ -163,7 +191,7 @@
<%def name="render_this_page_template()">
${parent.render_this_page_template()}
% if master.results_executable and request.has_perm('{}.execute_multiple'.format(permission_prefix)):
% if master.results_executable and master.has_perm('execute_multiple'):
${execute_form.render_deform(form_kwargs={'ref': 'actualForm'}, buttons=False)|n}
% endif
</%def>
@ -172,7 +200,15 @@
${parent.body()}
% if not use_buefy:
% if master.results_executable and request.has_perm('{}.execute_multiple'.format(permission_prefix)):
## Refresh Results
% if master.results_refreshable and master.has_perm('refresh'):
${h.form(url('{}.refresh_results'.format(route_prefix)), name='refresh-results')}
${h.csrf_token(request)}
${h.end_form()}
% endif
% if master.results_executable and master.has_perm('execute_multiple'):
<div id="execution-options-dialog" style="display: none;">
<br />
<p>

View file

@ -79,6 +79,7 @@ class BatchMasterView(MasterView):
refresh_after_create = False
cloneable = False
executable = True
results_refreshable = False
results_executable = False
supports_mobile = True
mobile_filterable = True
@ -1138,6 +1139,64 @@ class BatchMasterView(MasterView):
progress.session['success_url'] = self.get_action_url('view', batch)
progress.session.save()
def refresh_results(self):
"""
Refresh all batches which are returned from the current index query.
Starts a separate thread for the refresh, and displays a progress
indicator page.
"""
key = '{}.refresh_results'.format(self.get_route_prefix())
batches = self.get_effective_data()
progress = self.make_progress(key)
kwargs = {'progress': progress}
thread = Thread(target=self.refresh_results_thread,
args=(batches, self.request.user.uuid),
kwargs=kwargs)
thread.start()
return self.render_progress(progress, {
'cancel_url': self.get_index_url(),
'cancel_msg': "Batch execution was canceled",
})
def refresh_results_thread(self, batches, user_uuid, progress=None):
"""
Thread target for refreshing multiple batches with progress indicator.
"""
session = RattailSession()
batches = batches.with_session(session).all()
user = session.query(model.User).get(user_uuid)
try:
self.handler.refresh_many(batches, user=user, progress=progress)
except Exception as error:
session.rollback()
log.exception("refresh failed for batch(es)!")
session.close()
if progress:
progress.session.load()
progress.session['error'] = True
progress.session['error_msg'] = self.refresh_error_message(error)
progress.session.save()
else:
session.commit()
self.request.session.flash("{} {} were refreshed".format(
len(batches), self.get_model_title_plural()))
success_url = self.get_refresh_results_success_url()
session.close()
if progress:
progress.session.load()
progress.session['complete'] = True
progress.session['success_url'] = success_url
progress.session.save()
def refresh_error_message(self, error):
return "Batch refresh failed: {}".format(simple_error(error))
def get_refresh_results_success_url(self):
return self.get_index_url()
########################################
# batch rows
########################################
@ -1478,6 +1537,13 @@ class BatchMasterView(MasterView):
config.add_view(cls, attr='mobile_mark_pending', route_name='mobile.{}.mark_pending'.format(route_prefix),
permission='{}.edit'.format(permission_prefix))
# refresh multiple batches (results)
if cls.results_refreshable:
config.add_route('{}.refresh_results'.format(route_prefix), '{}/refresh-results'.format(url_prefix),
request_method='POST')
config.add_view(cls, attr='refresh_results', route_name='{}.refresh_results'.format(route_prefix),
permission='{}.refresh'.format(permission_prefix))
# execute (multiple) batch results
if cls.results_executable:
config.add_route('{}.execute_results'.format(route_prefix), '{}/execute-results'.format(url_prefix),