mirror of
https://git.selfprivacy.org/SelfPrivacy/selfprivacy-rest-api.git
synced 2024-11-23 20:41:27 +00:00
refactor(i10l): Move services string localization to API layer
This commit is contained in:
parent
d9dab29fe8
commit
47d1a0f4a4
|
@ -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(),
|
||||
|
|
|
@ -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),
|
||||
)
|
||||
|
|
|
@ -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]
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
|
@ -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:
|
||||
|
|
Loading…
Reference in a new issue