From e54a5ff87b8fb5275ab6fa51f9bfbfa767752020 Mon Sep 17 00:00:00 2001 From: allholy Date: Wed, 15 Jan 2025 21:40:54 +0100 Subject: [PATCH] Implement frontend layout for BST category field --- .../src/pages/editDescribeSounds.js | 4 +- sounds/forms.py | 11 +- sounds/templatetags/bst_category.py | 11 ++ sounds/views.py | 3 +- .../molecules/bst_category_form_field.html | 124 ++++++++++++++++++ templates/sounds/edit_and_describe.html | 4 +- 6 files changed, 150 insertions(+), 7 deletions(-) create mode 100644 sounds/templatetags/bst_category.py create mode 100644 templates/molecules/bst_category_form_field.html diff --git a/freesound/static/bw-frontend/src/pages/editDescribeSounds.js b/freesound/static/bw-frontend/src/pages/editDescribeSounds.js index 747313cf3..94922d16d 100644 --- a/freesound/static/bw-frontend/src/pages/editDescribeSounds.js +++ b/freesound/static/bw-frontend/src/pages/editDescribeSounds.js @@ -28,4 +28,6 @@ inputTypeSubmitElements.forEach(button => { } }); }); -}); \ No newline at end of file +}); + +// Move json for BST category field in description form here diff --git a/sounds/forms.py b/sounds/forms.py index cd23d441b..82a66486e 100644 --- a/sounds/forms.py +++ b/sounds/forms.py @@ -23,7 +23,7 @@ from captcha.fields import ReCaptchaField from django import forms -from django.core.exceptions import PermissionDenied +from django.core.exceptions import PermissionDenied, ValidationError from django.db.models import Q from django.forms import ModelForm, Textarea, TextInput from django.core.signing import BadSignature, SignatureExpired @@ -247,7 +247,8 @@ class SoundEditAndDescribeForm(forms.Form): widget=forms.TextInput(attrs={'size': 65, 'class': 'inputText'})) bst_category = forms.ChoiceField( choices=Sound.BST_CATEGORY_CHOICES, - required=False + help_text="Choose the most appropriate catgeory and subcatgoery (you can only choose one). This category will be displayed as a filtering option in the Freesound side bar.", + required=True, ) tags = TagField( widget=forms.Textarea(attrs={'cols': 80, 'rows': 3}), @@ -348,6 +349,12 @@ def clean_sources(self): def clean_pack(self): return _pack_form_clean_pack_helper(self.cleaned_data) + + def clean_bst_category(self): + value = self.cleaned_data['bst_category'] + if not '-' in value: + raise ValidationError("Please choose a subcategory.") + return value class SoundCSVDescriptionForm(SoundEditAndDescribeForm): diff --git a/sounds/templatetags/bst_category.py b/sounds/templatetags/bst_category.py new file mode 100644 index 000000000..530ce30a7 --- /dev/null +++ b/sounds/templatetags/bst_category.py @@ -0,0 +1,11 @@ +from django import template + +register = template.Library() + +@register.filter +def get_top_level_bst_category(value): + """ + Extract the top level category from the given value. + The top level category is the part before the '-' in value. + """ + return value.split('-')[0] if '-' in value else value diff --git a/sounds/views.py b/sounds/views.py index f09214b6b..1d56869dc 100644 --- a/sounds/views.py +++ b/sounds/views.py @@ -661,7 +661,8 @@ def update_edited_sound(sound, data): 'sounds_per_round': forms_per_round, 'last_latlong': request.user.profile.get_last_latlong(), 'total_sounds_to_describe': len_original_describe_edit_sounds, - 'next': request.GET.get('next', '') + 'next': request.GET.get('next', ''), + 'bst_taxonomy': settings.BROAD_SOUND_TAXONOMY } if request.method == "POST" and all_forms_validated_ok: diff --git a/templates/molecules/bst_category_form_field.html b/templates/molecules/bst_category_form_field.html new file mode 100644 index 000000000..b9ca50975 --- /dev/null +++ b/templates/molecules/bst_category_form_field.html @@ -0,0 +1,124 @@ +{% load bst_category %} + +
+ + {{ form.bst_category.errors }} +
+ {{ form.bst_category.as_hidden }} + +
+ {% for value, label in form.fields.bst_category.choices %} + {% if "-" not in value %} + + {% endif %} + {% endfor %} +
+ + +
+ {{ form.bst_category.help_text|safe }} +
+
+ + diff --git a/templates/sounds/edit_and_describe.html b/templates/sounds/edit_and_describe.html index 6650dd683..f0b11e155 100644 --- a/templates/sounds/edit_and_describe.html +++ b/templates/sounds/edit_and_describe.html @@ -71,9 +71,7 @@
Basic information
{{ form.name }}
- {{ form.bst_category.errors }} - {{ form.bst_category.label_tag }} - {{ form.bst_category }} + {% include "molecules/bst_category_form_field.html" %}
{% include "molecules/tags_form_field.html" %}