From 2d6c5dbd6fcccb1636b04ae36596af3e7cb6fe5d Mon Sep 17 00:00:00 2001 From: Inex Code Date: Mon, 15 Jul 2024 20:00:31 +0400 Subject: [PATCH] fix --- .../graphql/common_types/service.py | 12 ++++-- selfprivacy_api/services/config_item.py | 42 +++++++++++++++++++ 2 files changed, 51 insertions(+), 3 deletions(-) diff --git a/selfprivacy_api/graphql/common_types/service.py b/selfprivacy_api/graphql/common_types/service.py index 9223b9a..680c2a7 100644 --- a/selfprivacy_api/graphql/common_types/service.py +++ b/selfprivacy_api/graphql/common_types/service.py @@ -120,17 +120,20 @@ class ConfigItem: @strawberry.type class StringConfigItem(ConfigItem): value: str + default_value: str regex: Optional[str] @strawberry.type class BoolConfigItem(ConfigItem): value: bool + default_value: bool @strawberry.type class EnumConfigItem(ConfigItem): value: str + default_value: str options: list[str] @@ -142,7 +145,8 @@ def config_item_to_graphql(item: dict) -> ConfigItem: description=item["description"], widget=item["widget"], type=item_type, - value=item["default_value"], + value=item["value"], + default_value=item["default_value"], regex=item.get("regex") ) elif item_type == "bool": @@ -151,7 +155,8 @@ def config_item_to_graphql(item: dict) -> ConfigItem: description=item["description"], widget=item["widget"], type=item_type, - value=item["default_value"], + value=item["value"], + default_value=item["default_value"], ) elif item_type == "enum": return EnumConfigItem( @@ -159,7 +164,8 @@ def config_item_to_graphql(item: dict) -> ConfigItem: description=item["description"], widget=item["widget"], type=item_type, - value=item["default_value"], + value=item["value"], + default_value=item["default_value"], options=item["options"], ) else: diff --git a/selfprivacy_api/services/config_item.py b/selfprivacy_api/services/config_item.py index a57b7b3..90f0ed7 100644 --- a/selfprivacy_api/services/config_item.py +++ b/selfprivacy_api/services/config_item.py @@ -128,3 +128,45 @@ class EnumServiceConfigItem(ServiceConfigItem): "default_value": self.default_value, "options": self.options, } + + +# TODO: unused for now +class IntServiceConfigItem(ServiceConfigItem): + def __init__( + self, + id: str, + default_value: int, + description: str, + widget: Optional[str] = None, + min_value: Optional[int] = None, + max_value: Optional[int] = None, + ) -> None: + self.id = id + self.type = "int" + self.default_value = default_value + self.description = description + self.widget = widget if widget else "number" + self.min_value = min_value + self.max_value = max_value + + def get_value(self, service_options): + return service_options.get(self.id, self.default_value) + + def set_value(self, value, service_options): + if self.min_value is not None and value < self.min_value: + raise ValueError(f"Value {value} is less than min_value {self.min_value}") + if self.max_value is not None and value > self.max_value: + raise ValueError(f"Value {value} is greater than max_value {self.max_value}") + service_options[self.id] = value + + def as_dict(self, service_options): + return { + "id": self.id, + "type": self.type, + "description": self.description, + "widget": self.widget, + "value": self.get_value(service_options), + "default_value": self.default_value, + "min_value": self.min_value, + "max_value": self.max_value, + }