From bdb090e386835190c945ce5884267f51542dd6af Mon Sep 17 00:00:00 2001 From: Matt Conroy Date: Mon, 18 Nov 2019 00:45:18 -0500 Subject: [PATCH] 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 Reviewed-by: Joseph Nuthalapati --- plinth/modules/pagekite/forms.py | 55 ++++------- plinth/modules/pagekite/static/pagekite.js | 34 ------- .../templates/pagekite_configure.html | 81 ++++++++++++---- .../templates/pagekite_custom_services.html | 93 +++---------------- plinth/modules/pagekite/urls.py | 9 +- plinth/modules/pagekite/views.py | 52 ++++------- 6 files changed, 117 insertions(+), 207 deletions(-) delete mode 100644 plinth/modules/pagekite/static/pagekite.js diff --git a/plinth/modules/pagekite/forms.py b/plinth/modules/pagekite/forms.py index 5c1710417..e9286c508 100644 --- a/plinth/modules/pagekite/forms.py +++ b/plinth/modules/pagekite/forms.py @@ -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')] diff --git a/plinth/modules/pagekite/static/pagekite.js b/plinth/modules/pagekite/static/pagekite.js deleted file mode 100644 index 5cba2f6b7..000000000 --- a/plinth/modules/pagekite/static/pagekite.js +++ /dev/null @@ -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 . - * - * @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); diff --git a/plinth/modules/pagekite/templates/pagekite_configure.html b/plinth/modules/pagekite/templates/pagekite_configure.html index 9cc06835c..d9be91fef 100644 --- a/plinth/modules/pagekite/templates/pagekite_configure.html +++ b/plinth/modules/pagekite/templates/pagekite_configure.html @@ -21,29 +21,74 @@ {% load bootstrap %} {% load i18n %} {% load static %} +{% load pagekite_extras %} + +{% block page_head %} + + +{% endblock %} {% block configuration %} +{{ block.super }} -
- {% csrf_token %} +

{% trans "Custom Services" %}

- {% include 'bootstrapform/field.html' with field=form.enabled %} + + + {% trans 'Add Custom Service' %} + -
-

{% trans "PageKite Account" %}

- {{ form.server_domain|bootstrap_horizontal }} - {{ form.server_port|bootstrap_horizontal }} - {{ form.kite_name|bootstrap_horizontal }} - {{ form.kite_secret|bootstrap_horizontal }} +
+

{% trans "Existing custom services" %}

+ + {% if not custom_services %} + {% trans "You don't have any Custom Services enabled" %} + {% endif %} + +
+ {% for service in custom_services %} + {% create_pagekite_service_url service kite_name as service_url %} +
+ + + {% if service_url|slice:":4" == "http" %} + {{ service_url }} + {% else %} + {{ service_url }} + {% endif %} +
+ {% blocktrans trimmed with backend_host=service.backend_host backend_port=service.backend_port %} + connected to {{ backend_host }}:{{ backend_port }} + {% endblocktrans %} +
+
+ +
+ {% csrf_token %} + {{ service.form.as_p }} +
+ + +
+ {% endfor %} +
- - - -{% endblock %} - -{% block page_js %} - {% endblock %} diff --git a/plinth/modules/pagekite/templates/pagekite_custom_services.html b/plinth/modules/pagekite/templates/pagekite_custom_services.html index 84693c846..918d4c6e5 100644 --- a/plinth/modules/pagekite/templates/pagekite_custom_services.html +++ b/plinth/modules/pagekite/templates/pagekite_custom_services.html @@ -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 %} - -{% endblock %} -{% block configuration %} +{% block content %} +

{% trans 'Add custom PageKite service' %}

+ -
+
+ {% csrf_token %} -
- -

{% trans "Create a custom service" %}

- {{ form|bootstrap_horizontal:'col-lg-6' }} + {{ form|bootstrap }} - {% csrf_token %} - -
-
- -
-
- -
- -
-

{% trans "Existing custom services" %}

- - {% if not custom_services %} - {% trans "You don't have any Custom Services enabled" %} - {% endif %} - -
- {% for service in custom_services %} - {% create_pagekite_service_url service kite_name as service_url %} -
- - - {% if service_url|slice:":4" == "http" %} - {{ service_url }} - {% else %} - {{ service_url }} - {% endif %} -
- {% blocktrans trimmed with backend_host=service.backend_host backend_port=service.backend_port %} - connected to {{ backend_host }}:{{ backend_port }} - {% endblocktrans %} -
-
-
-
- {% csrf_token %} - {{ service.form.as_p }} -
- -
-
- {% endfor %} -
-
- -
+ + {% endblock %} diff --git a/plinth/modules/pagekite/urls.py b/plinth/modules/pagekite/urls.py index 3dc4bf949..8eb83486d 100644 --- a/plinth/modules/pagekite/urls.py +++ b/plinth/modules/pagekite/urls.py @@ -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'), ] diff --git a/plinth/modules/pagekite/views.py b/plinth/modules/pagekite/views.py index 7b892576f..f3351f27c 100644 --- a/plinth/modules/pagekite/views.py +++ b/plinth/modules/pagekite/views.py @@ -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()