From 6a462cecc8b83d2afb9c96730e99e476576c1f88 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sun, 28 Feb 2016 15:11:48 +0530 Subject: [PATCH] privoxy: Use common view for configuration --- plinth/modules/privoxy/__init__.py | 13 +++++ plinth/modules/privoxy/forms.py | 30 ------------ plinth/modules/privoxy/urls.py | 5 +- plinth/modules/privoxy/views.py | 78 ------------------------------ 4 files changed, 16 insertions(+), 110 deletions(-) delete mode 100644 plinth/modules/privoxy/forms.py delete mode 100644 plinth/modules/privoxy/views.py diff --git a/plinth/modules/privoxy/__init__.py b/plinth/modules/privoxy/__init__.py index eaf0d20d1..4416d8f53 100644 --- a/plinth/modules/privoxy/__init__.py +++ b/plinth/modules/privoxy/__init__.py @@ -71,6 +71,12 @@ def setup(helper, old_version=None): helper.call('post', service.notify_enabled, None, True) +def get_status(): + """Get the current settings from server.""" + return {'enabled': is_enabled(), + 'is_running': is_running()} + + def is_enabled(): """Return whether the module is enabled.""" return action_utils.service_is_enabled('privoxy') @@ -81,6 +87,13 @@ def is_running(): return action_utils.service_is_running('privoxy') +def enable(should_enable): + """Enable/disable the module.""" + sub_command = 'enable' if should_enable else 'disable' + actions.superuser_run('privoxy', [sub_command]) + service.notify_enabled(None, should_enable) + + def diagnose(): """Run diagnostics and return the results.""" results = [] diff --git a/plinth/modules/privoxy/forms.py b/plinth/modules/privoxy/forms.py deleted file mode 100644 index e22b99d27..000000000 --- a/plinth/modules/privoxy/forms.py +++ /dev/null @@ -1,30 +0,0 @@ -# -# This file is part of Plinth. -# -# 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 . -# - -""" -Forms for configuring Privoxy. -""" - -from django import forms -from django.utils.translation import ugettext_lazy as _ - - -class PrivoxyForm(forms.Form): - """Privoxy configuration form.""" - enabled = forms.BooleanField( - label=_('Enable Privoxy'), - required=False) diff --git a/plinth/modules/privoxy/urls.py b/plinth/modules/privoxy/urls.py index 3ca303381..a4a60ca39 100644 --- a/plinth/modules/privoxy/urls.py +++ b/plinth/modules/privoxy/urls.py @@ -21,9 +21,10 @@ URLs for the Privoxy module. from django.conf.urls import url -from . import views +from plinth.views import ConfigurationView urlpatterns = [ - url(r'^apps/privoxy/$', views.index, name='index'), + url(r'^apps/privoxy/$', ConfigurationView.as_view(module_name='privoxy'), + name='index'), ] diff --git a/plinth/modules/privoxy/views.py b/plinth/modules/privoxy/views.py deleted file mode 100644 index 853f8709f..000000000 --- a/plinth/modules/privoxy/views.py +++ /dev/null @@ -1,78 +0,0 @@ -# -# This file is part of Plinth. -# -# 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 . -# - -""" -Plinth module for configuring Privoxy Server. -""" - -from django.contrib import messages -from django.template.response import TemplateResponse -from django.utils.translation import ugettext as _ -import logging - -from .forms import PrivoxyForm -from plinth import actions -from plinth.modules import privoxy - -logger = logging.getLogger(__name__) - - -def index(request): - """Serve configuration page.""" - status = get_status() - - form = None - - if request.method == 'POST': - form = PrivoxyForm(request.POST, prefix='privoxy') - # pylint: disable=E1101 - if form.is_valid(): - _apply_changes(request, status, form.cleaned_data) - status = get_status() - form = PrivoxyForm(initial=status, prefix='privoxy') - else: - form = PrivoxyForm(initial=status, prefix='privoxy') - - return TemplateResponse(request, 'privoxy.html', - {'title': privoxy.title, - 'description': privoxy.description, - 'status': status, - 'form': form}) - - -def get_status(): - """Get the current settings from server.""" - status = {'enabled': privoxy.is_enabled(), - 'is_running': privoxy.is_running()} - - return status - - -def _apply_changes(request, old_status, new_status): - """Apply the changes.""" - modified = False - - if old_status['enabled'] != new_status['enabled']: - sub_command = 'enable' if new_status['enabled'] else 'disable' - actions.superuser_run('privoxy', [sub_command]) - privoxy.service.notify_enabled(None, new_status['enabled']) - modified = True - - if modified: - messages.success(request, _('Configuration updated')) - else: - messages.info(request, _('Setting unchanged'))