From 47d1a0f4a4ce5343adf9b5fc39ed238a5d0ca0eb Mon Sep 17 00:00:00 2001 From: Inex Code Date: Thu, 1 Jun 2023 22:42:27 +0300 Subject: [PATCH] refactor(i10l): Move services string localization to API layer --- .../graphql/common_types/service.py | 28 ++++++++------ .../graphql/mutations/services_mutations.py | 38 ++++++++++++------- selfprivacy_api/graphql/queries/services.py | 4 +- .../services/bitwarden/__init__.py | 4 +- selfprivacy_api/services/gitea/__init__.py | 4 +- selfprivacy_api/services/jitsi/__init__.py | 4 +- .../services/mailserver/__init__.py | 4 +- .../services/nextcloud/__init__.py | 8 ++-- selfprivacy_api/services/ocserv/__init__.py | 4 +- selfprivacy_api/services/pleroma/__init__.py | 4 +- setup.py | 2 +- 11 files changed, 60 insertions(+), 44 deletions(-) diff --git a/selfprivacy_api/graphql/common_types/service.py b/selfprivacy_api/graphql/common_types/service.py index 8f27386..2a701da 100644 --- a/selfprivacy_api/graphql/common_types/service.py +++ b/selfprivacy_api/graphql/common_types/service.py @@ -1,20 +1,21 @@ from enum import Enum import typing import strawberry -import datetime +from strawberry.types import Info from selfprivacy_api.graphql.common_types.dns import DnsRecord from selfprivacy_api.graphql.common_types.backup_snapshot import SnapshotInfo from selfprivacy_api.services import get_service_by_id, get_services_by_location from selfprivacy_api.services import Service as ServiceInterface from selfprivacy_api.utils.block_devices import BlockDevices +from selfprivacy_api.utils.localization import Localization as L10n -def get_usages(root: "StorageVolume") -> list["StorageUsageInterface"]: +def get_usages(root: "StorageVolume", locale: str) -> list["StorageUsageInterface"]: """Get usages of a volume""" return [ ServiceStorageUsage( - service=service_to_graphql_service(service), + service=service_to_graphql_service(service, locale), title=service.get_display_name(), used_space=str(service.get_storage_usage()), volume=get_volume_by_id(service.get_drive()), @@ -37,9 +38,10 @@ class StorageVolume: type: str @strawberry.field - def usages(self) -> list["StorageUsageInterface"]: + def usages(self, info: Info) -> list["StorageUsageInterface"]: """Get usages of a volume""" - return get_usages(self) + locale = info.context["locale"] + return get_usages(self, locale) @strawberry.interface @@ -67,7 +69,7 @@ class ServiceStatusEnum(Enum): OFF = "OFF" -def get_storage_usage(root: "Service") -> ServiceStorageUsage: +def get_storage_usage(root: "Service", locale: str) -> ServiceStorageUsage: """Get storage usage for a service""" service = get_service_by_id(root.id) if service is None: @@ -78,7 +80,7 @@ def get_storage_usage(root: "Service") -> ServiceStorageUsage: volume=get_volume_by_id("sda1"), ) return ServiceStorageUsage( - service=service_to_graphql_service(service), + service=service_to_graphql_service(service, locale), title=service.get_display_name(), used_space=str(service.get_storage_usage()), volume=get_volume_by_id(service.get_drive()), @@ -99,21 +101,23 @@ class Service: dns_records: typing.Optional[typing.List[DnsRecord]] @strawberry.field - def storage_usage(self) -> ServiceStorageUsage: + def storage_usage(self, info: Info) -> ServiceStorageUsage: """Get storage usage for a service""" - return get_storage_usage(self) + locale = info.context["locale"] + return get_storage_usage(self, locale) @strawberry.field def backup_snapshots(self) -> typing.Optional[typing.List[SnapshotInfo]]: return None -def service_to_graphql_service(service: ServiceInterface) -> Service: +def service_to_graphql_service(service: ServiceInterface, locale: str) -> Service: """Convert service to graphql service""" + l10n = L10n() return Service( id=service.get_id(), - display_name=service.get_display_name(), - description=service.get_description(), + display_name=l10n.get(service.get_display_name(), locale), + description=l10n.get(service.get_description(), locale), svg_icon=service.get_svg_icon(), is_movable=service.is_movable(), is_required=service.is_required(), diff --git a/selfprivacy_api/graphql/mutations/services_mutations.py b/selfprivacy_api/graphql/mutations/services_mutations.py index 38a0d7f..771d680 100644 --- a/selfprivacy_api/graphql/mutations/services_mutations.py +++ b/selfprivacy_api/graphql/mutations/services_mutations.py @@ -1,7 +1,9 @@ """Services mutations""" # pylint: disable=too-few-public-methods +from threading import local import typing import strawberry +from strawberry.types import Info from selfprivacy_api.graphql import IsAuthenticated from selfprivacy_api.graphql.common_types.jobs import job_to_api_job @@ -45,8 +47,9 @@ class ServicesMutations: """Services mutations.""" @strawberry.mutation(permission_classes=[IsAuthenticated]) - def enable_service(self, service_id: str) -> ServiceMutationReturn: + def enable_service(self, service_id: str, info: Info) -> ServiceMutationReturn: """Enable service.""" + locale = info.context["locale"] service = get_service_by_id(service_id) if service is None: return ServiceMutationReturn( @@ -59,12 +62,13 @@ class ServicesMutations: success=True, message="Service enabled.", code=200, - service=service_to_graphql_service(service), + service=service_to_graphql_service(service, locale), ) @strawberry.mutation(permission_classes=[IsAuthenticated]) - def disable_service(self, service_id: str) -> ServiceMutationReturn: + def disable_service(self, service_id: str, info: Info) -> ServiceMutationReturn: """Disable service.""" + locale = info.context["locale"] service = get_service_by_id(service_id) if service is None: return ServiceMutationReturn( @@ -77,12 +81,13 @@ class ServicesMutations: success=True, message="Service disabled.", code=200, - service=service_to_graphql_service(service), + service=service_to_graphql_service(service, locale), ) @strawberry.mutation(permission_classes=[IsAuthenticated]) - def stop_service(self, service_id: str) -> ServiceMutationReturn: + def stop_service(self, service_id: str, info: Info) -> ServiceMutationReturn: """Stop service.""" + locale = info.context["locale"] service = get_service_by_id(service_id) if service is None: return ServiceMutationReturn( @@ -95,12 +100,13 @@ class ServicesMutations: success=True, message="Service stopped.", code=200, - service=service_to_graphql_service(service), + service=service_to_graphql_service(service, locale), ) @strawberry.mutation(permission_classes=[IsAuthenticated]) - def start_service(self, service_id: str) -> ServiceMutationReturn: + def start_service(self, service_id: str, info: Info) -> ServiceMutationReturn: """Start service.""" + locale = info.context["locale"] service = get_service_by_id(service_id) if service is None: return ServiceMutationReturn( @@ -113,12 +119,13 @@ class ServicesMutations: success=True, message="Service started.", code=200, - service=service_to_graphql_service(service), + service=service_to_graphql_service(service, locale), ) @strawberry.mutation(permission_classes=[IsAuthenticated]) - def restart_service(self, service_id: str) -> ServiceMutationReturn: + def restart_service(self, service_id: str, info: Info) -> ServiceMutationReturn: """Restart service.""" + locale = info.context["locale"] service = get_service_by_id(service_id) if service is None: return ServiceMutationReturn( @@ -131,12 +138,15 @@ class ServicesMutations: success=True, message="Service restarted.", code=200, - service=service_to_graphql_service(service), + service=service_to_graphql_service(service, locale), ) @strawberry.mutation(permission_classes=[IsAuthenticated]) - def move_service(self, input: MoveServiceInput) -> ServiceJobMutationReturn: + def move_service( + self, input: MoveServiceInput, info: Info + ) -> ServiceJobMutationReturn: """Move service.""" + locale = info.context["locale"] service = get_service_by_id(input.service_id) if service is None: return ServiceJobMutationReturn( @@ -149,7 +159,7 @@ class ServicesMutations: success=False, message="Service is not movable.", code=400, - service=service_to_graphql_service(service), + service=service_to_graphql_service(service, locale), ) volume = BlockDevices().get_block_device(input.location) if volume is None: @@ -157,13 +167,13 @@ class ServicesMutations: success=False, message="Volume not found.", code=404, - service=service_to_graphql_service(service), + service=service_to_graphql_service(service, locale), ) job = service.move_to_volume(volume) return ServiceJobMutationReturn( success=True, message="Service moved.", code=200, - service=service_to_graphql_service(service), + service=service_to_graphql_service(service, locale), job=job_to_api_job(job), ) diff --git a/selfprivacy_api/graphql/queries/services.py b/selfprivacy_api/graphql/queries/services.py index 17e0b98..5b686d0 100644 --- a/selfprivacy_api/graphql/queries/services.py +++ b/selfprivacy_api/graphql/queries/services.py @@ -2,6 +2,7 @@ # pylint: disable=too-few-public-methods import typing import strawberry +from strawberry.types import Info from selfprivacy_api.graphql.common_types.service import ( Service, @@ -13,6 +14,7 @@ from selfprivacy_api.services import get_all_services @strawberry.type class Services: @strawberry.field - def all_services(self, locale: str = "en") -> typing.List[Service]: + def all_services(self, info: Info) -> typing.List[Service]: + locale = info.context["locale"] services = get_all_services() return [service_to_graphql_service(service, locale) for service in services] diff --git a/selfprivacy_api/services/bitwarden/__init__.py b/selfprivacy_api/services/bitwarden/__init__.py index 32fce39..9bef927 100644 --- a/selfprivacy_api/services/bitwarden/__init__.py +++ b/selfprivacy_api/services/bitwarden/__init__.py @@ -27,12 +27,12 @@ class Bitwarden(Service): @staticmethod def get_display_name(locale: str = "en") -> str: """Return service display name.""" - return L10n().get("services.bitwarden.display_name", locale) + return "services.bitwarden.display_name" @staticmethod def get_description(locale: str = "en") -> str: """Return service description.""" - return L10n().get("services.bitwarden.description", locale) + return "services.bitwarden.description" @staticmethod def get_svg_icon() -> str: diff --git a/selfprivacy_api/services/gitea/__init__.py b/selfprivacy_api/services/gitea/__init__.py index 7c9a558..7f52b07 100644 --- a/selfprivacy_api/services/gitea/__init__.py +++ b/selfprivacy_api/services/gitea/__init__.py @@ -27,12 +27,12 @@ class Gitea(Service): @staticmethod def get_display_name(locale: str = "en") -> str: """Return service display name.""" - return L10n().get("services.gitea.display_name", locale) + return "services.gitea.display_name" @staticmethod def get_description(locale: str = "en") -> str: """Return service description.""" - return L10n().get("services.gitea.description", locale) + return "services.gitea.description" @staticmethod def get_svg_icon() -> str: diff --git a/selfprivacy_api/services/jitsi/__init__.py b/selfprivacy_api/services/jitsi/__init__.py index 4258272..e38268e 100644 --- a/selfprivacy_api/services/jitsi/__init__.py +++ b/selfprivacy_api/services/jitsi/__init__.py @@ -27,12 +27,12 @@ class Jitsi(Service): @staticmethod def get_display_name(locale: str = "en") -> str: """Return service display name.""" - return L10n().get("services.jitsi.display_name", locale) + return "services.jitsi.display_name" @staticmethod def get_description(locale: str = "en") -> str: """Return service description.""" - return L10n().get("services.jitsi.description", locale) + return "services.jitsi.description" @staticmethod def get_svg_icon() -> str: diff --git a/selfprivacy_api/services/mailserver/__init__.py b/selfprivacy_api/services/mailserver/__init__.py index 9555571..41c725c 100644 --- a/selfprivacy_api/services/mailserver/__init__.py +++ b/selfprivacy_api/services/mailserver/__init__.py @@ -29,11 +29,11 @@ class MailServer(Service): @staticmethod def get_display_name(locale: str = "en") -> str: - return L10n().get("services.mailserver.display_name", locale) + return "services.mailserver.display_name" @staticmethod def get_description(locale: str = "en") -> str: - return L10n().get("services.mailserver.description", locale) + return "services.mailserver.description" @staticmethod def get_svg_icon() -> str: diff --git a/selfprivacy_api/services/nextcloud/__init__.py b/selfprivacy_api/services/nextcloud/__init__.py index 9041651..704d1a5 100644 --- a/selfprivacy_api/services/nextcloud/__init__.py +++ b/selfprivacy_api/services/nextcloud/__init__.py @@ -23,14 +23,14 @@ class Nextcloud(Service): return "nextcloud" @staticmethod - def get_display_name(locale: str = "en") -> str: + def get_display_name() -> str: """Return service display name.""" - return L10n().get("services.nextcloud.display_name", locale) + return "services.nextcloud.display_name" @staticmethod - def get_description(locale: str = "en") -> str: + def get_description() -> str: """Return service description.""" - return L10n().get("services.nextcloud.description", locale) + return "services.nextcloud.description" @staticmethod def get_svg_icon() -> str: diff --git a/selfprivacy_api/services/ocserv/__init__.py b/selfprivacy_api/services/ocserv/__init__.py index 92a1a4a..de07119 100644 --- a/selfprivacy_api/services/ocserv/__init__.py +++ b/selfprivacy_api/services/ocserv/__init__.py @@ -22,11 +22,11 @@ class Ocserv(Service): @staticmethod def get_display_name(locale: str = "en") -> str: - return L10n().get("services.ocserv.display_name", locale) + return "services.ocserv.display_name" @staticmethod def get_description(locale: str = "en") -> str: - return L10n().get("services.ocserv.description", locale) + return "services.ocserv.description" @staticmethod def get_svg_icon() -> str: diff --git a/selfprivacy_api/services/pleroma/__init__.py b/selfprivacy_api/services/pleroma/__init__.py index 2aafd33..44ddb77 100644 --- a/selfprivacy_api/services/pleroma/__init__.py +++ b/selfprivacy_api/services/pleroma/__init__.py @@ -23,11 +23,11 @@ class Pleroma(Service): @staticmethod def get_display_name(locale: str = "en") -> str: - return L10n().get("services.pleroma.display_name", locale) + return "services.pleroma.display_name" @staticmethod def get_description(locale: str = "en") -> str: - return L10n().get("services.pleroma.description", locale) + return "services.pleroma.description" @staticmethod def get_svg_icon() -> str: diff --git a/setup.py b/setup.py index 1fde917..08df641 100755 --- a/setup.py +++ b/setup.py @@ -7,5 +7,5 @@ setup( scripts=[ "selfprivacy_api/app.py", ], - include_package_data=True + include_package_data=True, )