diagnostics: Use AppView for app page

Tests:

- Enable/disable button is not present.

- Diagnostics menu item is not present.

- Page shows 'Running diagnostics' button when diagnostics are not running.

- Page shows progress bar and results when diagnostics are running/completed.

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: James Valleroy <jvalleroy@mailbox.org>
This commit is contained in:
Sunil Mohan Adapa 2022-08-19 16:27:25 -07:00 committed by James Valleroy
parent 2eca38299f
commit b970b72bb0
No known key found for this signature in database
GPG Key ID: 77C0C75E7B650808
4 changed files with 31 additions and 17 deletions

View File

@ -41,6 +41,8 @@ class DiagnosticsApp(app_module.App):
_version = 1
can_be_disabled = False
def __init__(self):
"""Create components for the app."""
super().__init__()

View File

@ -8,7 +8,7 @@
{% block configuration %}
{% if not is_running %}
{% if not is_task_running %}
<form class="form form-run-diagnostics" method="post"
action="{% url 'diagnostics:index' %}">
{% csrf_token %}

View File

@ -8,7 +8,8 @@ from django.urls import re_path
from . import views
urlpatterns = [
re_path(r'^sys/diagnostics/$', views.index, name='index'),
re_path(r'^sys/diagnostics/$', views.DiagnosticsView.as_view(),
name='index'),
re_path(r'^sys/diagnostics/(?P<app_id>[1-9a-z\-_]+)/$', views.diagnose_app,
name='app'),
]

View File

@ -5,33 +5,44 @@ FreedomBox app for running diagnostics.
import logging
from django.http import Http404
from django.http import Http404, HttpResponseRedirect
from django.template.response import TemplateResponse
from django.urls import reverse
from django.utils.translation import gettext_lazy as _
from django.views.decorators.http import require_POST
from plinth.app import App
from plinth.modules import diagnostics
from plinth.views import AppView
logger = logging.getLogger(__name__)
def index(request):
"""Serve the index page"""
if request.method == 'POST' and not diagnostics.running_task:
diagnostics.start_task()
class DiagnosticsView(AppView):
"""Diagnostics app page."""
is_running = diagnostics.running_task is not None
with diagnostics.results_lock:
results = diagnostics.current_results
app_id = 'diagnostics'
template_name = 'diagnostics.html'
return TemplateResponse(
request, 'diagnostics.html', {
'app_info': App.get('diagnostics').info,
'is_running': is_running,
'results': results,
'refresh_page_sec': 3 if is_running else None
})
def post(self, request):
"""Start diagnostics."""
if not diagnostics.running_task:
diagnostics.start_task()
return HttpResponseRedirect(reverse('diagnostics:index'))
def get_context_data(self, **kwargs):
"""Return additional context for rendering the template."""
is_task_running = diagnostics.running_task is not None
with diagnostics.results_lock:
results = diagnostics.current_results
context = super().get_context_data(**kwargs)
context['has_diagnostics'] = False
context['is_task_running'] = is_task_running
context['results'] = results
context['refresh_page_sec'] = 3 if is_task_running else None
return context
@require_POST