From 2f02947bf6bfac72ddd7a7564eb01c11a6a7a76a Mon Sep 17 00:00:00 2001 From: Mikko Nieminen Date: Thu, 9 Jan 2025 12:31:43 +0100 Subject: [PATCH] add app setting user_modifiable validation (#1536) --- CHANGELOG.rst | 1 + projectroles/plugins.py | 23 +++++++++++++++++++- projectroles/tests/test_plugins.py | 34 ++++++++++++++++++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 2cb2a18f..38a2b7bd 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -16,6 +16,7 @@ Added - App setting type constants (#1458) - ``PluginAppSettingDef`` class for app setting definitions (#1456) - Django check for unique app setting names within each plugin (#1456) + - App setting ``user_modifiable`` validation (#1536) Changed ------- diff --git a/projectroles/plugins.py b/projectroles/plugins.py index 3093e3fd..82785ae9 100644 --- a/projectroles/plugins.py +++ b/projectroles/plugins.py @@ -671,7 +671,7 @@ def __init__( placeholder=None, description=None, options=None, - user_modifiable=True, + user_modifiable=None, # Set None here to validate PROJECT_USER value global_edit=False, project_types=None, widget_attrs=None, @@ -714,6 +714,12 @@ def __init__( and not callable(options) ): self.validate_default_in_options(default, options) + # Validate and set user_modifiable + self.validate_user_modifiable(scope, user_modifiable) + if user_modifiable is None: + user_modifiable = ( + False if scope == APP_SETTING_SCOPE_PROJECT_USER else True + ) # Set members self.name = name self.scope = scope @@ -830,6 +836,21 @@ def validate_value(cls, setting_type, setting_value): 'Please enter valid JSON ({})'.format(setting_value) ) + @classmethod + def validate_user_modifiable(cls, scope, user_modifiable): + """ + Validate user_modifiable against scope. + + :param scope: String + :param user_modifiable: Bool or None + :raises: ValueError if user_modifiable can't be set for scope + """ + if user_modifiable and scope == APP_SETTING_SCOPE_PROJECT_USER: + raise ValueError( + 'Argument user_modifiable can not be set True for PROJECT_USER ' + 'scope settings' + ) + class PluginObjectLink: """ diff --git a/projectroles/tests/test_plugins.py b/projectroles/tests/test_plugins.py index 81f4b6b4..c2ba3fe5 100644 --- a/projectroles/tests/test_plugins.py +++ b/projectroles/tests/test_plugins.py @@ -249,3 +249,37 @@ def test_init_default_not_in_options(self): default=0, options=[1, 2], ) + + def test_init_project_user_modifiable_false(self): + """Test init with PROJECT_USER scope and user_modifiable=False""" + s_def = PluginAppSettingDef( + name=DEF_NAME, + scope=APP_SETTING_SCOPE_PROJECT_USER, + type=APP_SETTING_TYPE_BOOLEAN, + default=True, + user_modifiable=False, + ) + self.assertIsNotNone(s_def) + self.assertEqual(s_def.user_modifiable, False) + + def test_init_project_user_modifiable_unset(self): + """Test init with PROJECT_USER scope and user_modifiable unset""" + s_def = PluginAppSettingDef( + name=DEF_NAME, + scope=APP_SETTING_SCOPE_PROJECT_USER, + type=APP_SETTING_TYPE_BOOLEAN, + default=True, + ) + self.assertIsNotNone(s_def) + self.assertEqual(s_def.user_modifiable, False) + + def test_init_project_user_modifiable_true(self): + """Test init with PROJECT_USER scope and user_modifiable=True""" + with self.assertRaises(ValueError): + PluginAppSettingDef( + name=DEF_NAME, + scope=APP_SETTING_SCOPE_PROJECT_USER, + type=APP_SETTING_TYPE_BOOLEAN, + default=True, + user_modifiable=True, + )