add enumconfigitem

This commit is contained in:
Inex Code 2024-07-03 17:35:40 +04:00
parent 89afd8b32d
commit 2262606759
4 changed files with 65 additions and 36 deletions

View file

@ -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 Service as ServiceInterface
from selfprivacy_api.services import ServiceDnsRecord 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.block_devices import BlockDevices
from selfprivacy_api.utils.network import get_ip4, get_ip6 from selfprivacy_api.utils.network import get_ip4, get_ip6
@ -122,6 +128,42 @@ class BoolConfigItem(ConfigItem):
value: bool 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 @strawberry.type
class Service: class Service:
id: str id: str
@ -162,25 +204,7 @@ class Service:
if not config_items: if not config_items:
return None return None
# By the "type" field convert every dict into a ConfigItem. In the future there will be more types. # By the "type" field convert every dict into a ConfigItem. In the future there will be more types.
return [ return [config_item_to_graphql(item) for item in config_items]
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
]
# TODO: fill this # TODO: fill this
@strawberry.field @strawberry.field

View file

@ -31,6 +31,7 @@ from selfprivacy_api.graphql.queries.system import System
from selfprivacy_api.graphql.common_types.service import ( from selfprivacy_api.graphql.common_types.service import (
StringConfigItem, StringConfigItem,
BoolConfigItem, BoolConfigItem,
EnumConfigItem,
) )
from selfprivacy_api.graphql.mutations.users_mutations import UsersMutations from selfprivacy_api.graphql.mutations.users_mutations import UsersMutations
@ -152,5 +153,9 @@ schema = strawberry.Schema(
query=Query, query=Query,
mutation=Mutation, mutation=Mutation,
subscription=Subscription, subscription=Subscription,
types=[StringConfigItem, BoolConfigItem], types=[
StringConfigItem,
BoolConfigItem,
EnumConfigItem,
],
) )

View file

@ -3,7 +3,7 @@ import re
from typing import Optional from typing import Optional
class ConfigItem(ABC): class ServiceConfigItem(ABC):
id: str id: str
description: str description: str
widget: str widget: str
@ -27,7 +27,7 @@ class ConfigItem(ABC):
} }
class StringConfigItem(ConfigItem): class StringServiceConfigItem(ServiceConfigItem):
def __init__( def __init__(
self, self,
id: str, id: str,
@ -52,7 +52,7 @@ class StringConfigItem(ConfigItem):
service_options[self.id] = value service_options[self.id] = value
class BoolConfigItem(ConfigItem): class BoolServiceConfigItem(ServiceConfigItem):
def __init__( def __init__(
self, self,
id: str, id: str,
@ -73,7 +73,7 @@ class BoolConfigItem(ConfigItem):
service_options[self.id] = value service_options[self.id] = value
class EnumConfigItem(ConfigItem): class EnumServiceConfigItem(ServiceConfigItem):
def __init__( def __init__(
self, self,
id: str, id: str,

View file

@ -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.service import Service, ServiceStatus
from selfprivacy_api.services.forgejo.icon import FORGEJO_ICON from selfprivacy_api.services.forgejo.icon import FORGEJO_ICON
from selfprivacy_api.services.config_item import ( from selfprivacy_api.services.config_item import (
StringConfigItem, StringServiceConfigItem,
BoolConfigItem, BoolServiceConfigItem,
EnumConfigItem, EnumServiceConfigItem,
ConfigItem, ServiceConfigItem,
) )
@ -22,40 +22,40 @@ class Forgejo(Service):
Previously was Gitea, so some IDs are still called gitea for compatibility. Previously was Gitea, so some IDs are still called gitea for compatibility.
""" """
config_items: dict[str, ConfigItem] = { config_items: dict[str, ServiceConfigItem] = {
"subdomain": StringConfigItem( "subdomain": StringServiceConfigItem(
id="subdomain", id="subdomain",
default_value="git", default_value="git",
description="Subdomain", description="Subdomain",
regex=r"[A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9]", regex=r"[A-Za-z0-9][A-Za-z0-9\-]{0,61}[A-Za-z0-9]",
widget="subdomain", widget="subdomain",
), ),
"appName": StringConfigItem( "appName": StringServiceConfigItem(
id="appName", id="appName",
default_value="SelfPrivacy git Service", default_value="SelfPrivacy git Service",
description="The name displayed in the web interface", description="The name displayed in the web interface",
), ),
"enableLfs": BoolConfigItem( "enableLfs": BoolServiceConfigItem(
id="enableLfs", id="enableLfs",
default_value=True, default_value=True,
description="Enable Git LFS", description="Enable Git LFS",
), ),
"forcePrivate": BoolConfigItem( "forcePrivate": BoolServiceConfigItem(
id="forcePrivate", id="forcePrivate",
default_value=False, default_value=False,
description="Force all new repositories to be private", description="Force all new repositories to be private",
), ),
"disableRegistration": BoolConfigItem( "disableRegistration": BoolServiceConfigItem(
id="disableRegistration", id="disableRegistration",
default_value=False, default_value=False,
description="Disable registration of new users", description="Disable registration of new users",
), ),
"requireSigninView": BoolConfigItem( "requireSigninView": BoolServiceConfigItem(
id="requireSigninView", id="requireSigninView",
default_value=False, default_value=False,
description="Require signin to view any page", description="Require signin to view any page",
), ),
"defaultTheme": EnumConfigItem( "defaultTheme": EnumServiceConfigItem(
id="defaultTheme", id="defaultTheme",
default_value="forgejo-auto", default_value="forgejo-auto",
description="Default theme", description="Default theme",