Skip to content

Commit

Permalink
refactor init_app_settings() (#1545)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Jan 17, 2025
1 parent 59d4cea commit 5eda806
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 87 deletions.
99 changes: 41 additions & 58 deletions projectroles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,36 @@ def get_app_setting_label(self, plugin, label):
label,
)

def init_app_settings(self, app_plugins, scope, user_mod):
"""
Initialize app settings in form. Also sets up self.app_plugins.
:param app_plugins: List of app plugin objects
:param scope: App setting scope (string)
:param user_mod: Only include user modifiable settings if True (boolean)
"""
for plugin in app_plugins + [None]: # No plugin for projectroles
if plugin:
plugin_name = plugin.name
s_defs = app_settings.get_definitions(
scope,
plugin=plugin,
user_modifiable=user_mod,
)
else:
plugin_name = APP_NAME
s_defs = app_settings.get_definitions(
scope,
plugin_name=plugin_name,
user_modifiable=user_mod,
)
for s_def in s_defs.values():
s_field = 'settings.{}.{}'.format(plugin_name, s_def.name)
# Set field, widget and value
self.set_app_setting_field(plugin_name, s_field, s_def)
# Set label notes
self.set_app_setting_notes(plugin, s_field, s_def)

@classmethod
def clean_app_settings(
cls,
Expand Down Expand Up @@ -585,36 +615,6 @@ def _init_remote_sites(self):
required=False,
)

# TODO: Refactor into generic helper in SODARAppSettingFormMixin
def _init_app_settings(self):
"""
Initialize app settings fields in the form.
"""
# Show unmodifiable settings to superusers
user_mod = False if self.current_user.is_superuser else True
self.app_plugins = sorted(get_active_plugins(), key=lambda x: x.name)
for plugin in self.app_plugins + [None]: # Projectroles has no plugin
if plugin:
plugin_name = plugin.name
s_defs = app_settings.get_definitions(
APP_SETTING_SCOPE_PROJECT,
plugin=plugin,
user_modifiable=user_mod,
)
else:
plugin_name = APP_NAME
s_defs = app_settings.get_definitions(
APP_SETTING_SCOPE_PROJECT,
plugin_name=plugin_name,
user_modifiable=user_mod,
)
for s_def in s_defs.values():
s_field = 'settings.{}.{}'.format(plugin_name, s_def.name)
# Set field, widget and value
self.set_app_setting_field(plugin_name, s_field, s_def)
# Set label notes
self.set_app_setting_notes(plugin, s_field, s_def)

def __init__(self, project=None, current_user=None, *args, **kwargs):
"""Override for form initialization"""
super().__init__(*args, **kwargs)
Expand All @@ -634,8 +634,12 @@ def __init__(self, project=None, current_user=None, *args, **kwargs):
or (self.instance.pk and self.instance.type == PROJECT_TYPE_PROJECT)
):
self._init_remote_sites()
# Add settings fields
self._init_app_settings()
# Init app settings fields
self.app_plugins = sorted(get_active_plugins(), key=lambda x: x.name)
user_mod = False if self.current_user.is_superuser else True
self.init_app_settings(
self.app_plugins, APP_SETTING_SCOPE_PROJECT, user_mod
)

# Update help texts to match DISPLAY_NAMES
self.fields['title'].help_text = 'Title'
Expand Down Expand Up @@ -1211,32 +1215,11 @@ class SiteAppSettingsForm(SODARAppSettingFormMixin, SODARForm):

def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# Add settings fields
# TODO: Refactor into generic helper in SODARAppSettingFormMixin
self.project_plugins = get_active_plugins(plugin_type='project_app')
self.site_plugins = get_active_plugins(plugin_type='site_app')
self.app_plugins = self.project_plugins + self.site_plugins

for plugin in self.app_plugins + [None]:
if plugin:
plugin_name = plugin.name
s_defs = app_settings.get_definitions(
APP_SETTING_SCOPE_SITE, plugin=plugin, user_modifiable=True
)
else:
plugin_name = 'projectroles'
s_defs = app_settings.get_definitions(
APP_SETTING_SCOPE_SITE,
plugin_name=plugin_name,
user_modifiable=True,
)
for s_def in s_defs.values():
s_field = 'settings.{}.{}'.format(plugin_name, s_def.name)
self.set_app_setting_field(plugin_name, s_field, s_def)
self.fields[s_field].label = self.get_app_setting_label(
plugin, self.fields[s_field].label
)
self.set_app_setting_notes(plugin, s_field, s_def)
# Init app settings fields
project_plugins = get_active_plugins(plugin_type='project_app')
site_plugins = get_active_plugins(plugin_type='site_app')
self.app_plugins = project_plugins + site_plugins
self.init_app_settings(self.app_plugins, APP_SETTING_SCOPE_SITE, True)

def clean(self):
cleaned_data, errors = self.clean_app_settings(
Expand Down
6 changes: 3 additions & 3 deletions projectroles/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4975,15 +4975,15 @@ def test_post_delegate_no_perms(self):
self.assertEqual(ProjectInvite.objects.filter(active=True).count(), 1)


class TestSiteAppSettingsFormView(ViewTestBase):
"""Tests for SiteAppSettingsFormView"""
class TestSiteAppSettingsView(ViewTestBase):
"""Tests for SiteAppSettingsView"""

def setUp(self):
super().setUp()
self.url = reverse('projectroles:site_app_settings')

def test_get(self):
"""Test SiteAppSettingsFormView GET"""
"""Test SiteAppSettingsView GET"""
with self.login(self.user):
response = self.client.get(self.url)
self.assertEqual(response.status_code, 200)
Expand Down
34 changes: 8 additions & 26 deletions userprofile/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,32 +35,14 @@ class UserAppSettingsForm(SODARAppSettingFormMixin, SODARForm):
def __init__(self, *args, **kwargs):
self.user = kwargs.pop('current_user')
super().__init__(*args, **kwargs)
# Add settings fields
# TODO: Refactor into generic helper in SODARAppSettingFormMixin
self.app_plugins = get_active_plugins(plugin_type='project_app')
self.user_plugins = get_active_plugins(plugin_type='site_app')
self.app_plugins = self.app_plugins + self.user_plugins

for plugin in self.app_plugins + [None]:
if plugin:
plugin_name = plugin.name
s_defs = app_settings.get_definitions(
APP_SETTING_SCOPE_USER, plugin=plugin, user_modifiable=True
)
else:
plugin_name = 'projectroles'
s_defs = app_settings.get_definitions(
APP_SETTING_SCOPE_USER,
plugin_name=plugin_name,
user_modifiable=True,
)
for s_def in s_defs.values():
s_field = 'settings.{}.{}'.format(plugin_name, s_def.name)
self.set_app_setting_field(plugin_name, s_field, s_def)
self.fields[s_field].label = self.get_app_setting_label(
plugin, self.fields[s_field].label
)
self.set_app_setting_notes(plugin, s_field, s_def)
# Init app settings fields
project_plugins = get_active_plugins(plugin_type='project_app')
site_plugins = get_active_plugins(plugin_type='site_app')
self.app_plugins = project_plugins + site_plugins
user_mod = False if self.user.is_superuser else True
self.init_app_settings(
self.app_plugins, APP_SETTING_SCOPE_USER, user_mod
)

def clean(self):
cleaned_data, errors = self.clean_app_settings(
Expand Down

0 comments on commit 5eda806

Please sign in to comment.