Add 'restart datasync' button to datasync changes list page.

This commit is contained in:
Lance Edgar 2016-01-19 17:29:19 -06:00
parent 6b9727d7cb
commit b78b49d79e
4 changed files with 62 additions and 6 deletions

View file

@ -55,14 +55,15 @@ class TailboneAuthorizationPolicy(object):
raise NotImplementedError
def add_permission_group(config, key, label=None):
def add_permission_group(config, key, label=None, overwrite=True):
"""
Add a permission group to the app configuration.
"""
def action():
perms = config.get_settings().get('tailbone_permissions', {})
group = perms.setdefault(key, {'key': key})
group['label'] = label or prettify(key)
if key not in perms or overwrite:
group = perms.setdefault(key, {'key': key})
group['label'] = label or prettify(key)
config.add_settings({'tailbone_permissions': perms})
config.action(None, action)

View file

@ -0,0 +1,25 @@
## -*- coding: utf-8 -*-
<%inherit file="/master/index.mako" />
<%def name="head_tags()">
${parent.head_tags()}
<script type="text/javascript">
$(function() {
$('form[name="restart-datasync"]').submit(function() {
$(this).find('button')
.button('option', 'label', "Restarting DataSync...")
.button('disable');
});
});
</script>
</%def>
<%def name="grid_tools()">
${h.form(url('datasync.restart'), name='restart-datasync')}
<button type="submit">Restart DataSync Daemon</button>
${h.end_form()}
</%def>
${parent.body()}

View file

@ -2,7 +2,7 @@
################################################################################
#
# Rattail -- Retail Software Framework
# Copyright © 2010-2015 Lance Edgar
# Copyright © 2010-2016 Lance Edgar
#
# This file is part of Rattail.
#
@ -24,20 +24,29 @@
DataSync Views
"""
from __future__ import unicode_literals
from __future__ import unicode_literals, absolute_import
import subprocess
import logging
from rattail.db import model
from rattail.config import parse_list
from tailbone import forms
from tailbone.views import MasterView
log = logging.getLogger(__name__)
class DataSyncChangeView(MasterView):
"""
Master view for the DataSyncChange model.
"""
model_class = model.DataSyncChange
model_title = "DataSync Change"
url_prefix = '/datasync/changes'
permission_prefix = 'datasync'
creatable = False
viewable = False
@ -58,6 +67,27 @@ class DataSyncChangeView(MasterView):
],
readonly=True)
def restart(self):
# TODO: Add better validation (e.g. CSRF) here?
if self.request.method == 'POST':
cmd = parse_list(self.rattail_config.require('tailbone', 'datasync.restart'))
log.debug("attempting datasync restart with command: {}".format(cmd))
result = subprocess.call(cmd)
if result == 0:
self.request.session.flash("DataSync daemon has been restarted.")
else:
self.request.session.flash("DataSync daemon could not be restarted; result was: {}".format(result), 'error')
return self.redirect(self.request.route_url('datasyncchanges'))
def includeme(config):
config.add_tailbone_permission_group('datasync', label="DataSync")
# restart daemon
config.add_route('datasync.restart', '/datasync/restart')
config.add_view(DataSyncChangeView, attr='restart', route_name='datasync.restart',
permission='datasync.restart')
config.add_tailbone_permission('datasync', 'datasync.restart', label="Restart DataSync Daemon")
DataSyncChangeView.defaults(config)

View file

@ -608,7 +608,7 @@ class MasterView(View):
model_title = cls.get_model_title()
model_title_plural = cls.get_model_title_plural()
config.add_tailbone_permission_group(permission_prefix, model_title_plural)
config.add_tailbone_permission_group(permission_prefix, model_title_plural, overwrite=False)
# list/search
config.add_route(route_prefix, '{0}/'.format(url_prefix))