From 22626067594d402184bc9831450809334d0d98ef Mon Sep 17 00:00:00 2001 From: Inex Code Date: Wed, 3 Jul 2024 17:35:40 +0400 Subject: [PATCH] add enumconfigitem --- .../graphql/common_types/service.py | 62 +++++++++++++------ selfprivacy_api/graphql/schema.py | 7 ++- selfprivacy_api/services/config_item.py | 8 +-- selfprivacy_api/services/forgejo/__init__.py | 24 +++---- 4 files changed, 65 insertions(+), 36 deletions(-) diff --git a/selfprivacy_api/graphql/common_types/service.py b/selfprivacy_api/graphql/common_types/service.py index 3284958..6411ebf 100644 --- a/selfprivacy_api/graphql/common_types/service.py +++ b/selfprivacy_api/graphql/common_types/service.py @@ -10,6 +10,12 @@ 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 ServiceDnsRecord +from selfprivacy_api.services.config_item import ( + ServiceConfigItem, + StringServiceConfigItem, + BoolServiceConfigItem, + EnumServiceConfigItem, +) from selfprivacy_api.utils.block_devices import BlockDevices from selfprivacy_api.utils.network import get_ip4, get_ip6 @@ -122,6 +128,42 @@ class BoolConfigItem(ConfigItem): value: bool +@strawberry.type +class EnumConfigItem(ConfigItem): + value: str + options: list[str] + + +def config_item_to_graphql(item: ServiceConfigItem) -> ConfigItem: + if isinstance(item, StringServiceConfigItem): + return StringConfigItem( + id=item.id, + description=item.description, + widget=item.widget, + type=item.type, + value=item.default_value, + regex=item.regex.pattern if item.regex else None, + ) + if isinstance(item, BoolServiceConfigItem): + return BoolConfigItem( + id=item.id, + description=item.description, + widget=item.widget, + type=item.type, + value=item.default_value, + ) + if isinstance(item, EnumServiceConfigItem): + return EnumConfigItem( + id=item.id, + description=item.description, + widget=item.widget, + type=item.type, + value=item.default_value, + options=item.options, + ) + raise ValueError(f"Unknown config item type {item}") + + @strawberry.type class Service: id: str @@ -162,25 +204,7 @@ class Service: if not config_items: return None # By the "type" field convert every dict into a ConfigItem. In the future there will be more types. - return [ - StringConfigItem( - id=config_items[item]["id"], - description=config_items[item]["description"], - widget=config_items[item]["widget"], - type=config_items[item]["type"], - value=config_items[item]["value"], - regex=config_items[item].get("regex"), - ) - if config_items[item]["type"] == "string" - else BoolConfigItem( - id=config_items[item]["id"], - description=config_items[item]["description"], - widget=config_items[item]["widget"], - type=config_items[item]["type"], - value=config_items[item]["value"], - ) - for item in config_items - ] + return [config_item_to_graphql(item) for item in config_items] # TODO: fill this @strawberry.field diff --git a/selfprivacy_api/graphql/schema.py b/selfprivacy_api/graphql/schema.py index b689e48..8b0e3ca 100644 --- a/selfprivacy_api/graphql/schema.py +++ b/selfprivacy_api/graphql/schema.py @@ -31,6 +31,7 @@ from selfprivacy_api.graphql.queries.system import System from selfprivacy_api.graphql.common_types.service import ( StringConfigItem, BoolConfigItem, + EnumConfigItem, ) from selfprivacy_api.graphql.mutations.users_mutations import UsersMutations @@ -152,5 +153,9 @@ schema = strawberry.Schema( query=Query, mutation=Mutation, subscription=Subscription, - types=[StringConfigItem, BoolConfigItem], + types=[ + StringConfigItem, + BoolConfigItem, + EnumConfigItem, + ], ) diff --git a/selfprivacy_api/services/config_item.py b/selfprivacy_api/services/config_item.py index b64811b..9b1286d 100644 --- a/selfprivacy_api/services/config_item.py +++ b/selfprivacy_api/services/config_item.py @@ -3,7 +3,7 @@ import re from typing import Optional -class ConfigItem(ABC): +class ServiceConfigItem(ABC): id: str description: str widget: str @@ -27,7 +27,7 @@ class ConfigItem(ABC): } -class StringConfigItem(ConfigItem): +class StringServiceConfigItem(ServiceConfigItem): def __init__( self, id: str, @@ -52,7 +52,7 @@ class StringConfigItem(ConfigItem): service_options[self.id] = value -class BoolConfigItem(ConfigItem): +class BoolServiceConfigItem(ServiceConfigItem): def __init__( self, id: str, @@ -73,7 +73,7 @@ class BoolConfigItem(ConfigItem): service_options[self.id] = value -class EnumConfigItem(ConfigItem): +class EnumServiceConfigItem(ServiceConfigItem): def __init__( self, id: str, diff --git a/selfprivacy_api/services/forgejo/__init__.py b/selfprivacy_api/services/forgejo/__init__.py index 79f1d3e..fb43d0b 100644 --- a/selfprivacy_api/services/forgejo/__init__.py +++ b/selfprivacy_api/services/forgejo/__init__.py @@ -9,10 +9,10 @@ from selfprivacy_api.utils.systemd import get_service_status from selfprivacy_api.services.service import Service, ServiceStatus from selfprivacy_api.services.forgejo.icon import FORGEJO_ICON from selfprivacy_api.services.config_item import ( - StringConfigItem, - BoolConfigItem, - EnumConfigItem, - ConfigItem, + StringServiceConfigItem, + BoolServiceConfigItem, + EnumServiceConfigItem, + ServiceConfigItem, ) @@ -22,40 +22,40 @@ class Forgejo(Service): Previously was Gitea, so some IDs are still called gitea for compatibility. """ - config_items: dict[str, ConfigItem] = { - "subdomain": StringConfigItem( + config_items: dict[str, ServiceConfigItem] = { + "subdomain": StringServiceConfigItem( id="subdomain", default_value="git", description="Subdomain", regex=r"[A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9]", widget="subdomain", ), - "appName": StringConfigItem( + "appName": StringServiceConfigItem( id="appName", default_value="SelfPrivacy git Service", description="The name displayed in the web interface", ), - "enableLfs": BoolConfigItem( + "enableLfs": BoolServiceConfigItem( id="enableLfs", default_value=True, description="Enable Git LFS", ), - "forcePrivate": BoolConfigItem( + "forcePrivate": BoolServiceConfigItem( id="forcePrivate", default_value=False, description="Force all new repositories to be private", ), - "disableRegistration": BoolConfigItem( + "disableRegistration": BoolServiceConfigItem( id="disableRegistration", default_value=False, description="Disable registration of new users", ), - "requireSigninView": BoolConfigItem( + "requireSigninView": BoolServiceConfigItem( id="requireSigninView", default_value=False, description="Require signin to view any page", ), - "defaultTheme": EnumConfigItem( + "defaultTheme": EnumServiceConfigItem( id="defaultTheme", default_value="forgejo-auto", description="Default theme",