Skip to content

Commit

Permalink
feat: allow to translate message descriptions
Browse files Browse the repository at this point in the history
  • Loading branch information
hartungstenio committed Oct 20, 2024
1 parent 7341484 commit 0609471
Show file tree
Hide file tree
Showing 9 changed files with 134 additions and 7 deletions.
11 changes: 10 additions & 1 deletion django_healthy/health_checks/cache.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from __future__ import annotations

from typing import TYPE_CHECKING, ClassVar

from django.core.cache import caches
from django.utils.crypto import get_random_string
from django.utils.translation import gettext_lazy as _

from .base import HealthCheck, HealthCheckResult

if TYPE_CHECKING:
from .types import MessageDict


class CacheHealthCheck(HealthCheck):
__slots__: tuple[str, ...] = ("alias", "key_prefix")
messages: ClassVar[MessageDict] = {
"unexpected": _("Got unexpected value"),
}

def __init__(self, alias: str = "default", key_prefix: str = "django_healthy"):
self.alias = alias
Expand All @@ -26,7 +35,7 @@ async def check_health(self) -> HealthCheckResult:
else:
if got != given:
return HealthCheckResult.degraded(
description="Got unexpected value",
description=self.messages["unexpected"].format(given=given, got=got),
data={"given": given, "got": got},
)
return HealthCheckResult.healthy()
17 changes: 13 additions & 4 deletions django_healthy/health_checks/db.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
from __future__ import annotations

from typing import cast
from typing import TYPE_CHECKING, ClassVar, cast

from asgiref.sync import sync_to_async
from django.db import DatabaseError, connections
from django.db.backends.base.base import BaseDatabaseWrapper as DjangoDatabaseWrapper
from django.utils.crypto import get_random_string
from django.utils.translation import gettext_lazy as _

from django_healthy.models import Test

from .base import HealthCheck, HealthCheckResult

if TYPE_CHECKING:
from .types import MessageDict


class DatabasePingHealthCheck(HealthCheck):
__slots__: tuple[str, ...] = ("alias", "query")
Expand All @@ -36,6 +40,11 @@ def _perform_health_check(self) -> None:

class DatabaseModelHealthCheck(HealthCheck):
__slots__: tuple[str, ...] = ("alias",)
messages: ClassVar[MessageDict] = {
"insert": _("Could not insert"),
"update": _("Could not update"),
"delete": _("Could not delete"),
}

def __init__(self, alias: str | None = None):
self.alias = alias
Expand All @@ -45,17 +54,17 @@ async def check_health(self) -> HealthCheckResult:
try:
await instance.asave(using=self.alias)
except DatabaseError as exc:
return HealthCheckResult.unhealthy(description="Could not insert", exception=exc)
return HealthCheckResult.unhealthy(description=self.messages["insert"], exception=exc)

try:
instance.summary = get_random_string(100)
await instance.asave(using=self.alias)
except DatabaseError as exc:
return HealthCheckResult.unhealthy(description="Could not update", exception=exc)
return HealthCheckResult.unhealthy(description=self.messages["update"], exception=exc)

try:
await instance.adelete(using=self.alias)
except DatabaseError as exc:
return HealthCheckResult.unhealthy(description="Could not delete", exception=exc)
return HealthCheckResult.unhealthy(description=self.messages["delete"], exception=exc)

return HealthCheckResult.healthy()
19 changes: 17 additions & 2 deletions django_healthy/health_checks/storage.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
from __future__ import annotations

from io import StringIO
from typing import TYPE_CHECKING, ClassVar

from asgiref.sync import sync_to_async
from django.core.files.storage import Storage, storages
from django.utils.crypto import get_random_string
from django.utils.translation import gettext_lazy as _

from .base import HealthCheck, HealthCheckResult

if TYPE_CHECKING:
from .types import MessageDict


class StorageHealthCheck(HealthCheck):
__slots__: tuple[str, ...] = ("alias", "filename")
messages: ClassVar[MessageDict] = {
"missing": _("File missing"),
"delete": _("Could not delete file"),
}

