From 89afd8b32dd0963089915d4d72338fcd02663909 Mon Sep 17 00:00:00 2001 From: Inex Code Date: Wed, 3 Jul 2024 14:26:26 +0400 Subject: [PATCH] feat: Add EnumConfigItem for service configuration options --- selfprivacy_api/graphql/schema.py | 7 ++++-- selfprivacy_api/services/config_item.py | 25 ++++++++++++++++++++ selfprivacy_api/services/forgejo/__init__.py | 14 +++++++++++ 3 files changed, 44 insertions(+), 2 deletions(-) diff --git a/selfprivacy_api/graphql/schema.py b/selfprivacy_api/graphql/schema.py index 1075f8f..b689e48 100644 --- a/selfprivacy_api/graphql/schema.py +++ b/selfprivacy_api/graphql/schema.py @@ -28,7 +28,10 @@ from selfprivacy_api.graphql.queries.services import Services from selfprivacy_api.graphql.queries.storage import Storage from selfprivacy_api.graphql.queries.system import System -from selfprivacy_api.graphql.common_types.service import StringConfigItem, BoolConfigItem +from selfprivacy_api.graphql.common_types.service import ( + StringConfigItem, + BoolConfigItem, +) from selfprivacy_api.graphql.mutations.users_mutations import UsersMutations from selfprivacy_api.graphql.queries.users import Users @@ -149,5 +152,5 @@ schema = strawberry.Schema( query=Query, mutation=Mutation, subscription=Subscription, - types=[StringConfigItem, BoolConfigItem] + types=[StringConfigItem, BoolConfigItem], ) diff --git a/selfprivacy_api/services/config_item.py b/selfprivacy_api/services/config_item.py index 3f10ca5..b64811b 100644 --- a/selfprivacy_api/services/config_item.py +++ b/selfprivacy_api/services/config_item.py @@ -71,3 +71,28 @@ class BoolConfigItem(ConfigItem): def set_value(self, value, service_options): service_options[self.id] = value + + +class EnumConfigItem(ConfigItem): + def __init__( + self, + id: str, + default_value: str, + description: str, + options: list[str], + widget: Optional[str] = None, + ): + self.id = id + self.type = "enum" + self.default_value = default_value + self.description = description + self.options = options + self.widget = widget if widget else "select" + + def get_value(self, service_options): + return service_options.get(self.id, self.default_value) + + def set_value(self, value, service_options): + if value not in self.options: + raise ValueError(f"Value {value} not in options {self.options}") + service_options[self.id] = value diff --git a/selfprivacy_api/services/forgejo/__init__.py b/selfprivacy_api/services/forgejo/__init__.py index 7a0f07a..79f1d3e 100644 --- a/selfprivacy_api/services/forgejo/__init__.py +++ b/selfprivacy_api/services/forgejo/__init__.py @@ -11,6 +11,7 @@ from selfprivacy_api.services.forgejo.icon import FORGEJO_ICON from selfprivacy_api.services.config_item import ( StringConfigItem, BoolConfigItem, + EnumConfigItem, ConfigItem, ) @@ -54,6 +55,19 @@ class Forgejo(Service): default_value=False, description="Require signin to view any page", ), + "defaultTheme": EnumConfigItem( + id="defaultTheme", + default_value="forgejo-auto", + description="Default theme", + options=[ + "forgejo-auto", + "forgejo-light", + "forgejo-dark", + "auto", + "gitea", + "arc-green", + ], + ), } @staticmethod