From 07df62854bf2d5a52b3460d2d1e0c6c4b0020d96 Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Sun, 28 Feb 2016 14:54:06 +0530 Subject: [PATCH] restore: Use common view for configuration - Rename the template from restore_index.html to restore.html to avoid having to create a custom view. --- plinth/modules/restore/__init__.py | 17 ++++- plinth/modules/restore/forms.py | 30 -------- .../{restore_index.html => restore.html} | 0 plinth/modules/restore/urls.py | 5 +- plinth/modules/restore/views.py | 70 ------------------- 5 files changed, 19 insertions(+), 103 deletions(-) delete mode 100644 plinth/modules/restore/forms.py rename plinth/modules/restore/templates/{restore_index.html => restore.html} (100%) delete mode 100644 plinth/modules/restore/views.py diff --git a/plinth/modules/restore/__init__.py b/plinth/modules/restore/__init__.py index dff18b3d4..532cabd0a 100644 --- a/plinth/modules/restore/__init__.py +++ b/plinth/modules/restore/__init__.py @@ -20,11 +20,12 @@ Plinth module to configure reStore. """ from django.utils.translation import ugettext_lazy as _ + +from plinth import actions from plinth import action_utils, cfg from plinth import service as service_module from plinth.utils import format_lazy -service = None version = 1 @@ -45,6 +46,8 @@ description = [ 'reStore web-interface.') ] +service = None + def init(): """Initialize the reStore module.""" @@ -62,6 +65,18 @@ def setup(helper, old_version=None): helper.install(['node-restore']) +def get_status(): + """Get the current settings.""" + return {'enabled': is_enabled()} + + def is_enabled(): """Return whether the module is enabled.""" return action_utils.service_is_enabled('node-restore') + + +def enable(should_enable): + """Enable/disable the module.""" + sub_command = 'enable' if should_enable else 'disable' + actions.superuser_run('restore', [sub_command]) + service.notify_enabled(None, should_enable) diff --git a/plinth/modules/restore/forms.py b/plinth/modules/restore/forms.py deleted file mode 100644 index 4c8c2bf90..000000000 --- a/plinth/modules/restore/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 reStore. -""" - -from django import forms -from django.utils.translation import ugettext_lazy as _ - - -class ReStoreForm(forms.Form): - """reStore configuration form.""" - enabled = forms.BooleanField( - label=_('Enable reStore'), - required=False) diff --git a/plinth/modules/restore/templates/restore_index.html b/plinth/modules/restore/templates/restore.html similarity index 100% rename from plinth/modules/restore/templates/restore_index.html rename to plinth/modules/restore/templates/restore.html diff --git a/plinth/modules/restore/urls.py b/plinth/modules/restore/urls.py index 69cec1a51..c51116f8f 100644 --- a/plinth/modules/restore/urls.py +++ b/plinth/modules/restore/urls.py @@ -21,9 +21,10 @@ URLs for the reStore module. from django.conf.urls import url -from . import views +from plinth.views import ConfigurationView urlpatterns = [ - url(r'^apps/restore/$', views.index, name='index'), + url(r'^apps/restore/$', ConfigurationView.as_view(module_name='restore'), + name='index'), ] diff --git a/plinth/modules/restore/views.py b/plinth/modules/restore/views.py deleted file mode 100644 index 0e91904a4..000000000 --- a/plinth/modules/restore/views.py +++ /dev/null @@ -1,70 +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 reStore. -""" - -from django.contrib import messages -from django.template.response import TemplateResponse -from django.utils.translation import ugettext as _ - -from .forms import ReStoreForm -from plinth import actions -from plinth.modules import restore - - -def index(request): - """Serve configuration page.""" - status = get_status() - - if request.method == 'POST': - form = ReStoreForm(request.POST, prefix='restore') - if form.is_valid(): - _apply_changes(request, status, form.cleaned_data) - status = get_status() - form = ReStoreForm(initial=status, prefix='restore') - else: - form = ReStoreForm(initial=status, prefix='restore') - - return TemplateResponse(request, 'restore_index.html', - {'title': restore.title, - 'description': restore.description, - 'status': status, - 'form': form}) - - -def get_status(): - """Get the current settings.""" - status = {'enabled': restore.is_enabled()} - 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('restore', [sub_command]) - restore.service.notify_enabled(None, new_status['enabled']) - modified = True - - if modified: - messages.success(request, _('Configuration updated')) - else: - messages.info(request, _('Setting unchanged'))