gestioCOF/bda/forms.py

159 lines
5.5 KiB
Python
Raw Normal View History

2016-07-15 00:02:56 +02:00
# -*- coding: utf-8 -*-
from django import forms
2017-04-21 18:22:53 +02:00
from django.forms.models import BaseInlineFormSet
2016-07-25 23:03:33 +02:00
from django.utils import timezone
2017-04-21 18:22:53 +02:00
from bda.models import Attribution, Spectacle, SpectacleRevente
2017-04-21 18:22:53 +02:00
class InscriptionInlineFormSet(BaseInlineFormSet):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
# self.instance is a Participant object
tirage = self.instance.tirage
# set once for all "spectacle" field choices
# - restrict choices to the spectacles of this tirage
# - force_choices avoid many db requests
spectacles = tirage.spectacle_set.select_related('location')
choices = [(sp.pk, str(sp)) for sp in spectacles]
self.force_choices('spectacle', choices)
def force_choices(self, name, choices):
"""Set choices of a field.
As ModelChoiceIterator (default use to get choices of a
ModelChoiceField), it appends an empty selection if requested.
"""
for form in self.forms:
field = form.fields[name]
if field.empty_label is not None:
field.choices = [('', field.empty_label)] + choices
else:
field.choices = choices
class TokenForm(forms.Form):
token = forms.CharField(widget=forms.widgets.Textarea())
2016-07-27 13:08:00 +02:00
class AttributionModelMultipleChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
2017-10-26 12:40:11 +02:00
return str(obj.spectacle)
class ReventeModelMultipleChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
2017-10-26 12:40:11 +02:00
return str(obj.attribution.spectacle)
class ResellForm(forms.Form):
2016-07-27 13:08:00 +02:00
attributions = AttributionModelMultipleChoiceField(
2017-02-16 04:52:44 +01:00
label='',
2016-07-25 23:03:33 +02:00
queryset=Attribution.objects.none(),
2016-07-27 13:08:00 +02:00
widget=forms.CheckboxSelectMultiple,
required=False)
def __init__(self, participant, *args, **kwargs):
super(ResellForm, self).__init__(*args, **kwargs)
2017-04-08 12:10:23 +02:00
self.fields['attributions'].queryset = (
participant.attribution_set
.filter(spectacle__date__gte=timezone.now())
.exclude(revente__seller=participant)
2017-04-08 12:10:23 +02:00
.select_related('spectacle', 'spectacle__location',
'participant__user')
)
2016-07-27 13:08:00 +02:00
2017-10-26 12:40:11 +02:00
2016-07-27 13:08:00 +02:00
class AnnulForm(forms.Form):
attributions = AttributionModelMultipleChoiceField(
2017-02-16 04:52:44 +01:00
label='',
2016-07-27 13:08:00 +02:00
queryset=Attribution.objects.none(),
widget=forms.CheckboxSelectMultiple,
required=False)
def __init__(self, participant, *args, **kwargs):
super(AnnulForm, self).__init__(*args, **kwargs)
2017-04-08 12:10:23 +02:00
self.fields['attributions'].queryset = (
participant.attribution_set
2016-07-27 13:08:00 +02:00
.filter(spectacle__date__gte=timezone.now(),
revente__isnull=False,
2017-02-16 05:28:57 +01:00
revente__notif_sent=False,
2016-12-21 03:45:30 +01:00
revente__soldTo__isnull=True)
2017-04-08 12:10:23 +02:00
.select_related('spectacle', 'spectacle__location',
'participant__user')
)
2016-07-27 13:08:00 +02:00
2017-10-26 12:40:11 +02:00
2016-07-27 13:08:00 +02:00
class InscriptionReventeForm(forms.Form):
spectacles = forms.ModelMultipleChoiceField(
queryset=Spectacle.objects.none(),
widget=forms.CheckboxSelectMultiple,
required=False)
def __init__(self, tirage, *args, **kwargs):
super(InscriptionReventeForm, self).__init__(*args, **kwargs)
2017-04-08 12:10:23 +02:00
self.fields['spectacles'].queryset = (
tirage.spectacle_set
.select_related('location')
.filter(date__gte=timezone.now())
)
2017-02-16 04:52:44 +01:00
2017-10-26 12:40:11 +02:00
class ReventeTirageAnnulForm(forms.Form):
reventes = ReventeModelMultipleChoiceField(
label='',
queryset=SpectacleRevente.objects.none(),
widget=forms.CheckboxSelectMultiple,
required=False
)
def __init__(self, participant, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['reventes'].queryset = (
participant.entered.filter(soldTo__isnull=True)
.select_related('attribution__spectacle')
)
class ReventeTirageForm(forms.Form):
reventes = ReventeModelMultipleChoiceField(
label='',
queryset=SpectacleRevente.objects.none(),
widget=forms.CheckboxSelectMultiple,
required=False
)
def __init__(self, participant, *args, **kwargs):
super().__init__(*args, **kwargs)
self.fields['reventes'].queryset = (
SpectacleRevente.objects.filter(
notif_sent=True,
shotgun=False,
tirage_done=False
).exclude(confirmed_entry=participant)
.select_related('attribution__spectacle')
)
2017-02-16 04:52:44 +01:00
2017-10-26 12:40:11 +02:00
2017-02-16 04:52:44 +01:00
class SoldForm(forms.Form):
attributions = AttributionModelMultipleChoiceField(
label='',
queryset=Attribution.objects.none(),
widget=forms.CheckboxSelectMultiple)
def __init__(self, participant, *args, **kwargs):
super(SoldForm, self).__init__(*args, **kwargs)
2017-02-21 23:49:29 +01:00
self.fields['attributions'].queryset = (
participant.attribution_set
2017-04-08 12:10:23 +02:00
.filter(revente__isnull=False,
revente__soldTo__isnull=False)
.exclude(revente__soldTo=participant)
.select_related('spectacle', 'spectacle__location',
'participant__user')
2017-02-21 23:49:29 +01:00
)