Replace choice field with integer field for rating

main
Luca 2 years ago
parent de064195ff
commit 78f79646d1

@ -223,14 +223,3 @@ svg {
.row > :not(:first-child) { .row > :not(:first-child) {
margin-left: 0.5em !important; margin-left: 0.5em !important;
} }
.row > .rating-container {
justify-content: center;
margin: 0;
padding: 0;
}
.row > .rating-container > .rating > .rating-point {
height: 1em;
width: 1em;
}

@ -3,7 +3,7 @@ from django.conf import settings
class InsertForm(forms.Form): class InsertForm(forms.Form):
name = forms.CharField() name = forms.CharField()
rating = forms.ChoiceField(choices=settings.RATING_CHOICES, required=False, widget=forms.RadioSelect) rating = forms.IntegerField(max_value=10, min_value=1, required=False)
InsertFormSet = forms.formset_factory(InsertForm) InsertFormSet = forms.formset_factory(InsertForm)

@ -10,18 +10,9 @@
{% for form in formset %} {% for form in formset %}
<div class="row"> <div class="row">
{{ form.name }} {% for field in form %}
{{ field }}
<div class="rating-container">
<div class="rating" style="--num-choices: {{ form.rating | length }}">
{% for radio in form.rating %}
{{ radio.tag }}
<label class="rating-point" for="{{ radio.id_for_label }}"></label>
{% endfor %} {% endfor %}
</div>
</div>
<button class="reset" type="button">Zurücksetzen</button>
</div> </div>
{% endfor %} {% endfor %}
@ -76,10 +67,10 @@ const registerEvents = row => {
} }
}); });
row.querySelector('.reset').addEventListener('click', event => { row.querySelector('input[name$=rating]').addEventListener('keydown', event => {
event.target.parentElement.querySelectorAll('.rating-container > .rating > input').forEach(input => { if (event.key === 'Enter') {
input.checked = false; event.preventDefault();
}); }
}); });
}; };
document.querySelectorAll('.row').forEach(registerEvents); document.querySelectorAll('.row').forEach(registerEvents);
@ -110,9 +101,7 @@ addBtn.addEventListener('click', () => {
input.dataset.bestMatch = ''; input.dataset.bestMatch = '';
input.dataset.value = ''; input.dataset.value = '';
row.querySelectorAll('.rating-container > .rating > input').forEach(input => { row.querySelector('input[name$=rating]').value = undefined;
input.checked = false;
});
registerEvents(row); registerEvents(row);

@ -59,7 +59,10 @@ def insert(request, session):
artist = get_object_or_404(Artist, name=form.cleaned_data['name']) artist = get_object_or_404(Artist, name=form.cleaned_data['name'])
rating = Rating(artist=artist, participant=participant, value=form.cleaned_data['rating']) value = form.cleaned_data['rating']
value = str(value) if value else ''
rating = Rating(artist=artist, participant=participant, value=value)
rating.save() rating.save()
formset = InsertFormSet() # clear formset if successful formset = InsertFormSet() # clear formset if successful

Loading…
Cancel
Save