From 6a6983f39ef8b17521843a83551968d40c395ff0 Mon Sep 17 00:00:00 2001
From: Sunil Mohan Adapa
Date: Wed, 23 Oct 2019 11:22:53 -0700
Subject: [PATCH] storage: Use AppView and cleanup custom code
- Follow common code so that extending becomes easier.
Signed-off-by: Sunil Mohan Adapa
Reviewed-by: James Valleroy
---
plinth/modules/storage/templates/storage.html | 27 +---------
plinth/modules/storage/urls.py | 2 +-
plinth/modules/storage/views.py | 51 +++++++++++--------
plinth/views.py | 3 +-
4 files changed, 35 insertions(+), 48 deletions(-)
diff --git a/plinth/modules/storage/templates/storage.html b/plinth/modules/storage/templates/storage.html
index 93f028691..dc2b162ee 100644
--- a/plinth/modules/storage/templates/storage.html
+++ b/plinth/modules/storage/templates/storage.html
@@ -1,4 +1,4 @@
-{% extends "base.html" %}
+{% extends "app.html" %}
{% comment %}
#
# This file is part of FreedomBox.
@@ -30,25 +30,7 @@
{% endblock %}
-{% block content %}
-
- {% block pagetitle %}
- {{ title }}
- {% endblock %}
-
- {% block description %}
- {% for paragraph in description %}
- {{ paragraph|safe }}
- {% endfor %}
- {% endblock %}
-
- {% if manual_page %}
-
-
- {% trans 'Learn more...' %}
-
-
- {% endif %}
+{% block configuration %}
{% trans "The following storage devices are in use:" %}
@@ -121,9 +103,4 @@
{% endif %}
- {% block status %}
-
- {{ block.super }}
-
- {% endblock %}
{% endblock %}
diff --git a/plinth/modules/storage/urls.py b/plinth/modules/storage/urls.py
index b39b07bb4..493d6e894 100644
--- a/plinth/modules/storage/urls.py
+++ b/plinth/modules/storage/urls.py
@@ -23,7 +23,7 @@ from django.conf.urls import url
from . import views
urlpatterns = [
- url(r'^sys/storage/$', views.index, name='index'),
+ url(r'^sys/storage/$', views.StorageAppView.as_view(), name='index'),
url(r'^sys/storage/expand$', views.expand, name='expand'),
url(r'^sys/storage/eject/(?P[A-Za-z0-9%_.\-~]+)/$',
views.eject, name='eject')
diff --git a/plinth/modules/storage/views.py b/plinth/modules/storage/views.py
index 5afd5b922..880ee1b2e 100644
--- a/plinth/modules/storage/views.py
+++ b/plinth/modules/storage/views.py
@@ -29,7 +29,7 @@ from django.urls import reverse
from django.utils.translation import ugettext as _
from django.views.decorators.http import require_POST
-from plinth import actions
+from plinth import actions, views
from plinth.errors import PlinthError
from plinth.modules import storage
from plinth.utils import format_lazy, is_user_admin
@@ -39,23 +39,32 @@ from . import get_disk_info, get_error_message
logger = logging.getLogger(__name__)
-def index(request):
- """Show connection list."""
- disks = storage.get_disks()
- root_device = storage.get_root_device(disks)
- expandable_root_size = storage.is_expandable(root_device)
- expandable_root_size = storage.format_bytes(expandable_root_size)
+class StorageAppView(views.AppView):
+ """Show storage information."""
+ name = storage.name
+ description = storage.description
+ manual_page = storage.manual_page
+ app_id = 'storage'
+ template_name = 'storage.html'
+ show_status_block = False
- warn_about_low_disk_space(request)
+ def render_to_response(self, context, **response_kwargs):
+ """Add disk space warning to the view."""
+ warn_about_low_disk_space(self.request)
+ return super().render_to_response(context, **response_kwargs)
- return TemplateResponse(
- request, 'storage.html', {
- 'title': _('Storage'),
- 'description': storage.description,
- 'disks': disks,
- 'manual_page': storage.manual_page,
- 'expandable_root_size': expandable_root_size
- })
+ def get_context_data(self, *args, **kwargs):
+ """Return template context data."""
+ context = super().get_context_data(*args, **kwargs)
+
+ disks = storage.get_disks()
+ root_device = storage.get_root_device(disks)
+ expandable_root_size = storage.is_expandable(root_device)
+ expandable_root_size = storage.format_bytes(expandable_root_size)
+
+ context['disks'] = disks
+ context['expandable_root_size'] = expandable_root_size
+ return context
def expand(request):
@@ -83,8 +92,8 @@ def expand_partition(request, device):
except Exception as exception:
messages.error(
request,
- _('Error expanding partition: {exception}')
- .format(exception=exception))
+ _('Error expanding partition: {exception}').format(
+ exception=exception))
else:
messages.success(request, _('Partition expanded successfully.'))
@@ -129,9 +138,9 @@ def eject(request, device_path):
if drive:
messages.success(
request,
- _('{drive_vendor} {drive_model} can be safely unplugged.')
- .format(drive_vendor=drive['vendor'],
- drive_model=drive['model']))
+ _('{drive_vendor} {drive_model} can be safely unplugged.').
+ format(drive_vendor=drive['vendor'],
+ drive_model=drive['model']))
else:
messages.success(request, _('Device can be safely unplugged.'))
except Exception as exception:
diff --git a/plinth/views.py b/plinth/views.py
index 42aaae7e0..57d811603 100644
--- a/plinth/views.py
+++ b/plinth/views.py
@@ -34,7 +34,6 @@ from plinth import package
from plinth.app import App
from plinth.daemon import app_is_running
from plinth.modules.config import get_advanced_mode
-from plinth.modules.storage import views as disk_views
from plinth.translation import get_language_from_request, set_language
from . import forms, frontpage
@@ -54,6 +53,7 @@ def index(request):
]
selected_shortcut = selected_shortcut[0] if selected_shortcut else None
+ from plinth.modules.storage import views as disk_views
disk_views.warn_about_low_disk_space(request)
return TemplateResponse(
@@ -77,6 +77,7 @@ class AppsIndexView(TemplateView):
def system_index(request):
"""Serve the system index page."""
+ from plinth.modules.storage import views as disk_views
disk_views.warn_about_low_disk_space(request)
return TemplateResponse(request, 'system.html',
{'advanced_mode': get_advanced_mode()})