2016-05-27 17:52:02 +02:00
|
|
|
from django import forms
|
2017-04-21 18:22:53 +02:00
|
|
|
from django.forms.models import BaseInlineFormSet
|
2018-11-21 16:14:52 +01:00
|
|
|
from django.template import loader
|
2016-07-25 23:03:33 +02:00
|
|
|
from django.utils import timezone
|
2017-04-21 18:22:53 +02:00
|
|
|
|
2017-10-23 17:25:58 +02:00
|
|
|
from bda.models import Attribution, Spectacle, SpectacleRevente
|
2016-05-27 17:52:02 +02:00
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
|
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
|
2018-10-06 12:35:49 +02:00
|
|
|
spectacles = tirage.spectacle_set.select_related("location")
|
2017-04-21 18:22:53 +02:00
|
|
|
choices = [(sp.pk, str(sp)) for sp in spectacles]
|
2018-10-06 12:35:49 +02:00
|
|
|
self.force_choices("spectacle", choices)
|
2017-04-21 18:22:53 +02:00
|
|
|
|
|
|
|
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:
|
2018-10-06 12:35:49 +02:00
|
|
|
field.choices = [("", field.empty_label)] + choices
|
2017-04-21 18:22:53 +02:00
|
|
|
else:
|
|
|
|
field.choices = choices
|
|
|
|
|
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
class TokenForm(forms.Form):
|
2016-06-05 02:16:14 +02:00
|
|
|
token = forms.CharField(widget=forms.widgets.Textarea())
|
2016-05-27 17:52:02 +02:00
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2016-07-27 13:08:00 +02:00
|
|
|
class AttributionModelMultipleChoiceField(forms.ModelMultipleChoiceField):
|
2016-05-27 17:52:02 +02:00
|
|
|
def label_from_instance(self, obj):
|
2017-10-26 12:40:11 +02:00
|
|
|
return str(obj.spectacle)
|
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
|
2017-10-23 17:25:58 +02:00
|
|
|
class ReventeModelMultipleChoiceField(forms.ModelMultipleChoiceField):
|
2017-12-19 12:40:50 +01:00
|
|
|
def __init__(self, *args, own=True, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
|
|
|
self.own = own
|
|
|
|
|
2017-10-23 17:25:58 +02:00
|
|
|
def label_from_instance(self, obj):
|
2017-12-19 12:40:50 +01:00
|
|
|
label = "{show}{suffix}"
|
|
|
|
suffix = ""
|
|
|
|
if self.own:
|
2018-04-13 10:54:45 +02:00
|
|
|
# C'est notre propre revente : informations sur le statut
|
2017-12-19 12:40:50 +01:00
|
|
|
if obj.soldTo is not None:
|
|
|
|
suffix = " -- Vendue à {firstname} {lastname}".format(
|
2018-10-06 12:35:49 +02:00
|
|
|
firstname=obj.soldTo.user.first_name,
|
|
|
|
lastname=obj.soldTo.user.last_name,
|
|
|
|
)
|
2018-04-13 10:54:45 +02:00
|
|
|
elif obj.shotgun:
|
|
|
|
suffix = " -- Tirage infructueux"
|
|
|
|
elif obj.notif_sent:
|
|
|
|
suffix = " -- Inscriptions au tirage en cours"
|
2017-12-19 12:40:50 +01:00
|
|
|
else:
|
|
|
|
# Ce n'est pas à nous : on ne voit jamais l'acheteur
|
|
|
|
suffix = " -- Vendue par {firstname} {lastname}".format(
|
2018-10-06 12:35:49 +02:00
|
|
|
firstname=obj.seller.user.first_name, lastname=obj.seller.user.last_name
|
|
|
|
)
|
2017-12-19 12:40:50 +01:00
|
|
|
|
2018-10-06 12:35:49 +02:00
|
|
|
return label.format(show=str(obj.attribution.spectacle), suffix=suffix)
|
2017-10-26 12:40:11 +02:00
|
|
|
|
2016-07-09 22:31:56 +02:00
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
class ResellForm(forms.Form):
|
2016-07-27 13:08:00 +02:00
|
|
|
attributions = AttributionModelMultipleChoiceField(
|
2018-10-06 12:35:49 +02:00
|
|
|
label="",
|
|
|
|
queryset=Attribution.objects.none(),
|
|
|
|
widget=forms.CheckboxSelectMultiple,
|
|
|
|
required=False,
|
|
|
|
)
|
2016-05-27 17:52:02 +02:00
|
|
|
|
|
|
|
def __init__(self, participant, *args, **kwargs):
|
2018-01-16 16:22:52 +01:00
|
|
|
super().__init__(*args, **kwargs)
|
2018-10-06 12:35:49 +02:00
|
|
|
self.fields["attributions"].queryset = (
|
|
|
|
participant.attribution_set.filter(spectacle__date__gte=timezone.now())
|
2016-09-05 02:29:49 +02:00
|
|
|
.exclude(revente__seller=participant)
|
2018-10-06 12:35:49 +02:00
|
|
|
.select_related("spectacle", "spectacle__location", "participant__user")
|
2017-04-08 12:10:23 +02:00
|
|
|
)
|
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):
|
|
|
|
def __init__(self, participant, *args, **kwargs):
|
2018-01-16 16:22:52 +01:00
|
|
|
super().__init__(*args, **kwargs)
|
2019-01-07 14:59:20 +01:00
|
|
|
self.fields["reventes"] = ReventeModelMultipleChoiceField(
|
|
|
|
own=True,
|
|
|
|
label="",
|
|
|
|
queryset=participant.original_shows.filter(
|
2018-10-07 00:34:36 +02:00
|
|
|
attribution__spectacle__date__gte=timezone.now(), soldTo__isnull=True
|
|
|
|
)
|
|
|
|
.select_related(
|
|
|
|
"attribution__spectacle", "attribution__spectacle__location"
|
|
|
|
)
|
2019-01-07 14:59:20 +01:00
|
|
|
.order_by("-date"),
|
|
|
|
widget=forms.CheckboxSelectMultiple,
|
|
|
|
required=False,
|
2017-04-08 12:10:23 +02:00
|
|
|
)
|
2016-07-27 13:08:00 +02:00
|
|
|
|
2017-10-26 12:40:11 +02:00
|
|
|
|
2018-11-21 16:14:52 +01:00
|
|
|
class TemplateLabelField(forms.ModelMultipleChoiceField):
|
|
|
|
"""
|
2018-12-07 17:04:18 +01:00
|
|
|
Extends ModelMultipleChoiceField to offer two more customization options :
|
|
|
|
- `label_from_instance` can be used with a template file
|
|
|
|
- the widget rendering template can be specified with `option_template_name`
|
2018-11-21 16:14:52 +01:00
|
|
|
"""
|
|
|
|
|
2018-12-07 17:04:18 +01:00
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
label_template_name=None,
|
|
|
|
context_object_name="obj",
|
|
|
|
option_template_name=None,
|
|
|
|
*args,
|
|
|
|
**kwargs
|
|
|
|
):
|
2018-11-21 16:14:52 +01:00
|
|
|
super().__init__(*args, **kwargs)
|
2018-12-07 17:04:18 +01:00
|
|
|
self.label_template_name = label_template_name
|
2018-11-21 16:14:52 +01:00
|
|
|
self.context_object_name = context_object_name
|
2018-12-07 17:04:18 +01:00
|
|
|
if option_template_name is not None:
|
|
|
|
self.widget.option_template_name = option_template_name
|
2018-11-21 16:14:52 +01:00
|
|
|
|
|
|
|
def label_from_instance(self, obj):
|
2018-12-07 17:04:18 +01:00
|
|
|
if self.label_template_name is None:
|
2018-11-21 16:14:52 +01:00
|
|
|
return super().label_from_instance(obj)
|
|
|
|
else:
|
|
|
|
return loader.render_to_string(
|
2018-12-07 17:04:18 +01:00
|
|
|
self.label_template_name, context={self.context_object_name: obj}
|
2018-11-21 16:14:52 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
2016-07-27 13:08:00 +02:00
|
|
|
class InscriptionReventeForm(forms.Form):
|
|
|
|
def __init__(self, tirage, *args, **kwargs):
|
2018-01-16 16:22:52 +01:00
|
|
|
super().__init__(*args, **kwargs)
|
2019-01-07 14:59:20 +01:00
|
|
|
self.fields["spectacles"] = TemplateLabelField(
|
|
|
|
queryset=tirage.spectacle_set.select_related("location").filter(
|
|
|
|
date__gte=timezone.now()
|
|
|
|
),
|
|
|
|
widget=forms.CheckboxSelectMultiple,
|
|
|
|
required=False,
|
|
|
|
label_template_name="bda/forms/spectacle_label_table.html",
|
|
|
|
option_template_name="bda/forms/checkbox_table.html",
|
|
|
|
context_object_name="spectacle",
|
|
|
|
)
|
2017-02-16 04:52:44 +01:00
|
|
|
|
2017-10-26 12:40:11 +02:00
|
|
|
|
2017-10-23 17:25:58 +02:00
|
|
|
class ReventeTirageAnnulForm(forms.Form):
|
|
|
|
def __init__(self, participant, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2019-01-07 14:59:20 +01:00
|
|
|
self.fields["reventes"] = TemplateLabelField(
|
|
|
|
queryset=participant.entered.filter(soldTo__isnull=True).select_related(
|
|
|
|
"attribution__spectacle", "seller__user"
|
|
|
|
),
|
|
|
|
widget=forms.CheckboxSelectMultiple,
|
|
|
|
required=False,
|
|
|
|
label_template_name="bda/forms/revente_other_label_table.html",
|
|
|
|
option_template_name="bda/forms/checkbox_table.html",
|
|
|
|
context_object_name="revente",
|
|
|
|
)
|
|
|
|
|
|
|
|
participant.entered.filter(soldTo__isnull=True).select_related(
|
|
|
|
"attribution__spectacle", "seller__user"
|
|
|
|
)
|
2017-10-23 17:25:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ReventeTirageForm(forms.Form):
|
|
|
|
def __init__(self, participant, *args, **kwargs):
|
|
|
|
super().__init__(*args, **kwargs)
|
2018-12-07 17:35:40 +01:00
|
|
|
|
|
|
|
self.fields["reventes"] = TemplateLabelField(
|
|
|
|
queryset=(
|
|
|
|
SpectacleRevente.objects.filter(
|
|
|
|
notif_sent=True, shotgun=False, tirage_done=False
|
|
|
|
)
|
|
|
|
.exclude(confirmed_entry=participant)
|
|
|
|
.select_related("attribution__spectacle")
|
|
|
|
),
|
|
|
|
widget=forms.CheckboxSelectMultiple,
|
|
|
|
required=False,
|
|
|
|
label_template_name="bda/forms/revente_other_label_table.html",
|
|
|
|
option_template_name="bda/forms/checkbox_table.html",
|
|
|
|
context_object_name="revente",
|
2017-10-23 17:25:58 +02:00
|
|
|
)
|
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):
|
|
|
|
def __init__(self, participant, *args, **kwargs):
|
2018-01-16 16:22:52 +01:00
|
|
|
super().__init__(*args, **kwargs)
|
2019-01-07 14:59:20 +01:00
|
|
|
self.fields["reventes"].queryset = ReventeModelMultipleChoiceField(
|
|
|
|
own=True,
|
|
|
|
label="",
|
|
|
|
queryset=participant.original_shows.filter(soldTo__isnull=False)
|
2017-12-19 12:41:50 +01:00
|
|
|
.exclude(soldTo=participant)
|
2018-10-06 12:35:49 +02:00
|
|
|
.select_related(
|
|
|
|
"attribution__spectacle", "attribution__spectacle__location"
|
2019-01-07 14:59:20 +01:00
|
|
|
),
|
|
|
|
widget=forms.CheckboxSelectMultiple,
|
2017-02-21 23:49:29 +01:00
|
|
|
)
|
2019-01-07 14:59:20 +01:00
|
|
|
|