def __init__(self, alias: str = "default", filename: str = "healthy_test_file.txt"):
self.alias = alias
Expand All @@ -26,12 +35,18 @@ async def check_health(self) -> HealthCheckResult:

exists = await sync_to_async(storage.exists)(filename)
if not exists:
return HealthCheckResult.degraded(description="File missing", data={"filename": filename})
return HealthCheckResult.degraded(
description=self.messages["missing"].format(filename=filename),
data={"filename": filename},
)

await sync_to_async(storage.delete)(filename)
exists = await sync_to_async(storage.exists)(filename)
if exists:
return HealthCheckResult.degraded(description="Could not delete file", data={"filename": filename})
return HealthCheckResult.degraded(
description=self.messages["delete"].format(filename=filename),
data={"filename": filename},
)
except Exception as exc: # noqa: BLE001
return HealthCheckResult.unhealthy(exception=exc)
else:
Expand Down
5 changes: 5 additions & 0 deletions django_healthy/health_checks/types.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from typing import Mapping

from django_healthy._compat import TypeAlias

MessageDict: TypeAlias = Mapping[str, str]
Binary file added django_healthy/locale/en/LC_MESSAGES/django.mo
Binary file not shown.
44 changes: 44 additions & 0 deletions django_healthy/locale/en/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-10-11 21:44+0000\n"
"PO-Revision-Date: 2024-10-11 18:58-0300\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: en\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n != 1);\n"
"X-Generator: Poedit 3.5\n"

#: health_checks/cache.py:16
msgid "Got unexpected value"
msgstr "Got unexpected value"

#: health_checks/db.py:42
msgid "Could not insert"
msgstr "Could not insert"

#: health_checks/db.py:43
msgid "Could not update"
msgstr "Could not update"

#: health_checks/db.py:44
msgid "Could not delete"
msgstr "Could not delete"

#: health_checks/storage.py:18
msgid "File missing"
msgstr "File missing"

#: health_checks/storage.py:19
msgid "Could not delete file"
msgstr "Could not delete file"
Binary file added django_healthy/locale/pt_BR/LC_MESSAGES/django.mo
Binary file not shown.
44 changes: 44 additions & 0 deletions django_healthy/locale/pt_BR/LC_MESSAGES/django.po
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# SOME DESCRIPTIVE TITLE.
# Copyright (C) YEAR THE PACKAGE'S COPYRIGHT HOLDER
# This file is distributed under the same license as the PACKAGE package.
# FIRST AUTHOR <EMAIL@ADDRESS>, YEAR.
#
#, fuzzy
msgid ""
msgstr ""
"Project-Id-Version: \n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2024-10-11 21:53+0000\n"
"PO-Revision-Date: 2024-10-11 19:01-0300\n"
"Last-Translator: \n"
"Language-Team: \n"
"Language: pt_BR\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Plural-Forms: nplurals=2; plural=(n > 1);\n"
"X-Generator: Poedit 3.5\n"

#: health_checks/cache.py:16
msgid "Got unexpected value"
msgstr "Valor inesperado"

#: health_checks/db.py:42
msgid "Could not insert"
msgstr "Falha ao inserir"

#: health_checks/db.py:43
msgid "Could not update"
msgstr "Falha ao atualizar"

#: health_checks/db.py:44
msgid "Could not delete"
msgstr "Falha ao excluir"

#: health_checks/storage.py:18
msgid "File missing"
msgstr "Arquivo não encontrado"

#: health_checks/storage.py:19
msgid "Could not delete file"
msgstr "Falha ao excluir arquivo"
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ exclude_lines = [
[tool.pytest.ini_options]
DJANGO_SETTINGS_MODULE = "testproj.settings"
pythonpath = [".", "src"]
asyncio_default_fixture_loop_scope = "function"

[tool.hatch.build.targets.wheel]
packages = ["django_healthy"]
Expand Down

0 comments on commit 0609471

Please sign in to comment.