Skip to content

Commit

Permalink
Validate settings before save
Browse files Browse the repository at this point in the history
No-Issue
  • Loading branch information
rochacbruno committed Aug 17, 2023
1 parent 441f981 commit c40875f
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 41 deletions.
84 changes: 44 additions & 40 deletions galaxy_ng/app/dynamic_settings.py
Original file line number Diff line number Diff line change
@@ -1,54 +1,57 @@
"""
type:
default:
description:
choices:
key_hints:
value_hints:
"""
from dynaconf import Validator

DYNAMIC_SETTINGS_SCHEMA = {
"FOO": { # for testing
"type": str,
"default": "bar",
"description": "Foo bar",
"value_hints": ["bar", "baz"],
},
"GALAXY_REQUIRE_CONTENT_APPROVAL": {
"type": bool,
"default": False,
"description": "Require content approval before it can be published",
"choices": [True, False],
"validator": Validator(is_type_of=bool),
"schema": {
"type": "boolean",
"enum": ["true", "false"],
"default": False,
"description": "Require content approval before it can be published",
}
},
"GALAXY_REQUIRE_SIGNATURE_FOR_APPROVAL": {
"type": bool,
"default": False,
"description": "Require signature for content approval",
"choices": [True, False],
"validator": Validator(is_type_of=bool),
"schema": {
"type": "boolean",
"enum": ["true", "false"],
"default": "false",
"description": "Require signature for content approval",
}
},
"GALAXY_SIGNATURE_UPLOAD_ENABLED": {
"type": bool,
"default": False,
"description": "Enable signature upload",
"choices": [True, False],
"validator": Validator(is_type_of=bool),
"schema": {
"type": "boolean",
"enum": ["true", "false"],
"default": "false",
"description": "Enable signature upload",
}
},
"GALAXY_AUTO_SIGN_COLLECTIONS": {
"type": bool,
"default": False,
"description": "Automatically sign collections during approval/upload",
"choices": [True, False],
"validator": Validator(is_type_of=bool),
"schema": {
"type": "boolean",
"enum": ["true", "false"],
"default": "false",
"description": "Automatically sign collections during approval/upload",
}
},
"GALAXY_FEATURE_FLAGS": {
"type": dict,
"default": {},
"description": "Feature flags for galaxy_ng",
"key_hints": {
"execution_environments": {
"type": bool,
"default": False,
"description": "Enable execution environments",
"choices": [True, False],
"validator": Validator(is_type_of=dict),
"schema": {
"type": "object",
"properties": {
"execution_environments": {
"type": "boolean",
"enum": ["true", "false"],
"default": "false",
"description": "Enable execution environments",
},
},
}
"default": {},
"description": "Feature flags for galaxy_ng",
},
},
# For 1816 PR
"INSIGHTS_TRACKING_STATE": {},
Expand All @@ -57,4 +60,5 @@
"REDHAT_PASSWORD": {},
"AUTOMATION_ANALYTICS_LAST_GATHERED": {},
"AUTOMATION_ANALYTICS_LAST_ENTRIES": {},
"FOO": {}
}
10 changes: 9 additions & 1 deletion galaxy_ng/app/models/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,15 @@ def _hook_validate(self):
raise Exception(f"Setting {self.key} not allowed by schema")

logger.debug("validate %s via settings validators", self.key)
# TODO: Create a settings copy, build a Validation from schema, validate it.
validator = DYNAMIC_SETTINGS_SCHEMA[self.base_key].get("validator")
if validator:
validator.names = [self.base_key]
temp_settings = settings.dynaconf_clone()
temp_settings.validators.register(validator)
temp_settings.update(self.as_dict(), tomlfy=True)
temp_settings.set(self.base_key, self.value, tomlfy=True)
temp_settings.validators.validate()


@property
def base_key(self):
Expand Down

0 comments on commit c40875f

Please sign in to comment.