diff --git a/plinth/modules/diagnostics/__init__.py b/plinth/modules/diagnostics/__init__.py index 473fd86fe..cdef381a5 100644 --- a/plinth/modules/diagnostics/__init__.py +++ b/plinth/modules/diagnostics/__init__.py @@ -68,7 +68,7 @@ class DiagnosticsApp(app_module.App): glib.schedule(3600, _warn_about_low_ram_space) # Run diagnostics once a day - glib.schedule(24 * 3600, start_diagnostics, in_thread=False) + glib.schedule(24 * 3600, _daily_diagnostics_run, in_thread=False) def setup(self, old_version): """Install and configure the app.""" @@ -246,6 +246,15 @@ def _warn_about_low_ram_space(request): actions=actions, data=data, group='admin') +def _daily_diagnostics_run(data: None = None): + """Start daily run if enabled.""" + if is_daily_run_enabled(): + logger.info('Starting daily diagnostics run') + start_diagnostics() + else: + logger.info('Skipping daily diagnostics run (disabled)') + + def start_diagnostics(data: None = None): """Start full diagnostics as a background operation.""" logger.info('Running full diagnostics') @@ -343,3 +352,13 @@ def get_results(): results['results'][app_id]['name'] = app.info.name or app_id return results + + +def is_daily_run_enabled() -> bool: + """Return whether daily run is enabled.""" + return kvstore.get_default('diagnostics_daily_run_enabled', True) + + +def set_daily_run_enabled(enabled: bool): + """Enable or disable daily run.""" + kvstore.set('diagnostics_daily_run_enabled', enabled) diff --git a/plinth/modules/diagnostics/forms.py b/plinth/modules/diagnostics/forms.py new file mode 100644 index 000000000..21f2ba25a --- /dev/null +++ b/plinth/modules/diagnostics/forms.py @@ -0,0 +1,12 @@ +# SPDX-License-Identifier: AGPL-3.0-or-later +"""Forms for configuring diagnostics.""" + +from django import forms +from django.utils.translation import gettext_lazy as _ + + +class ConfigureForm(forms.Form): + """Configuration form to enable/disable daily diagnostics run.""" + daily_run_enabled = forms.BooleanField( + label=_('Enable daily run'), required=False, + help_text=_('When enabled, diagnostic checks will run once a day.')) diff --git a/plinth/modules/diagnostics/templates/diagnostics.html b/plinth/modules/diagnostics/templates/diagnostics.html index 03412c641..b7fd620c0 100644 --- a/plinth/modules/diagnostics/templates/diagnostics.html +++ b/plinth/modules/diagnostics/templates/diagnostics.html @@ -6,14 +6,15 @@ {% load i18n %} {% load static %} -{% block configuration %} +{% block extra_content %} +

{% trans "Diagnostics Run" %}

{% csrf_token %} - +
{% if results_available %} diff --git a/plinth/modules/diagnostics/views.py b/plinth/modules/diagnostics/views.py index f5b73103f..c9bd1ab8b 100644 --- a/plinth/modules/diagnostics/views.py +++ b/plinth/modules/diagnostics/views.py @@ -5,9 +5,9 @@ FreedomBox app for running diagnostics. import logging -from django.http import Http404, HttpResponseRedirect +from django.contrib import messages +from django.http import Http404 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 django.views.generic import TemplateView @@ -18,6 +18,7 @@ from plinth.modules import diagnostics from plinth.views import AppView from .check import Result +from .forms import ConfigureForm logger = logging.getLogger(__name__) @@ -26,13 +27,9 @@ class DiagnosticsView(AppView): """Diagnostics app page.""" app_id = 'diagnostics' + form_class = ConfigureForm template_name = 'diagnostics.html' - def post(self, request): - """Start diagnostics.""" - diagnostics.start_diagnostics() - return HttpResponseRedirect(reverse('diagnostics:index')) - def get_context_data(self, **kwargs): """Return additional context for rendering the template.""" context = super().get_context_data(**kwargs) @@ -40,6 +37,22 @@ class DiagnosticsView(AppView): context['results_available'] = diagnostics.are_results_available() return context + def get_initial(self): + """Return the initial values for the form.""" + status = super().get_initial() + status['daily_run_enabled'] = diagnostics.is_daily_run_enabled() + return status + + def form_valid(self, form): + """Apply the form changes.""" + old_status = form.initial + new_status = form.cleaned_data + if old_status['daily_run_enabled'] != new_status['daily_run_enabled']: + diagnostics.set_daily_run_enabled(new_status['daily_run_enabled']) + messages.success(self.request, _('Configuration updated.')) + + return super().form_valid(form) + class DiagnosticsFullView(TemplateView): """View to run full diagnostics."""