Fix concurrency issues

Creating form fields in the class and modifiying them dynamically can
cause concurrency issues because the form class is shared between tabs.
This commit is contained in:
Ludovic Stephan 2019-01-07 14:59:20 +01:00
parent a30955fb75
commit 519ef9dc20

View file

@ -90,24 +90,20 @@ class ResellForm(forms.Form):
class AnnulForm(forms.Form): class AnnulForm(forms.Form):
reventes = ReventeModelMultipleChoiceField(
own=True,
label="",
queryset=Attribution.objects.none(),
widget=forms.CheckboxSelectMultiple,
required=False,
)
def __init__(self, participant, *args, **kwargs): def __init__(self, participant, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.fields["reventes"].queryset = ( self.fields["reventes"] = ReventeModelMultipleChoiceField(
participant.original_shows.filter( own=True,
label="",
queryset=participant.original_shows.filter(
attribution__spectacle__date__gte=timezone.now(), soldTo__isnull=True attribution__spectacle__date__gte=timezone.now(), soldTo__isnull=True
) )
.select_related( .select_related(
"attribution__spectacle", "attribution__spectacle__location" "attribution__spectacle", "attribution__spectacle__location"
) )
.order_by("-date") .order_by("-date"),
widget=forms.CheckboxSelectMultiple,
required=False,
) )
@ -142,8 +138,12 @@ class TemplateLabelField(forms.ModelMultipleChoiceField):
class InscriptionReventeForm(forms.Form): class InscriptionReventeForm(forms.Form):
spectacles = TemplateLabelField( def __init__(self, tirage, *args, **kwargs):
queryset=Spectacle.objects.none(), super().__init__(*args, **kwargs)
self.fields["spectacles"] = TemplateLabelField(
queryset=tirage.spectacle_set.select_related("location").filter(
date__gte=timezone.now()
),
widget=forms.CheckboxSelectMultiple, widget=forms.CheckboxSelectMultiple,
required=False, required=False,
label_template_name="bda/forms/spectacle_label_table.html", label_template_name="bda/forms/spectacle_label_table.html",
@ -151,16 +151,14 @@ class InscriptionReventeForm(forms.Form):
context_object_name="spectacle", context_object_name="spectacle",
) )
def __init__(self, tirage, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields["spectacles"].queryset = tirage.spectacle_set.select_related(
"location"
).filter(date__gte=timezone.now())
class ReventeTirageAnnulForm(forms.Form): class ReventeTirageAnnulForm(forms.Form):
reventes = TemplateLabelField( def __init__(self, participant, *args, **kwargs):
queryset=SpectacleRevente.objects.none(), super().__init__(*args, **kwargs)
self.fields["reventes"] = TemplateLabelField(
queryset=participant.entered.filter(soldTo__isnull=True).select_related(
"attribution__spectacle", "seller__user"
),
widget=forms.CheckboxSelectMultiple, widget=forms.CheckboxSelectMultiple,
required=False, required=False,
label_template_name="bda/forms/revente_other_label_table.html", label_template_name="bda/forms/revente_other_label_table.html",
@ -168,11 +166,9 @@ class ReventeTirageAnnulForm(forms.Form):
context_object_name="revente", context_object_name="revente",
) )
def __init__(self, participant, *args, **kwargs): participant.entered.filter(soldTo__isnull=True).select_related(
super().__init__(*args, **kwargs) "attribution__spectacle", "seller__user"
self.fields["reventes"].queryset = participant.entered.filter( )
soldTo__isnull=True
).select_related("attribution__spectacle", "seller__user")
class ReventeTirageForm(forms.Form): class ReventeTirageForm(forms.Form):
@ -196,19 +192,16 @@ class ReventeTirageForm(forms.Form):
class SoldForm(forms.Form): class SoldForm(forms.Form):
reventes = ReventeModelMultipleChoiceField(
own=True,
label="",
queryset=Attribution.objects.none(),
widget=forms.CheckboxSelectMultiple,
)
def __init__(self, participant, *args, **kwargs): def __init__(self, participant, *args, **kwargs):
super().__init__(*args, **kwargs) super().__init__(*args, **kwargs)
self.fields["reventes"].queryset = ( self.fields["reventes"].queryset = ReventeModelMultipleChoiceField(
participant.original_shows.filter(soldTo__isnull=False) own=True,
label="",
queryset=participant.original_shows.filter(soldTo__isnull=False)
.exclude(soldTo=participant) .exclude(soldTo=participant)
.select_related( .select_related(
"attribution__spectacle", "attribution__spectacle__location" "attribution__spectacle", "attribution__spectacle__location"
),
widget=forms.CheckboxSelectMultiple,
) )
)