Skip to content

Commit

Permalink
refactor forms and search results view (#1389)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikkonie committed Jun 28, 2024
1 parent 383bc0a commit 635e758
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 184 deletions.
159 changes: 73 additions & 86 deletions projectroles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,19 +397,76 @@ def _set_app_setting_field(self, plugin_name, s_field, s_key, s_val):
:param s_val: Setting value
"""
s_widget_attrs = s_val.get('widget_attrs') or {}

# Set project type
s_project_types = s_val.get('project_types') or [PROJECT_TYPE_PROJECT]
s_widget_attrs['data-project-types'] = ','.join(s_project_types).lower()

if 'placeholder' in s_val:
s_widget_attrs['placeholder'] = s_val.get('placeholder')
setting_kwargs = {
'required': False,
'label': s_val.get('label') or '{}.{}'.format(plugin_name, s_key),
'help_text': s_val['description'],
}
if s_val['type'] == 'JSON':

# Option
if (
s_val.get('options')
and callable(s_val['options'])
and self.instance.pk
):
values = s_val['options'](project=self.instance)
self.fields[s_field] = forms.ChoiceField(
choices=[
(
(str(value[0]), str(value[1]))
if isinstance(value, tuple)
else (str(value), str(value))
)
for value in values
],
**setting_kwargs
)
elif (
s_val.get('options')
and callable(s_val['options'])
and not self.instance.pk
):
values = s_val['options'](project=None)
self.fields[s_field] = forms.ChoiceField(
choices=[
(
(str(value[0]), str(value[1]))
if isinstance(value, tuple)
else (str(value), str(value))
)
for value in values
],
**setting_kwargs
)
elif s_val.get('options'):
self.fields[s_field] = forms.ChoiceField(
choices=[
(
(int(option), int(option))
if s_val['type'] == 'INTEGER'
else (option, option)
)
for option in s_val['options']
],
**setting_kwargs
)
# Other types
elif s_val['type'] == 'STRING':
self.fields[s_field] = forms.CharField(
widget=forms.TextInput(attrs=s_widget_attrs), **setting_kwargs
)
elif s_val['type'] == 'INTEGER':
self.fields[s_field] = forms.IntegerField(
widget=forms.NumberInput(attrs=s_widget_attrs), **setting_kwargs
)
elif s_val['type'] == 'BOOLEAN':
self.fields[s_field] = forms.BooleanField(**setting_kwargs)
# JSON
elif s_val['type'] == 'JSON':
# NOTE: Attrs MUST be supplied here (#404)
if 'class' in s_widget_attrs:
s_widget_attrs['class'] += ' sodar-json-input'
Expand All @@ -418,89 +475,19 @@ def _set_app_setting_field(self, plugin_name, s_field, s_key, s_val):
self.fields[s_field] = forms.CharField(
widget=forms.Textarea(attrs=s_widget_attrs), **setting_kwargs
)
if self.instance.pk:
json_data = self.app_settings.get(
plugin_name=plugin_name,
setting_name=s_key,
project=self.instance,
)
else:
json_data = self.app_settings.get_default(
plugin_name=plugin_name,
setting_name=s_key,
project=None,
)
self.initial[s_field] = json.dumps(json_data)
else:
if s_val.get('options'):
if callable(s_val['options']) and self.instance.pk:
values = s_val['options'](project=self.instance)
self.fields[s_field] = forms.ChoiceField(
choices=[
(
(str(value[0]), str(value[1]))
if isinstance(value, tuple)
else (str(value), str(value))
)
for value in values
],
**setting_kwargs
)
elif callable(s_val['options']) and not self.instance.pk:
values = s_val['options'](project=None)
self.fields[s_field] = forms.ChoiceField(
choices=[
(
(str(value[0]), str(value[1]))
if isinstance(value, tuple)
else (str(value), str(value))
)
for value in values
],
**setting_kwargs
)
else:
self.fields[s_field] = forms.ChoiceField(
choices=[
(
(int(option), int(option))
if s_val['type'] == 'INTEGER'
else (option, option)
)
for option in s_val['options']
],
**setting_kwargs
)
elif s_val['type'] == 'STRING':
self.fields[s_field] = forms.CharField(
widget=forms.TextInput(attrs=s_widget_attrs),
**setting_kwargs
)
elif s_val['type'] == 'INTEGER':
self.fields[s_field] = forms.IntegerField(
widget=forms.NumberInput(attrs=s_widget_attrs),
**setting_kwargs
)
elif s_val['type'] == 'BOOLEAN':
self.fields[s_field] = forms.BooleanField(**setting_kwargs)

# Add optional attributes from plugin (#404)
# NOTE: Experimental! Use at your own risk!
self.fields[s_field].widget.attrs.update(s_widget_attrs)

# Set initial value
if self.instance.pk:
self.initial[s_field] = self.app_settings.get(
plugin_name=plugin_name,
setting_name=s_key,
project=self.instance,
)
else:
self.initial[s_field] = self.app_settings.get_default(
plugin_name=plugin_name,
setting_name=s_key,
project=None,
)
# Add optional attributes from plugin (#404)
# NOTE: Experimental! Use at your own risk!
self.fields[s_field].widget.attrs.update(s_widget_attrs)
# Set initial value
value = self.app_settings.get(
plugin_name=plugin_name,
setting_name=s_key,
project=self.instance if self.instance.pk else None,
)
if s_val['type'] == 'JSON':
value = json.dumps(value)
self.initial[s_field] = value

def _set_app_setting_notes(self, s_field, s_val, plugin):
"""
Expand Down
21 changes: 10 additions & 11 deletions projectroles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -780,6 +780,14 @@ class ProjectSearchResultsView(

template_name = 'projectroles/search_results.html'

def _handle_context(self, request, *args, **kwargs):
"""Handle context and render to response in GET/POST requests"""
context = self.get_context_data(*args, **kwargs)
if not context['search_terms']:
messages.error(request, 'No search terms provided.')
return redirect(reverse('home'))
return super().render_to_response(context)

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(**kwargs)
search_input = ''
Expand Down Expand Up @@ -808,7 +816,6 @@ def get_context_data(self, *args, **kwargs):
search_term += ' ' + s.lower()
if search_term:
search_terms = [search_term]

search_terms = list(dict.fromkeys(search_terms)) # Remove dupes

for s in keyword_input:
Expand Down Expand Up @@ -852,18 +859,10 @@ def get_context_data(self, *args, **kwargs):
return context

def get(self, request, *args, **kwargs):
context = self.get_context_data(*args, **kwargs)
if not context['search_terms']:
messages.error(request, 'No search terms provided.')
return redirect(reverse('home'))
return super().render_to_response(context)
return self._handle_context(request, *args, *kwargs)

def post(self, request, *args, **kwargs):
context = self.get_context_data(*args, **kwargs)
if not context['search_terms']:
messages.error(request, 'No search terms provided.')
return redirect(reverse('home'))
return super().render_to_response(context)
return self._handle_context(request, *args, *kwargs)


class ProjectAdvancedSearchView(
Expand Down
Loading

0 comments on commit 635e758

Please sign in to comment.