From 633f54b75c648c79d2e4a78dc01ec1a5b47c7e67 Mon Sep 17 00:00:00 2001 From: Fioddor Superconcentrado Date: Tue, 13 Oct 2020 21:34:17 +0200 Subject: [PATCH] diagnostics: Lazy format all diagnostic test strings properly Helps: #1938. Fixed application of available translations in daemon.py and apache, diagnostics, networks, firewall and users modules. diagnostics: - __init__.py: return the app name along its results. - diagnostics.html: display the app name instead of its id. - diagnostics_results.html: - mark for translation, - apply class to results HTML tag. main.css: center-align the results. Locale files excluded. Will be regenerated automatically and translations to be done via Weblate. original testing (rebased later): - Yapf applied. - Flake8 without errors or warnings for changed files. - (Unit) tests run without errors. Signed-off-by: Fioddor Superconcentrado [sunil: Translate 'None' app name] [sunil: Don't translate tests strings second time in template] [sunil: Tweak the center rule] [sunil: Don't split a translation string] Signed-off-by: Sunil Mohan Adapa Reviewed-by: Sunil Mohan Adapa --- plinth/daemon.py | 23 +++++++++++-------- plinth/modules/apache/components.py | 16 ++++++++----- plinth/modules/diagnostics/__init__.py | 6 ++++- .../diagnostics/templates/diagnostics.html | 12 ++++++---- .../templates/diagnostics_results.html | 2 +- plinth/modules/firewall/components.py | 23 ++++++++++--------- plinth/modules/networks/__init__.py | 6 ++++- plinth/modules/users/__init__.py | 13 +++++------ static/themes/default/css/main.css | 1 + 9 files changed, 62 insertions(+), 40 deletions(-) diff --git a/plinth/daemon.py b/plinth/daemon.py index eb64eba3b..144eb85d4 100644 --- a/plinth/daemon.py +++ b/plinth/daemon.py @@ -7,7 +7,8 @@ import socket import subprocess import psutil -from django.utils.translation import ugettext as _ +from django.utils.text import format_lazy +from django.utils.translation import ugettext as _, ugettext_lazy from plinth import action_utils, actions, app @@ -82,10 +83,12 @@ class Daemon(app.LeaderComponent): def _diagnose_unit_is_running(self): """Check if a daemon is running.""" - message = _('Service {service_name} is running').format( - service_name=self.unit) result = 'passed' if self.is_running() else 'failed' - return [message, result] + + template = ugettext_lazy('Service {service_name} is running') + testname = format_lazy(template, service_name=self.unit) + + return [testname, result] def app_is_running(app_): @@ -108,13 +111,15 @@ def diagnose_port_listening(port, kind='tcp', listen_address=None): result = _check_port(port, kind, listen_address) if listen_address: - test = _('Listening on {kind} port {listen_address}:{port}') \ - .format(kind=kind, listen_address=listen_address, port=port) + template = ugettext_lazy( + 'Listening on {kind} port {listen_address}:{port}') + testname = format_lazy(template, kind=kind, + listen_address=listen_address, port=port) else: - test = _('Listening on {kind} port {port}') \ - .format(kind=kind, port=port) + template = ugettext_lazy('Listening on {kind} port {port}') + testname = format_lazy(template, kind=kind, port=port) - return [test, 'passed' if result else 'failed'] + return [testname, 'passed' if result else 'failed'] def _check_port(port, kind='tcp', listen_address=None): diff --git a/plinth/modules/apache/components.py b/plinth/modules/apache/components.py index b6e0afccb..82cf0284a 100644 --- a/plinth/modules/apache/components.py +++ b/plinth/modules/apache/components.py @@ -6,13 +6,15 @@ App component for other apps to use Apache configuration functionality. import re import subprocess -from django.utils.translation import ugettext as _ +from django.utils.text import format_lazy +from django.utils.translation import ugettext_lazy from plinth import action_utils, actions, app class Webserver(app.LeaderComponent): """Component to enable/disable Apache configuration.""" + def __init__(self, component_id, web_name, kind='config', urls=None): """Initialize the web server component. @@ -71,6 +73,7 @@ class Webserver(app.LeaderComponent): class Uwsgi(app.LeaderComponent): """Component to enable/disable uWSGI configuration.""" + def __init__(self, component_id, uwsgi_name): """Initialize the uWSGI component. @@ -116,12 +119,13 @@ def diagnose_url(url, kind=None, env=None, check_certificate=True, wrapper, expected_output) if kind: - return [ - _('Access URL {url} on tcp{kind}').format(url=url, kind=kind), - result - ] + template = ugettext_lazy('Access URL {url} on tcp{kind}') + testname = format_lazy(template, url=url, kind=kind) + else: + template = ugettext_lazy('Access URL {url}') + testname = format_lazy(template, url=url) - return [_('Access URL {url}').format(url=url), result] + return [testname, result] def diagnose_url_on_all(url, **kwargs): diff --git a/plinth/modules/diagnostics/__init__.py b/plinth/modules/diagnostics/__init__.py index 43a23b4d2..dc9805a8d 100644 --- a/plinth/modules/diagnostics/__init__.py +++ b/plinth/modules/diagnostics/__init__.py @@ -129,7 +129,11 @@ def run_on_all_enabled_modules(): current_results['apps'] = apps for current_index, (app_id, app) in enumerate(apps): - current_results['results'][app_id] = app.diagnose() + app_name = app.info.name or _('None') + current_results['results'][app_id] = { + 'name': app_name, + 'results': app.diagnose() + } current_results['progress_percentage'] = \ int((current_index + 1) * 100 / len(apps)) diff --git a/plinth/modules/diagnostics/templates/diagnostics.html b/plinth/modules/diagnostics/templates/diagnostics.html index e87525db6..6e8ac9422 100644 --- a/plinth/modules/diagnostics/templates/diagnostics.html +++ b/plinth/modules/diagnostics/templates/diagnostics.html @@ -37,11 +37,15 @@ {{ results.error }} {% else %} - {% for app_id, app_results in results.results.items %} -

