Skip to content

Commit

Permalink
cleanup redundant s_key references (#1456)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Jan 7, 2025
1 parent f0e41b2 commit 85b718d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
24 changes: 10 additions & 14 deletions projectroles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,6 @@
SETTING_SOURCE_ONLY_MSG = '[Only editable on source site]'


# TODO: Remove redundant s_key fields (not needed)


# Base Classes and Mixins ------------------------------------------------------


Expand Down Expand Up @@ -390,13 +387,12 @@ def _init_remote_sites(self):
required=False,
)

def _set_app_setting_field(self, plugin_name, s_field, s_key, s_def):
def _set_app_setting_field(self, plugin_name, s_field, s_def):
"""
Internal helper for setting app setting field, widget and value.
:param plugin_name: App plugin name
:param s_field: Form field name
:param s_key: Setting key
:param s_def: PluginAppSettingDef object
"""
s_widget_attrs = s_def.widget_attrs
Expand All @@ -406,7 +402,7 @@ def _set_app_setting_field(self, plugin_name, s_field, s_key, s_def):
s_widget_attrs['placeholder'] = s_def.placeholder
setting_kwargs = {
'required': False,
'label': s_def.label or '{}.{}'.format(plugin_name, s_key),
'label': s_def.label or '{}.{}'.format(plugin_name, s_def.name),
'help_text': s_def.description,
}

Expand Down Expand Up @@ -477,7 +473,7 @@ def _set_app_setting_field(self, plugin_name, s_field, s_key, s_def):
# Set initial value
value = self.app_settings.get(
plugin_name=plugin_name,
setting_name=s_key,
setting_name=s_def.name,
project=self.instance if self.instance.pk else None,
)
if s_def.type == APP_SETTING_TYPE_JSON:
Expand Down Expand Up @@ -532,10 +528,10 @@ def _init_app_settings(self):
plugin_name=plugin_name,
**self.p_kwargs
)
for s_key, s_def in s_defs.items():
s_field = 'settings.{}.{}'.format(plugin_name, s_key)
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_key, s_def)
self._set_app_setting_field(plugin_name, s_field, s_def)
# Set label notes
self._set_app_setting_notes(s_field, s_def, plugin)

Expand All @@ -562,12 +558,12 @@ def _validate_app_settings(
)
p_settings = {}

for s_key, s_def in s_defs.items():
s_field = '.'.join(['settings', p_name, s_key])
p_settings[s_key] = cleaned_data.get(s_field)
for s_def in s_defs.values():
s_field = '.'.join(['settings', p_name, s_def.name])
p_settings[s_def.name] = cleaned_data.get(s_field)

if s_def.type == APP_SETTING_TYPE_JSON:
if not p_settings[s_key]:
if not p_settings[s_def.name]:
cleaned_data[s_field] = '{}'
try:
cleaned_data[s_field] = json.loads(
Expand Down
6 changes: 3 additions & 3 deletions projectroles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -976,14 +976,14 @@ def _get_app_settings(data, instance, user):
APP_SETTING_SCOPE_PROJECT, plugin_name=name, **p_kwargs
)

for s_key, s_def in p_settings.items():
s_name = 'settings.{}.{}'.format(name, s_key)
for s_def in p_settings.values():
s_name = 'settings.{}.{}'.format(name, s_def.name)
s_data = data.get(s_name)

if instance.type not in s_def.project_types:
continue
if s_data is None and not instance:
s_data = app_settings.get_default(name, s_key)
s_data = app_settings.get_default(name, s_def.name)
if s_def.type == APP_SETTING_TYPE_JSON:
if s_data is None:
s_data = {}
Expand Down
30 changes: 13 additions & 17 deletions userprofile/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,27 +34,23 @@
APP_SETTING_TYPE_STRING = SODAR_CONSTANTS['APP_SETTING_TYPE_STRING']


# TODO: Remove redundant s_key fields (not needed)


class UserSettingsForm(SODARForm):
"""Form for configuring user settings"""

def _set_app_setting_field(self, plugin_name, s_field, s_key, s_def):
def _set_app_setting_field(self, plugin_name, s_field, s_def):
"""
Set user app setting field, widget and value.
:param plugin_name: App plugin name
:param s_field: Form field name
:param s_key: Setting key
:param s_def: PluginAppSettingDef
:param s_def: PluginAppSettingDef object
"""
s_widget_attrs = s_def.widget_attrs
if s_def.placeholder is not None:
s_widget_attrs['placeholder'] = s_def.placeholder
setting_kwargs = {
'required': False,
'label': s_def.label or '{}.{}'.format(plugin_name, s_key),
'label': s_def.label or '{}.{}'.format(plugin_name, s_def.name),
'help_text': s_def.description,
}
# Disable global user settings if on target site
Expand Down Expand Up @@ -116,7 +112,7 @@ def _set_app_setting_field(self, plugin_name, s_field, s_key, s_def):
# Modify initial value and attributes
self.fields[s_field].widget.attrs.update(s_widget_attrs)
value = app_settings.get(
plugin_name=plugin_name, setting_name=s_key, user=self.user
plugin_name=plugin_name, setting_name=s_def.name, user=self.user
)
if s_def.type == APP_SETTING_TYPE_JSON:
value = json.dumps(value)
Expand All @@ -132,20 +128,20 @@ def __init__(self, *args, **kwargs):

for plugin in self.app_plugins + [None]:
if plugin:
name = plugin.name
plugin_name = plugin.name
s_defs = app_settings.get_definitions(
APP_SETTING_SCOPE_USER, plugin=plugin, user_modifiable=True
)
else:
name = 'projectroles'
plugin_name = 'projectroles'
s_defs = app_settings.get_definitions(
APP_SETTING_SCOPE_USER,
plugin_name=name,
plugin_name=plugin_name,
user_modifiable=True,
)
for s_key, s_def in s_defs.items():
s_field = 'settings.{}.{}'.format(name, s_key)
self._set_app_setting_field(name, s_field, s_key, s_def)
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
)
Expand All @@ -165,9 +161,9 @@ def clean(self):
)
p_settings = {}

for s_key, s_def in s_defs.items():
s_field = '.'.join(['settings', p_name, s_key])
p_settings[s_key] = self.cleaned_data.get(s_field)
for s_def in s_defs.values():
s_field = '.'.join(['settings', p_name, s_def.name])
p_settings[s_def.name] = self.cleaned_data.get(s_field)

if s_def.type == APP_SETTING_TYPE_JSON:
if not self.cleaned_data.get(s_field):
Expand Down

0 comments on commit 85b718d

Please sign in to comment.