pagekite: Get rid of tabs in the configuration page

Made the changes requested in issue #1693 to the pagekite
configuration page. Removed the tabs and the javascript.
Made a sort of hacky but working solution to always having
the standard pagekite services being enabled. Put custom
services configuration below the regular configuration and
added a separate add custom service page a la the Gitweb
page.

Fixed formatting issues.

Signed-off-by: Matt Conroy <matt@mattconroy.net>
Reviewed-by: Joseph Nuthalapati <njoseph@riseup.net>
This commit is contained in:
Matt Conroy 2019-11-18 00:45:18 -05:00 committed by Joseph Nuthalapati
parent a869ef531c
commit bdb090e386
No known key found for this signature in database
GPG Key ID: 5398F00A2FA43C35
6 changed files with 117 additions and 207 deletions

View File

@ -112,6 +112,24 @@ class ConfigurationForm(forms.Form):
if old['enabled'] != new['enabled']:
if new['enabled']:
utils.run(['start-and-enable'])
# Ensure all standard/predefined services are enabled
for service_name in utils.PREDEFINED_SERVICES.keys():
service = \
utils.PREDEFINED_SERVICES[service_name]['params']
service = json.dumps(service)
# Probably should keep track of which services
# are enabled since adding the service produces
# an error if it is already added. But this works
# too.
try:
utils.run(['add-service', '--service', service])
except ActionError as exception:
if "already exists" in str(exception):
pass
else:
raise
messages.success(request, _('PageKite enabled'))
else:
utils.run(['stop-and-disable'])
@ -127,43 +145,6 @@ class ConfigurationForm(forms.Form):
kite_name=new['kite_name'])
class StandardServiceForm(forms.Form):
"""Creates a form out of PREDEFINED_SERVICES"""
def __init__(self, *args, **kwargs):
"""Add the fields from PREDEFINED_SERVICES"""
super(StandardServiceForm, self).__init__(*args, **kwargs)
kite = utils.get_kite_details()
for name, service in utils.PREDEFINED_SERVICES.items():
if name in ('http', 'https'):
help_text = service['help_text'].format(kite['kite_name'])
else:
help_text = service['help_text']
self.fields[name] = forms.BooleanField(
label=service['label'], help_text=help_text, required=False)
def save(self, request):
formdata = self.cleaned_data
for service_name in utils.PREDEFINED_SERVICES.keys():
if self.initial[service_name] != formdata[service_name]:
service = utils.PREDEFINED_SERVICES[service_name]['params']
service = json.dumps(service)
if formdata[service_name]:
utils.run(['add-service', '--service', service])
messages.success(
request,
_('Service enabled: {name}').format(name=service_name))
else:
utils.run(['remove-service', '--service', service])
messages.success(
request,
_('Service disabled: {name}').format(
name=service_name))
# Update kite services registered with Name Services module.
utils.update_names_module()
class BaseCustomServiceForm(forms.Form):
"""Basic form functionality to handle a custom service"""
choices = [('http', 'http'), ('https', 'https'), ('raw', 'raw')]

View File

@ -1,34 +0,0 @@
/**
* @licstart The following is the entire license notice for the JavaScript
* code in this page.
*
* This file is part of FreedomBox.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @licend The above is the entire license notice for the JavaScript code
* in this page.
*/
(function($) {
$('#id_pagekite-enabled').change(function() {
if ($('#id_pagekite-enabled').prop('checked')) {
$('#pagekite-post-enabled-form').show('fast');
} else {
$('#pagekite-post-enabled-form').hide('fast');
}
}).change();
})(jQuery);

View File

