refactor(i10l): Move services string localization to API layer

This commit is contained in:
Inex Code 2023-06-01 22:42:27 +03:00
parent d9dab29fe8
commit 47d1a0f4a4
11 changed files with 60 additions and 44 deletions

View file

@ -1,20 +1,21 @@
from enum import Enum from enum import Enum
import typing import typing
import strawberry import strawberry
import datetime from strawberry.types import Info
from selfprivacy_api.graphql.common_types.dns import DnsRecord from selfprivacy_api.graphql.common_types.dns import DnsRecord
from selfprivacy_api.graphql.common_types.backup_snapshot import SnapshotInfo 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 get_service_by_id, get_services_by_location
from selfprivacy_api.services import Service as ServiceInterface from selfprivacy_api.services import Service as ServiceInterface
from selfprivacy_api.utils.block_devices import BlockDevices 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""" """Get usages of a volume"""
return [ return [
ServiceStorageUsage( ServiceStorageUsage(
service=service_to_graphql_service(service), service=service_to_graphql_service(service, locale),
title=service.get_display_name(), title=service.get_display_name(),
used_space=str(service.get_storage_usage()), used_space=str(service.get_storage_usage()),
volume=get_volume_by_id(service.get_drive()), volume=get_volume_by_id(service.get_drive()),
@ -37,9 +38,10 @@ class StorageVolume:
type: str type: str
@strawberry.field @strawberry.field
def usages(self) -> list["StorageUsageInterface"]: def usages(self, info: Info) -> list["StorageUsageInterface"]:
"""Get usages of a volume""" """Get usages of a volume"""
return get_usages(self) locale = info.context["locale"]
return get_usages(self, locale)
@strawberry.interface @strawberry.interface
@ -67,7 +69,7 @@ class ServiceStatusEnum(Enum):
OFF = "OFF" OFF = "OFF"
def get_storage_usage(root: "Service") -> ServiceStorageUsage: def get_storage_usage(root: "Service", locale: str) -> ServiceStorageUsage:
"""Get storage usage for a service""" """Get storage usage for a service"""
service = get_service_by_id(root.id) service = get_service_by_id(root.id)
if service is None: if service is None:
@ -78,7 +80,7 @@ def get_storage_usage(root: "Service") -> ServiceStorageUsage:
volume=get_volume_by_id("sda1"), volume=get_volume_by_id("sda1"),
) )
return ServiceStorageUsage( return ServiceStorageUsage(
service=service_to_graphql_service(service), service=service_to_graphql_service(service, locale),
title=service.get_display_name(), title=service.get_display_name(),
used_space=str(service.get_storage_usage()), used_space=str(service.get_storage_usage()),
volume=get_volume_by_id(service.get_drive()), volume=get_volume_by_id(service.get_drive()),
@ -99,21 +101,23 @@ class Service:
dns_records: typing.Optional[typing.List[DnsRecord]] dns_records: typing.Optional[typing.List[DnsRecord]]
@strawberry.field @strawberry.field
def storage_usage(self) -> ServiceStorageUsage: def storage_usage(self, info: Info) -> ServiceStorageUsage:
"""Get storage usage for a service""" """Get storage usage for a service"""
return get_storage_usage(self) locale = info.context["locale"]
return get_storage_usage(self, locale)
@strawberry.field @strawberry.field
def backup_snapshots(self) -> typing.Optional[typing.List[SnapshotInfo]]: def backup_snapshots(self) -> typing.Optional[typing.List[SnapshotInfo]]:
return None 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""" """Convert service to graphql service"""
l10n = L10n()
return Service( return Service(
id=service.get_id(), id=service.get_id(),
display_name=service.get_display_name(), display_name=l10n.get(service.get_display_name(), locale),
description=service.get_description(), description=l10n.get(service.get_description(), locale),
svg_icon=service.get_svg_icon(), svg_icon=service.get_svg_icon(),
is_movable=service.is_movable(), is_movable=service.is_movable(),
is_required=service.is_required(), is_required=service.is_required(),

View file

@ -1,7 +1,9 @@
"""Services mutations""" """Services mutations"""
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
from threading import local
import typing import typing
import strawberry import strawberry
from strawberry.types import Info
from selfprivacy_api.graphql import IsAuthenticated from selfprivacy_api.graphql import IsAuthenticated
from selfprivacy_api.graphql.common_types.jobs import job_to_api_job from selfprivacy_api.graphql.common_types.jobs import job_to_api_job
@ -45,8 +47,9 @@ class ServicesMutations:
"""Services mutations.""" """Services mutations."""
@strawberry.mutation(permission_classes=[IsAuthenticated]) @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.""" """Enable service."""
locale = info.context["locale"]
service = get_service_by_id(service_id) service = get_service_by_id(service_id)
if service is None: if service is None:
return ServiceMutationReturn( return ServiceMutationReturn(
@ -59,12 +62,13 @@ class ServicesMutations:
success=True, success=True,
message="Service enabled.", message="Service enabled.",
code=200, code=200,
service=service_to_graphql_service(service), service=service_to_graphql_service(service, locale),
) )
@strawberry.mutation(permission_classes=[IsAuthenticated]) @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.""" """Disable service."""
locale = info.context["locale"]
service = get_service_by_id(service_id) service = get_service_by_id(service_id)
if service is None: if service is None:
return ServiceMutationReturn( return ServiceMutationReturn(
@ -77,12 +81,13 @@ class ServicesMutations:
success=True, success=True,
message="Service disabled.", message="Service disabled.",
code=200, code=200,
service=service_to_graphql_service(service), service=service_to_graphql_service(service, locale),
) )
@strawberry.mutation(permission_classes=[IsAuthenticated]) @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.""" """Stop service."""
locale = info.context["locale"]
service = get_service_by_id(service_id) service = get_service_by_id(service_id)
if service is None: if service is None:
return ServiceMutationReturn( return ServiceMutationReturn(
@ -95,12 +100,13 @@ class ServicesMutations:
success=True, success=True,
message="Service stopped.", message="Service stopped.",
code=200, code=200,
service=service_to_graphql_service(service), service=service_to_graphql_service(service, locale),
) )
@strawberry.mutation(permission_classes=[IsAuthenticated]) @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.""" """Start service."""
locale = info.context["locale"]
service = get_service_by_id(service_id) service = get_service_by_id(service_id)
if service is None: if service is None:
return ServiceMutationReturn( return ServiceMutationReturn(
@ -113,12 +119,13 @@ class ServicesMutations:
success=True, success=True,
message="Service started.", message="Service started.",
code=200, code=200,
service=service_to_graphql_service(service), service=service_to_graphql_service(service, locale),
) )
@strawberry.mutation(permission_classes=[IsAuthenticated]) @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.""" """Restart service."""
locale = info.context["locale"]
service = get_service_by_id(service_id) service = get_service_by_id(service_id)
if service is None: if service is None:
return ServiceMutationReturn( return ServiceMutationReturn(
@ -131,12 +138,15 @@ class ServicesMutations:
success=True, success=True,
message="Service restarted.", message="Service restarted.",
code=200, code=200,
service=service_to_graphql_service(service), service=service_to_graphql_service(service, locale),
) )
@strawberry.mutation(permission_classes=[IsAuthenticated]) @strawberry.mutation(permission_classes=[IsAuthenticated])
def move_service(self, input: MoveServiceInput) -> ServiceJobMutationReturn: def move_service(
self, input: MoveServiceInput, info: Info
) -> ServiceJobMutationReturn:
"""Move service.""" """Move service."""
locale = info.context["locale"]
service = get_service_by_id(input.service_id) service = get_service_by_id(input.service_id)
if service is None: if service is None:
return ServiceJobMutationReturn( return ServiceJobMutationReturn(
@ -149,7 +159,7 @@ class ServicesMutations:
success=False, success=False,
message="Service is not movable.", message="Service is not movable.",
code=400, code=400,
service=service_to_graphql_service(service), service=service_to_graphql_service(service, locale),
) )
volume = BlockDevices().get_block_device(input.location) volume = BlockDevices().get_block_device(input.location)
if volume is None: if volume is None:
@ -157,13 +167,13 @@ class ServicesMutations:
success=False, success=False,
message="Volume not found.", message="Volume not found.",
code=404, code=404,
service=service_to_graphql_service(service), service=service_to_graphql_service(service, locale),
) )
job = service.move_to_volume(volume) job = service.move_to_volume(volume)
return ServiceJobMutationReturn( return ServiceJobMutationReturn(
success=True, success=True,
message="Service moved.", message="Service moved.",
code=200, code=200,
service=service_to_graphql_service(service), service=service_to_graphql_service(service, locale),
job=job_to_api_job(job), job=job_to_api_job(job),
) )

View file

@ -2,6 +2,7 @@
# pylint: disable=too-few-public-methods # pylint: disable=too-few-public-methods
import typing import typing
import strawberry import strawberry
from strawberry.types import Info
from selfprivacy_api.graphql.common_types.service import ( from selfprivacy_api.graphql.common_types.service import (
Service, Service,
@ -13,6 +14,7 @@ from selfprivacy_api.services import get_all_services
@strawberry.type @strawberry.type
class Services: class Services:
@strawberry.field @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() services = get_all_services()
return [service_to_graphql_service(service, locale) for service in services] return [service_to_graphql_service(service, locale) for service in services]

View file

@ -27,12 +27,12 @@ class Bitwarden(Service):
@staticmethod @staticmethod
def get_display_name(locale: str = "en") -> str: def get_display_name(locale: str = "en") -> str:
"""Return service display name.""" """Return service display name."""
return L10n().get("services.bitwarden.display_name", locale) return "services.bitwarden.display_name"
@staticmethod @staticmethod
def get_description(locale: str = "en") -> str: def get_description(locale: str = "en") -> str:
"""Return service description.""" """Return service description."""
return L10n().get("services.bitwarden.description", locale) return "services.bitwarden.description"
@staticmethod @staticmethod
def get_svg_icon() -> str: def get_svg_icon() -> str:

View file

@ -27,12 +27,12 @@ class Gitea(Service):
@staticmethod @staticmethod
def get_display_name(locale: str = "en") -> str: def get_display_name(locale: str = "en") -> str:
"""Return service display name.""" """Return service display name."""
return L10n().get("services.gitea.display_name", locale) return "services.gitea.display_name"
@staticmethod @staticmethod
def get_description(locale: str = "en") -> str: def get_description(locale: str = "en") -> str:
"""Return service description.""" """Return service description."""
return L10n().get("services.gitea.description", locale) return "services.gitea.description"
@staticmethod @staticmethod
def get_svg_icon() -> str: def get_svg_icon() -> str:

View file

@ -27,12 +27,12 @@ class Jitsi(Service):
@staticmethod @staticmethod
def get_display_name(locale: str = "en") -> str: def get_display_name(locale: str = "en") -> str:
"""Return service display name.""" """Return service display name."""
return L10n().get("services.jitsi.display_name", locale) return "services.jitsi.display_name"
@staticmethod @staticmethod
def get_description(locale: str = "en") -> str: def get_description(locale: str = "en") -> str:
"""Return service description.""" """Return service description."""
return L10n().get("services.jitsi.description", locale) return "services.jitsi.description"
@staticmethod @staticmethod
def get_svg_icon() -> str: def get_svg_icon() -> str:

View file

@ -29,11 +29,11 @@ class MailServer(Service):
@staticmethod @staticmethod
def get_display_name(locale: str = "en") -> str: def get_display_name(locale: str = "en") -> str:
return L10n().get("services.mailserver.display_name", locale) return "services.mailserver.display_name"
@staticmethod @staticmethod
def get_description(locale: str = "en") -> str: def get_description(locale: str = "en") -> str:
return L10n().get("services.mailserver.description", locale) return "services.mailserver.description"
@staticmethod @staticmethod
def get_svg_icon() -> str: def get_svg_icon() -> str:

View file

@ -23,14 +23,14 @@ class Nextcloud(Service):
return "nextcloud" return "nextcloud"
@staticmethod @staticmethod
def get_display_name(locale: str = "en") -> str: def get_display_name() -> str:
"""Return service display name.""" """Return service display name."""
return L10n().get("services.nextcloud.display_name", locale) return "services.nextcloud.display_name"
@staticmethod @staticmethod
def get_description(locale: str = "en") -> str: def get_description() -> str:
"""Return service description.""" """Return service description."""
return L10n().get("services.nextcloud.description", locale) return "services.nextcloud.description"
@staticmethod @staticmethod
def get_svg_icon() -> str: def get_svg_icon() -> str:

View file

@ -22,11 +22,11 @@ class Ocserv(Service):
@staticmethod @staticmethod
def get_display_name(locale: str = "en") -> str: def get_display_name(locale: str = "en") -> str:
return L10n().get("services.ocserv.display_name", locale) return "services.ocserv.display_name"
@staticmethod @staticmethod
def get_description(locale: str = "en") -> str: def get_description(locale: str = "en") -> str:
return L10n().get("services.ocserv.description", locale) return "services.ocserv.description"
@staticmethod @staticmethod
def get_svg_icon() -> str: def get_svg_icon() -> str:

View file

@ -23,11 +23,11 @@ class Pleroma(Service):
@staticmethod @staticmethod
def get_display_name(locale: str = "en") -> str: def get_display_name(locale: str = "en") -> str:
return L10n().get("services.pleroma.display_name", locale) return "services.pleroma.display_name"
@staticmethod @staticmethod
def get_description(locale: str = "en") -> str: def get_description(locale: str = "en") -> str:
return L10n().get("services.pleroma.description", locale) return "services.pleroma.description"
@staticmethod @staticmethod
def get_svg_icon() -> str: def get_svg_icon() -> str:

View file

@ -7,5 +7,5 @@ setup(
scripts=[ scripts=[
"selfprivacy_api/app.py", "selfprivacy_api/app.py",
], ],
include_package_data=True include_package_data=True,
) )