diff --git a/bda/forms.py b/bda/forms.py index 94a52128..8e3038a6 100644 --- a/bda/forms.py +++ b/bda/forms.py @@ -1,5 +1,6 @@ from django import forms from django.forms.models import BaseInlineFormSet +from django.template import loader from django.utils import timezone from bda.models import Attribution, Spectacle, SpectacleRevente @@ -110,11 +111,32 @@ class AnnulForm(forms.Form): ) +class TemplateLabelField(forms.ModelMultipleChoiceField): + """ + Offers an option to render a label with Django template rendering + """ + + def __init__(self, template_name=None, context_object_name="obj", *args, **kwargs): + super().__init__(*args, **kwargs) + self.template_name = template_name + self.context_object_name = context_object_name + + def label_from_instance(self, obj): + if self.template_name is None: + return super().label_from_instance(obj) + else: + return loader.render_to_string( + self.template_name, context={self.context_object_name: obj} + ) + + class InscriptionReventeForm(forms.Form): - spectacles = forms.ModelMultipleChoiceField( + spectacles = TemplateLabelField( queryset=Spectacle.objects.none(), widget=forms.CheckboxSelectMultiple, required=False, + template_name="bda/forms/spectacle_label_table.html", + context_object_name="spectacle", ) def __init__(self, tirage, *args, **kwargs): @@ -122,6 +144,9 @@ class InscriptionReventeForm(forms.Form): self.fields["spectacles"].queryset = tirage.spectacle_set.select_related( "location" ).filter(date__gte=timezone.now()) + self.fields[ + "spectacles" + ].widget.option_template_name = "bda/forms/spectacle_checkbox_table.html" class ReventeTirageAnnulForm(forms.Form): diff --git a/bda/templates/bda/forms/spectacle_checkbox_table.html b/bda/templates/bda/forms/spectacle_checkbox_table.html new file mode 100644 index 00000000..e13a9c9d --- /dev/null +++ b/bda/templates/bda/forms/spectacle_checkbox_table.html @@ -0,0 +1,4 @@ +