@ -21,29 +21,74 @@
{% load bootstrap %}
{% load i18n %}
{% load static %}
{% load pagekite_extras %}
{% block page_head %}
<style type="text/css">
div.custom-services span.service {
display: inline-block;
padding-top: 6px;
}
form.pull-right button {
margin: 10px 5px;
}
.add-service input.btn {
margin: 10px 0px;
}
</style>
{% endblock %}
{% block configuration %}
{{ block.super }}
<form class="form" method="post">
{% csrf_token %}
<h3>{% trans "Custom Services" %}</h3>
{% include 'bootstrapform/field.html' with field=form.enabled %}
<a href="{% url 'pagekite:add-custom-service' %}" class="btn btn-primary"
role="button" title="{% trans 'Add Custom Service' %}">
<span class="fa fa-plus" aria-hidden="true"></span>
{% trans 'Add Custom Service' %}
</a>
<div id='pagekite-post-enabled-form'
style='display: {{ form.enabled.value|yesno:'block,none' }};'>
<h3>{% trans "PageKite Account" %}</h3>
{{ form.server_domain|bootstrap_horizontal }}
{{ form.server_port|bootstrap_horizontal }}
{{ form.kite_name|bootstrap_horizontal }}
{{ form.kite_secret|bootstrap_horizontal }}
<div>
<h4>{% trans "Existing custom services" %}</h4>
{% if not custom_services %}
<i>{% trans "You don't have any Custom Services enabled" %}</i>
{% endif %}
<div class="list-group">
{% for service in custom_services %}
{% create_pagekite_service_url service kite_name as service_url %}
<div class="list-group-item clearfix">
<span class="service">
<span title="Connects {{ service_url }} to {{ service.backend_host }}:{{ service.backend_port }}">
{% if service_url|slice:":4" == "http" %}
<a href="{{ service_url }}">{{ service_url }}</a>
{% else %}
{{ service_url }}
{% endif %}
<br>
{% blocktrans trimmed with backend_host=service.backend_host backend_port=service.backend_port %}
connected to {{ backend_host }}:{{ backend_port }}
{% endblocktrans %}
</span>
</span>
<form class="form pull-right" method="post"
action="{% url 'pagekite:delete-custom-service' %}">
<div style='display:none'>
{% csrf_token %}
{{ service.form.as_p }}
</div>
<button type="submit" class="btn btn-default"
title="{% trans "Delete this service" %}">
<span class="fa fa-trash-o" aria-hidden="true">
</span>
</button>
</form>
</div>
{% endfor %}
</div>
</div>
<input type="submit" class="btn btn-primary"
value="{% trans "Save settings" %}"/>
</form>
{% endblock %}
{% block page_js %}
<script type="text/javascript" src="{% static 'pagekite/pagekite.js' %}"></script>
{% endblock %}

View File

@ -1,4 +1,4 @@
{% extends "pagekite_base.html" %}
{% extends "base.html" %}
{% comment %}
#
# This file is part of FreedomBox.
@ -20,95 +20,32 @@
{% load bootstrap %}
{% load i18n %}
{% load static %}
{% load pagekite_extras %}
{% block page_head %}
<style type="text/css">
div.custom-services span.service {
display: inline-block;
padding-top: 6px;
}
form.pull-right button {
margin: 10px 5px;
}
.add-service input.btn {
margin: 10px 0px;
}
</style>
{% endblock %}
{% block configuration %}
{% block content %}
<h3>{% trans 'Add custom PageKite service' %}</h3>
<div class="alert alert-warning" role="alert">
{% blocktrans trimmed %}
<b>Warning:</b><br>Your PageKite frontend server may not support
all the protocol/port combinations that you are able to define
here. For example, HTTPS on ports other than 443 is known to
cause problems.
<b>Warning:</b><br>Your PageKite frontend server may not support
all the protocol/port combinations that you are able to define
here. For example, HTTPS on ports other than 443 is known to
cause problems.
{% endblocktrans %}
</div>
<div class="row custom-services">
<form class="form" method="post">
{% csrf_token %}
<div>
<form class="form add-service" method="post">
<h4>{% trans "Create a custom service" %}</h4>
{{ form|bootstrap_horizontal:'col-lg-6' }}
{{ form|bootstrap }}
{% csrf_token %}
<div class="form-group">
<div class="col-lg-offset-6 col-lg-6">
<input type="submit" class="btn btn-primary"
value="{% trans "Add Service" %}"/>
</div>
</div>
</form>
</div>
<div>
<h4>{% trans "Existing custom services" %}</h4>
{% if not custom_services %}
<i>{% trans "You don't have any Custom Services enabled" %}</i>
{% endif %}
<div class="list-group">
{% for service in custom_services %}
{% create_pagekite_service_url service kite_name as service_url %}
<div class="list-group-item clearfix">
<span class="service">
<span title="Connects {{ service_url }} to {{ service.backend_host }}:{{ service.backend_port }}">
{% if service_url|slice:":4" == "http" %}
<a href="{{ service_url }}">{{ service_url }}</a>
{% else %}
{{ service_url }}
{% endif %}
<br>
{% blocktrans trimmed with backend_host=service.backend_host backend_port=service.backend_port %}
connected to {{ backend_host }}:{{ backend_port }}
{% endblocktrans %}
</span>
</span>
<form class="form pull-right" method="post"
action="{% url 'pagekite:delete-custom-service' %}">
<div style='display:none'>
{% csrf_token %}
{{ service.form.as_p }}
</div>
<button type="submit" class="btn btn-default"
title="{% trans "Delete this service" %}">
<span class="fa fa-trash-o" aria-hidden="true">
</span>
</button>
</form>
</div>
{% endfor %}
</div>
</div>
</div>
<input type="submit" class="btn btn-primary"
value="{% trans "Submit" %}"/>
</form>
{% endblock %}

