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 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

View file

@ -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,
],
)

View file

@ -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,

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.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",