From 9b6774f279e2c8af588609c2413aa9804fd48cfa Mon Sep 17 00:00:00 2001 From: Sunil Mohan Adapa Date: Fri, 19 Aug 2022 23:10:04 -0700 Subject: [PATCH] snapshot: Use AppView for app page Tests: - Enable/disable button is not shown. - Diagnostics menu item is shown and works. - Both Configure and manage snapshots tabs are shown. - Changing configuration works, updated configuration is shown. - Deleting some snapshots works. Signed-off-by: Sunil Mohan Adapa Reviewed-by: James Valleroy --- plinth/modules/snapshot/__init__.py | 2 + .../modules/snapshot/templates/snapshot.html | 3 +- .../modules/snapshot/tests/test_functional.py | 6 ++- plinth/modules/snapshot/urls.py | 2 +- plinth/modules/snapshot/views.py | 45 ++++++++++--------- 5 files changed, 33 insertions(+), 25 deletions(-) diff --git a/plinth/modules/snapshot/__init__.py b/plinth/modules/snapshot/__init__.py index 015bac1f5..c16ccb4d6 100644 --- a/plinth/modules/snapshot/__init__.py +++ b/plinth/modules/snapshot/__init__.py @@ -44,6 +44,8 @@ class SnapshotApp(app_module.App): _version = 4 + can_be_disabled = False + def __init__(self): """Create components for the app.""" super().__init__() diff --git a/plinth/modules/snapshot/templates/snapshot.html b/plinth/modules/snapshot/templates/snapshot.html index bbf0b9da2..36c153f70 100644 --- a/plinth/modules/snapshot/templates/snapshot.html +++ b/plinth/modules/snapshot/templates/snapshot.html @@ -7,7 +7,8 @@ {% load i18n %} {% block configuration %} -
+ {% csrf_token %}

{% trans "Configuration" %}

{{ form|bootstrap_horizontal:'col-md-4' }} diff --git a/plinth/modules/snapshot/tests/test_functional.py b/plinth/modules/snapshot/tests/test_functional.py index 7bdfc8ec8..e8fac1a55 100644 --- a/plinth/modules/snapshot/tests/test_functional.py +++ b/plinth/modules/snapshot/tests/test_functional.py @@ -4,6 +4,7 @@ Functional, browser based tests for snapshot app. """ import pytest + from plinth.tests import functional pytestmark = [pytest.mark.system, pytest.mark.snapshot] @@ -73,7 +74,8 @@ def _delete_all(browser): def _create_snapshot(browser): functional.visit(browser, '/plinth/sys/snapshot/manage/') - functional.submit(browser) # Click on 'Create Snapshot' + create_button = browser.find_by_name('create').first + functional.submit(browser, create_button) def _get_count(browser): @@ -102,7 +104,7 @@ def _set_configuration(browser, free_space, timeline_enabled, software_enabled, browser.find_by_name('weekly_limit').fill(weekly) browser.find_by_name('monthly_limit').fill(monthly) browser.find_by_name('yearly_limit').fill(yearly) - functional.submit(browser) + functional.submit(browser, form_class='form-configuration') def _get_configuration(browser): diff --git a/plinth/modules/snapshot/urls.py b/plinth/modules/snapshot/urls.py index 0eb25c744..f1d2dcdbe 100644 --- a/plinth/modules/snapshot/urls.py +++ b/plinth/modules/snapshot/urls.py @@ -8,7 +8,7 @@ from django.urls import re_path from . import views urlpatterns = [ - re_path(r'^sys/snapshot/$', views.index, name='index'), + re_path(r'^sys/snapshot/$', views.SnapshotAppView.as_view(), name='index'), re_path(r'^sys/snapshot/manage/$', views.manage, name='manage'), re_path(r'^sys/snapshot/selected/delete$', views.delete_selected, name='delete-selected'), diff --git a/plinth/modules/snapshot/views.py b/plinth/modules/snapshot/views.py index 960383b9f..e40df217a 100644 --- a/plinth/modules/snapshot/views.py +++ b/plinth/modules/snapshot/views.py @@ -19,6 +19,7 @@ from plinth import app as app_module from plinth.errors import ActionError from plinth.modules import snapshot as snapshot_module from plinth.modules import storage +from plinth.views import AppView from . import get_configuration from .forms import SnapshotForm @@ -55,29 +56,31 @@ def not_supported_view(request): template_data) -def index(request): - """Show snapshot list.""" - if not snapshot_module.is_supported(): - return not_supported_view(request) +class SnapshotAppView(AppView): + """Show snapshot app main page.""" - status = get_configuration() - if request.method == 'POST': - form = SnapshotForm(request.POST) - if 'update' in request.POST and form.is_valid(): - update_configuration(request, status, form.cleaned_data) - status = get_configuration() - form = SnapshotForm(initial=status) - else: - form = SnapshotForm(initial=status) + app_id = 'snapshot' + template_name = 'snapshot.html' + form_class = SnapshotForm - app = app_module.App.get('snapshot') - return TemplateResponse( - request, 'snapshot.html', { - 'app_info': app.info, - 'title': app.info.name, - 'subsubmenu': subsubmenu, - 'form': form - }) + def get_initial(self): + """Return the values to fill in the form.""" + initial = super().get_initial() + initial.update(get_configuration()) + return initial + + def get_context_data(self, *args, **kwargs): + """Add additional context data for template.""" + context = super().get_context_data(*args, **kwargs) + context['subsubmenu'] = subsubmenu + return context + + def form_valid(self, form): + """Apply the changes submitted in the form.""" + if 'update' in self.request.POST: + update_configuration(self.request, form.initial, form.cleaned_data) + + return super().form_valid(form) def manage(request):