View File

@ -20,15 +20,12 @@ URLs for the PageKite module
from django.conf.urls import url
from .views import (ConfigurationView, CustomServiceView, DeleteServiceView,
StandardServiceView)
from .views import (ConfigurationView, AddCustomServiceView, DeleteServiceView)
urlpatterns = [
url(r'^sys/pagekite/$', ConfigurationView.as_view(), name='index'),
url(r'^sys/pagekite/services/standard/$', StandardServiceView.as_view(),
name='standard-services'),
url(r'^sys/pagekite/services/custom/$', CustomServiceView.as_view(),
name='custom-services'),
url(r'^sys/pagekite/services/custom/add/$', AddCustomServiceView.as_view(),
name='add-custom-service'),
url(r'^sys/pagekite/services/custom/delete/$', DeleteServiceView.as_view(),
name='delete-custom-service'),
]

View File

@ -17,7 +17,6 @@
from django.http import HttpResponseRedirect
from django.urls import reverse, reverse_lazy
from django.utils.translation import ugettext_lazy as _
from django.views.generic import TemplateView, View
from django.views.generic.edit import FormView
@ -25,20 +24,7 @@ from plinth.modules import pagekite
from . import utils
from .forms import (AddCustomServiceForm, ConfigurationForm,
DeleteCustomServiceForm, StandardServiceForm)
subsubmenu = [{
'url': reverse_lazy('pagekite:index'),
'text': _('Configure')
},
{
'url': reverse_lazy('pagekite:standard-services'),
'text': _('Standard Services')
},
{
'url': reverse_lazy('pagekite:custom-services'),
'text': _('Custom Services')
}]
DeleteCustomServiceForm)
class ContextMixin(object):
@ -54,7 +40,6 @@ class ContextMixin(object):
context['name'] = pagekite.name
context['description'] = pagekite.description
context['manual_page'] = pagekite.manual_page
context['subsubmenu'] = subsubmenu
return context
def dispatch(self, *args, **kwargs):
@ -66,14 +51,14 @@ class DeleteServiceView(ContextMixin, View):
form = DeleteCustomServiceForm(request.POST)
if form.is_valid():
form.delete(request)
return HttpResponseRedirect(reverse('pagekite:custom-services'))
return HttpResponseRedirect(reverse('pagekite:index'))
class CustomServiceView(ContextMixin, TemplateView):
class AddCustomServiceView(ContextMixin, TemplateView):
template_name = 'pagekite_custom_services.html'
def get_context_data(self, *args, **kwargs):
context = super(CustomServiceView, self).get_context_data(
context = super(AddCustomServiceView, self).get_context_data(
*args, **kwargs)
unused, custom_services = utils.get_pagekite_services()
for service in custom_services:
@ -99,21 +84,7 @@ class CustomServiceView(ContextMixin, TemplateView):
context = self.get_context_data()
context['form'] = form
return self.render_to_response(context)
class StandardServiceView(ContextMixin, FormView):
template_name = 'pagekite_standard_services.html'
form_class = StandardServiceForm
success_url = reverse_lazy('pagekite:standard-services')
def get_initial(self):
return utils.get_pagekite_services()[0]
def form_valid(self, form):
form.save(self.request)
return super(StandardServiceView, self).form_valid(form)
return HttpResponseRedirect(reverse('pagekite:index'))
class ConfigurationView(ContextMixin, FormView):
@ -122,6 +93,19 @@ class ConfigurationView(ContextMixin, FormView):
prefix = 'pagekite'
success_url = reverse_lazy('pagekite:index')
def get_context_data(self, *args, **kwargs):
context = super(ConfigurationView, self).get_context_data(
*args, **kwargs)
unused, custom_services = utils.get_pagekite_services()
for service in custom_services:
service['form'] = AddCustomServiceForm(initial=service)
context['custom_services'] = [
utils.prepare_service_for_display(service)
for service in custom_services
]
context.update(utils.get_kite_details())
return context
def get_initial(self):
return utils.get_pagekite_config()