{% blocktrans %}App: {{ app_id }}{% endblocktrans %}

+ {% for app_id, app_data in results.results.items %} +

+ {% blocktrans with app_name=app_data.name %} + App: {{app_name}} + {% endblocktrans %} +

- {% if app_results %} - {% include "diagnostics_results.html" with results=app_results %} + {% if app_data.results %} + {% include "diagnostics_results.html" with results=app_data.results %} {% else %}

{% endif %} diff --git a/plinth/modules/diagnostics/templates/diagnostics_results.html b/plinth/modules/diagnostics/templates/diagnostics_results.html index 04c684b93..b34c8a976 100644 --- a/plinth/modules/diagnostics/templates/diagnostics_results.html +++ b/plinth/modules/diagnostics/templates/diagnostics_results.html @@ -15,7 +15,7 @@ {% for test, result in results %} {{ test }} - + {% if result == 'passed' %} {% trans result %} {% elif result == 'failed' %} diff --git a/plinth/modules/firewall/components.py b/plinth/modules/firewall/components.py index 37c4dbc29..a2ffe6064 100644 --- a/plinth/modules/firewall/components.py +++ b/plinth/modules/firewall/components.py @@ -6,6 +6,7 @@ App component for other apps to use firewall functionality. import logging import re +from django.utils.text import format_lazy from django.utils.translation import ugettext_lazy as _ from plinth import app @@ -129,24 +130,24 @@ class Firewall(app.FollowerComponent): # Internal zone result = 'passed' if port in internal_ports else 'failed' - message = _( - 'Port {name} ({details}) available for internal networks' - ).format(name=port, details=details) - results.append([message, result]) + template = _( + 'Port {name} ({details}) available for internal networks') + testname = format_lazy(template, name=port, details=details) + results.append([testname, result]) # External zone if self.is_external: result = 'passed' if port in external_ports else 'failed' - message = _( - 'Port {name} ({details}) available for external networks' - ).format(name=port, details=details) + template = _( + 'Port {name} ({details}) available for external networks') + testname = format_lazy(template, name=port, details=details) else: result = 'passed' if port not in external_ports else 'failed' - message = _( + template = _( 'Port {name} ({details}) unavailable for external networks' - ).format(name=port, details=details) - - results.append([message, result]) + ) + testname = format_lazy(template, name=port, details=details) + results.append([testname, result]) return results diff --git a/plinth/modules/networks/__init__.py b/plinth/modules/networks/__init__.py index e5c773e2a..044669234 100644 --- a/plinth/modules/networks/__init__.py +++ b/plinth/modules/networks/__init__.py @@ -6,6 +6,7 @@ FreedomBox app to interface with network-manager. import subprocess from logging import Logger +from django.utils.text import format_lazy from django.utils.translation import ugettext_lazy as _ from plinth import actions @@ -175,4 +176,7 @@ def _diagnose_dnssec(kind='4'): except subprocess.CalledProcessError: pass - return [_('Using DNSSEC on IPv{kind}').format(kind=kind), result] + template = _('Using DNSSEC on IPv{kind}') + testname = format_lazy(template, kind=kind) + + return [testname, result] diff --git a/plinth/modules/users/__init__.py b/plinth/modules/users/__init__.py index fdb1e8f03..7eee772a4 100644 --- a/plinth/modules/users/__init__.py +++ b/plinth/modules/users/__init__.py @@ -6,13 +6,12 @@ FreedomBox app to manage users. import grp import subprocess -from django.utils.translation import ugettext_lazy as _ - from plinth import actions from plinth import app as app_module from plinth import cfg, menu from plinth.daemon import Daemon -from plinth.utils import format_lazy +from django.utils.text import format_lazy +from django.utils.translation import ugettext_lazy as _, ugettext_lazy from .components import UsersAndGroups @@ -111,10 +110,10 @@ def _diagnose_ldap_entry(search_item): except subprocess.CalledProcessError: pass - return [ - _('Check LDAP entry "{search_item}"').format(search_item=search_item), - result - ] + template = ugettext_lazy('Check LDAP entry "{search_item}"') + testname = format_lazy(template, search_item=search_item) + + return [testname, result] def create_group(group): diff --git a/static/themes/default/css/main.css b/static/themes/default/css/main.css index 3b258e1c4..ca05b65c3 100644 --- a/static/themes/default/css/main.css +++ b/static/themes/default/css/main.css @@ -177,6 +177,7 @@ body { .diagnostics-results .diagnostics-result { width: 60px; + text-align: center; } /* Sticky footer styles