mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-07-22 11:59:33 +00:00
Convert pagekite pages to use Django forms
This commit is contained in:
parent
f097047b8b
commit
fe56ef3761
@ -20,14 +20,14 @@ Plinth module for configuring PageKite service
|
||||
"""
|
||||
|
||||
import cherrypy
|
||||
from django import forms
|
||||
from django.core import validators
|
||||
from gettext import gettext as _
|
||||
|
||||
import actions
|
||||
import cfg
|
||||
from forms import Form
|
||||
from modules.auth import require
|
||||
from plugin_mount import PagePlugin, FormPlugin
|
||||
import re
|
||||
from plugin_mount import PagePlugin
|
||||
import util
|
||||
|
||||
|
||||
@ -39,7 +39,6 @@ class PageKite(PagePlugin):
|
||||
PagePlugin.__init__(self, *args, **kwargs)
|
||||
|
||||
self.register_page("router.setup.pagekite")
|
||||
self.register_page("router.setup.pagekite.configure")
|
||||
cfg.html_root.router.setup.menu.add_item(
|
||||
_("Public Visibility (PageKite)"), "icon-flag",
|
||||
"/router/setup/pagekite", 50)
|
||||
@ -48,7 +47,7 @@ class PageKite(PagePlugin):
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(**kwargs):
|
||||
"""Serve introcution page"""
|
||||
"""Serve introdution page"""
|
||||
del kwargs # Unused
|
||||
|
||||
menu = {'title': _('PageKite'),
|
||||
@ -61,29 +60,82 @@ class PageKite(PagePlugin):
|
||||
sidebar_right=sidebar_right)
|
||||
|
||||
|
||||
class configure(FormPlugin, PagePlugin): # pylint: disable-msg=C0103
|
||||
class TrimmedCharField(forms.CharField):
|
||||
"""Trim the contents of a CharField"""
|
||||
def clean(self, value):
|
||||
"""Clean and validate the field value"""
|
||||
if value:
|
||||
value = value.strip()
|
||||
|
||||
return super(TrimmedCharField, self).clean(value)
|
||||
|
||||
|
||||
class ConfigureForm(forms.Form): # pylint: disable-msg=W0232
|
||||
"""Form to configure PageKite"""
|
||||
enabled = forms.BooleanField(label=_('Enable PageKite'),
|
||||
required=False)
|
||||
|
||||
server = forms.CharField(
|
||||
label=_('Server'), required=False,
|
||||
help_text=_('Currently only pagekite.net server is supported'),
|
||||
widget=forms.TextInput(attrs={'placeholder': 'pagekite.net',
|
||||
'disabled': 'disabled'}))
|
||||
|
||||
kite_name = TrimmedCharField(
|
||||
label=_('Kite name'),
|
||||
help_text=_('Example: mybox1-myacc.pagekite.me'),
|
||||
validators=[
|
||||
validators.RegexValidator(r'^[\w-]{1,63}(\.[\w-]{1,63})*$',
|
||||
_('Invalid kite name'))])
|
||||
|
||||
kite_secret = TrimmedCharField(
|
||||
label=_('Kite secret'),
|
||||
help_text=_('A secret associated with the kite or the default secret \
|
||||
for your account if no secret is set on the kite'))
|
||||
|
||||
http_enabled = forms.BooleanField(
|
||||
label=_('Web Server (HTTP)'), required=False,
|
||||
help_text=_('Site will be available at \
|
||||
<a href="http://mybox1-myacc.pagekite.me">http://mybox1-myacc.pagekite.me \
|
||||
</a>'))
|
||||
|
||||
ssh_enabled = forms.BooleanField(
|
||||
label=_('Secure Shell (SSH)'), required=False,
|
||||
help_text=_('See SSH client setup <a href="\
|
||||
https://pagekite.net/wiki/Howto/SshOverPageKite/">instructions</a>'))
|
||||
|
||||
|
||||
class Configure(PagePlugin): # pylint: disable-msg=C0103
|
||||
"""Main configuration form"""
|
||||
order = 65
|
||||
|
||||
url = ["/router/setup/pagekite/configure"]
|
||||
def __init__(self, *args, **kwargs):
|
||||
PagePlugin.__init__(self, *args, **kwargs)
|
||||
|
||||
js = """
|
||||
<script type="text/javascript">
|
||||
(function($) {
|
||||
self.register_page("router.setup.pagekite.configure")
|
||||
|
||||
$('#pagekite-server').attr("disabled", "disabled");
|
||||
@cherrypy.expose
|
||||
@require()
|
||||
def index(self, **kwargs):
|
||||
"""Serve the configuration form"""
|
||||
status = self.get_status()
|
||||
|
||||
$('#pagekite-enable').change(function() {
|
||||
if ($('#pagekite-enable').prop('checked')) {
|
||||
$('#pagekite-post-enable-form').show('slow');
|
||||
} else {
|
||||
$('#pagekite-post-enable-form').hide('slow');
|
||||
}
|
||||
});
|
||||
form = None
|
||||
messages = []
|
||||
|
||||
})(jQuery);
|
||||
</script>
|
||||
"""
|
||||
if kwargs:
|
||||
form = ConfigureForm(kwargs, prefix='pagekite')
|
||||
# pylint: disable-msg=E1101
|
||||
if form.is_valid():
|
||||
self._apply_changes(status, form.cleaned_data, messages)
|
||||
status = self.get_status()
|
||||
form = ConfigureForm(initial=status, prefix='pagekite')
|
||||
else:
|
||||
form = ConfigureForm(initial=status, prefix='pagekite')
|
||||
|
||||
return util.render_template(template='pagekite_configure',
|
||||
title=_('Configure PageKite'), form=form,
|
||||
messages=messages)
|
||||
|
||||
def get_status(self):
|
||||
"""
|
||||
@ -112,99 +164,11 @@ $('#pagekite-enable').change(function() {
|
||||
status['service'] = {}
|
||||
for service in ('http', 'ssh'):
|
||||
output = self._run(['get-service-status', service])
|
||||
status['service'][service] = (output.split()[0] == 'enabled')
|
||||
status[service + '_enabled'] = (output.split()[0] == 'enabled')
|
||||
|
||||
return status
|
||||
|
||||
def main(self, *args, **kwargs):
|
||||
"""Build and return the main content area which is the form"""
|
||||
del args # unused
|
||||
|
||||
status = self.get_status()
|
||||
|
||||
if not status:
|
||||
return _('''
|
||||
<p>PageKite is not installed, please install it. PageKite comes
|
||||
pre-installed with {box_name}. On any Debian based system (such as
|
||||
{box_name}) you may install it using the command 'aptitude install
|
||||
pagekite'</p>''').format(box_name=cfg.box_name)
|
||||
|
||||
try:
|
||||
message = kwargs['message'].text
|
||||
except KeyError:
|
||||
message = None
|
||||
form = Form(
|
||||
title="Configure PageKite",
|
||||
action=cfg.server_dir + "/router/setup/pagekite/configure/",
|
||||
name="configure_pagekite_form", message=message)
|
||||
|
||||
form.checkbox(_("Enable PageKite"), name="pagekite_enable",
|
||||
id="pagekite-enable", checked=status['enabled'])
|
||||
|
||||
show_form = "block" if status['enabled'] else "none"
|
||||
form.html('''
|
||||
<div id='pagekite-post-enable-form'
|
||||
style='display: {show_form}'>'''.format(show_form=show_form))
|
||||
|
||||
form.html(_("<h3>PageKite Account</h3>"))
|
||||
form.text_input(_("Server"), name="pagekite_server",
|
||||
id="pagekite-server", value="pagekite.net")
|
||||
form.text_input(_("Kite name"), name="pagekite_kite_name",
|
||||
id="pagekite-kite-name", value=status['kite_name'])
|
||||
form.text_input(_("Kite secret"), name="pagekite_kite_secret",
|
||||
id="pagekite-kite-secret", value=status['kite_secret'])
|
||||
|
||||
form.html(_("<h3>Services</h3>"))
|
||||
form.checkbox(_("Web Server (HTTP)"), name="pagekite_http_enable",
|
||||
id="pagekite-http-enable",
|
||||
checked=status['service']['http'])
|
||||
form.checkbox(_("Secure Shell (SSH)"), name="pagekite_ssh_enable",
|
||||
id="pagekite-ssh-enable",
|
||||
checked=status['service']['ssh'])
|
||||
|
||||
form.html("</div>") # pagekite-post-enable-form
|
||||
|
||||
form.submit(_("Update setup"))
|
||||
return form.render()
|
||||
|
||||
def process_form(self, **kwargs):
|
||||
"""Handle form submission"""
|
||||
status = self.get_status()
|
||||
|
||||
message, new_status = self.validate_form(**kwargs)
|
||||
if not message.text:
|
||||
self.apply_changes(status, new_status, message)
|
||||
|
||||
return message
|
||||
|
||||
@staticmethod
|
||||
def validate_form(**kwargs):
|
||||
"""Check whether all the input form values are correct"""
|
||||
new_status = {}
|
||||
message = util.Message()
|
||||
|
||||
domain_name_re = r'^[\w-]{1,63}(\.[\w-]{1,63})*$'
|
||||
pagekite_kite_name = kwargs.get('pagekite_kite_name', '').strip()
|
||||
if not re.match(domain_name_re, pagekite_kite_name):
|
||||
message.add(_('Invalid kite name'))
|
||||
else:
|
||||
new_status['kite_name'] = pagekite_kite_name
|
||||
|
||||
pagekite_kite_secret = kwargs.get('pagekite_kite_secret', '').strip()
|
||||
if not pagekite_kite_secret:
|
||||
message.add(_('Invalid kite secret'))
|
||||
else:
|
||||
new_status['kite_secret'] = pagekite_kite_secret
|
||||
|
||||
new_status['enabled'] = (kwargs.get('pagekite_enable') == 'on')
|
||||
new_status['service'] = {
|
||||
'http': (kwargs.get('pagekite_http_enable') == 'on'),
|
||||
'ssh': (kwargs.get('pagekite_ssh_enable') == 'on')
|
||||
}
|
||||
|
||||
return message, new_status
|
||||
|
||||
def apply_changes(self, old_status, new_status, message):
|
||||
def _apply_changes(self, old_status, new_status, messages):
|
||||
"""Apply the changes to PageKite configuration"""
|
||||
cfg.log.info('New status is - %s' % new_status)
|
||||
|
||||
@ -214,27 +178,29 @@ pagekite'</p>''').format(box_name=cfg.box_name)
|
||||
if old_status['enabled'] != new_status['enabled']:
|
||||
if new_status['enabled']:
|
||||
self._run(['set-status', 'enable'])
|
||||
message.add(_('PageKite enabled'))
|
||||
messages.append(('success', _('PageKite enabled')))
|
||||
else:
|
||||
self._run(['set-status', 'disable'])
|
||||
message.add(_('PageKite disabled'))
|
||||
messages.append(('success', _('PageKite disabled')))
|
||||
|
||||
if old_status['kite_name'] != new_status['kite_name'] or \
|
||||
old_status['kite_secret'] != new_status['kite_secret']:
|
||||
self._run(['set-kite', '--kite-name', new_status['kite_name'],
|
||||
'--kite-secret', new_status['kite_secret']])
|
||||
message.add(_('Kite details set'))
|
||||
messages.append(('success', _('Kite details set')))
|
||||
|
||||
for service, old_value in old_status['service'].items():
|
||||
if old_value != new_status['service'][service]:
|
||||
if new_status['service'][service]:
|
||||
for service in ['http', 'ssh']:
|
||||
if old_status[service + '_enabled'] != \
|
||||
new_status[service + '_enabled']:
|
||||
if new_status[service + '_enabled']:
|
||||
self._run(['set-service-status', service, 'enable'])
|
||||
message.add(_('Service enabled: {service}')
|
||||
.format(service=service))
|
||||
messages.append(('success', _('Service enabled: {service}')
|
||||
.format(service=service)))
|
||||
else:
|
||||
self._run(['set-service-status', service, 'disable'])
|
||||
message.add(_('Service disabled: {service}')
|
||||
.format(service=service))
|
||||
messages.append(('success',
|
||||
_('Service disabled: {service}')
|
||||
.format(service=service)))
|
||||
|
||||
if old_status != new_status:
|
||||
self._run(['start'])
|
||||
|
||||
60
modules/installed/router/templates/pagekite_configure.html
Normal file
60
modules/installed/router/templates/pagekite_configure.html
Normal file
@ -0,0 +1,60 @@
|
||||
{% extends "login_nav.html" %}
|
||||
{% load bootstrap %}
|
||||
|
||||
{% block main_block %}
|
||||
|
||||
{% if pagekite_status = 'not_installed' %}
|
||||
|
||||
<p>PageKite is not installed, please install it. PageKite comes
|
||||
pre-installed with {{ cfg.box_name }}. On any Debian based system
|
||||
(such as {{ cfg.box_name }}) you may install it using the command
|
||||
'aptitude install pagekite'</p>
|
||||
|
||||
{% else %}
|
||||
|
||||
{% for severity, message in messages %}
|
||||
<div class='alert alert-{{ severity }}'>{{ message }}</div>
|
||||
{% endfor %}
|
||||
|
||||
<form class="form" method="post">
|
||||
{% csrf_token %}
|
||||
|
||||
{% include 'bootstrapform/field.html' with field=form.enabled %}
|
||||
|
||||
<div id='pagekite-post-enabled-form'
|
||||
style='display: {{ form.enabled.value|yesno:'block,none' }};'>
|
||||
<h3>PageKite Account</h3>
|
||||
{% include 'bootstrapform/field.html' with field=form.server %}
|
||||
{% include 'bootstrapform/field.html' with field=form.kite_name %}
|
||||
{% include 'bootstrapform/field.html' with field=form.kite_secret %}
|
||||
|
||||
<h3>Services</h3>
|
||||
{% include 'bootstrapform/field.html' with field=form.http_enabled %}
|
||||
{% include 'bootstrapform/field.html' with field=form.ssh_enabled %}
|
||||
</div>
|
||||
|
||||
<input type="submit" class="btn-primary" value="Update setup"/>
|
||||
|
||||
</form>
|
||||
|
||||
{% endif %}
|
||||
|
||||
{% endblock %}
|
||||
|
||||
{% block js_block %}
|
||||
{{ js|safe }}
|
||||
|
||||
<script type="text/javascript">
|
||||
(function($) {
|
||||
|
||||
$('#id_pagekite-enabled').change(function() {
|
||||
if ($('#id_pagekite-enabled').prop('checked')) {
|
||||
$('#pagekite-post-enabled-form').show('slow');
|
||||
} else {
|
||||
$('#pagekite-post-enabled-form').hide('slow');
|
||||
}
|
||||
});
|
||||
|
||||
})(jQuery);
|
||||
</script>
|
||||
{% endblock %}
|
||||
@ -193,6 +193,8 @@
|
||||
<!--<script type="text/javascript" src="{{ basehref }}/static/theme/js/libs/bootstrap/typeahead.js"></script>-->
|
||||
<!-- JS plugins -->
|
||||
<script type="text/javascript" src="{{ basehref }}/static/theme/js/plugins.js"></script>
|
||||
{{ js|safe }}
|
||||
{% block js_block %}
|
||||
{{ js|safe }}
|
||||
{% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user