mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-11-29 08:25:10 +00:00
diagnostics: Add method to translate checks
Signed-off-by: James Valleroy <jvalleroy@mailbox.org> Reviewed-by: Sunil Mohan Adapa <sunil@medhas.org>
This commit is contained in:
1 parent
4612df954d
commit
a5820bc36d
6 files changed
+72
-27
No files matched your search
@@ -4,6 +4,10 @@
|
||||
from dataclasses import dataclass, field
|
||||
from enum import StrEnum
|
||||
|
||||
from django.utils.translation import gettext
|
||||
|
||||
from plinth.utils import SafeFormatter
|
||||
|
||||
|
||||
class Result(StrEnum):
|
||||
"""The result of a diagnostic check."""
|
||||
@@ -24,3 +28,19 @@ class DiagnosticCheck:
|
||||
description: str
|
||||
result: Result = Result.NOT_DONE
|
||||
parameters: dict = field(default_factory=dict)
|
||||
|
||||
|
||||
def translate(check: DiagnosticCheck) -> DiagnosticCheck:
|
||||
"""Translate and format description using parameters."""
|
||||
description = gettext(check.description)
|
||||
if check.parameters:
|
||||
description = SafeFormatter().vformat(description, [],
|
||||
check.parameters)
|
||||
|
||||
return DiagnosticCheck(check.check_id, description, check.result,
|
||||
check.parameters)
|
||||
|
||||
|
||||
def translate_checks(checks: list[DiagnosticCheck]) -> list[DiagnosticCheck]:
|
||||
"""Translate and format diagnostic checks."""
|
||||
return [translate(check) for check in checks]
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
import pytest
|
||||
|
||||
from plinth.modules.diagnostics.check import DiagnosticCheck, Result
|
||||
from plinth.modules.diagnostics.check import DiagnosticCheck, Result, translate
|
||||
|
||||
|
||||
def test_result():
|
||||
@@ -35,3 +35,25 @@ def test_diagnostic_check():
|
||||
check = DiagnosticCheck('some-check-id', 'sample check', Result.PASSED,
|
||||
{'key': 'value'})
|
||||
assert check.parameters['key'] == 'value'
|
||||
|
||||
|
||||
def test_translate():
|
||||
"""Test formatting the translated description."""
|
||||
check = DiagnosticCheck('some-check-id', 'sample check', Result.PASSED)
|
||||
translated = translate(check)
|
||||
assert translated.check_id == 'some-check-id'
|
||||
assert translated.description == 'sample check'
|
||||
assert translated.result == Result.PASSED
|
||||
assert not translated.parameters
|
||||
|
||||
check = DiagnosticCheck('some-check-id', 'sample check {key}',
|
||||
Result.FAILED, {'key': 'value'})
|
||||
translated = translate(check)
|
||||
assert translated.description == 'sample check value'
|
||||
assert translated.result == Result.FAILED
|
||||
assert translated.parameters == {'key': 'value'}
|
||||
|
||||
check = DiagnosticCheck('some-check-id', 'sample check {missing}',
|
||||
Result.PASSED, {'key': 'value'})
|
||||
translated = translate(check)
|
||||
assert translated.description == 'sample check ?missing?'
|
||||
Reference in new issue
Block a user