2016-07-15 00:02:56 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2016-05-27 17:52:02 +02:00
|
|
|
|
2016-07-15 00:02:56 +02:00
|
|
|
from __future__ import division
|
|
|
|
from __future__ import print_function
|
2016-07-08 23:04:34 +02:00
|
|
|
from __future__ import unicode_literals
|
2016-05-27 17:52:02 +02:00
|
|
|
|
|
|
|
from django import forms
|
|
|
|
from django.utils.translation import ugettext_lazy as _
|
|
|
|
from django.contrib.auth.models import User
|
|
|
|
from django.forms.widgets import RadioSelect, CheckboxSelectMultiple
|
2016-06-10 23:59:41 +02:00
|
|
|
from django.db.models import Max
|
2016-05-27 17:52:02 +02:00
|
|
|
|
|
|
|
from gestioncof.models import CofProfile, EventCommentValue
|
|
|
|
from gestioncof.widgets import TriStateCheckbox
|
2016-06-10 23:59:41 +02:00
|
|
|
from gestioncof.shared import lock_table, unlock_table
|
2016-05-27 17:52:02 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
class EventForm(forms.Form):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
event = kwargs.pop("event")
|
|
|
|
self.event = event
|
|
|
|
current_choices = kwargs.pop("current_choices", None)
|
|
|
|
super(EventForm, self).__init__(*args, **kwargs)
|
|
|
|
choices = {}
|
|
|
|
if current_choices:
|
|
|
|
for choice in current_choices.all():
|
|
|
|
if choice.event_option.id not in choices:
|
|
|
|
choices[choice.event_option.id] = [choice.id]
|
|
|
|
else:
|
|
|
|
choices[choice.event_option.id].append(choice.id)
|
|
|
|
all_choices = choices
|
|
|
|
for option in event.options.all():
|
2016-07-09 22:31:56 +02:00
|
|
|
choices = [(choice.id, choice.value)
|
|
|
|
for choice in option.choices.all()]
|
2016-05-27 17:52:02 +02:00
|
|
|
if option.multi_choices:
|
2016-07-09 22:31:56 +02:00
|
|
|
initial = [] if option.id not in all_choices \
|
|
|
|
else all_choices[option.id]
|
|
|
|
field = forms.MultipleChoiceField(
|
|
|
|
label=option.name,
|
|
|
|
choices=choices,
|
|
|
|
widget=CheckboxSelectMultiple,
|
|
|
|
required=False,
|
|
|
|
initial=initial)
|
2016-05-27 17:52:02 +02:00
|
|
|
else:
|
2016-07-09 22:31:56 +02:00
|
|
|
initial = None if option.id not in all_choices \
|
|
|
|
else all_choices[option.id][0]
|
2016-07-09 21:19:37 +02:00
|
|
|
field = forms.ChoiceField(label=option.name,
|
|
|
|
choices=choices,
|
|
|
|
widget=RadioSelect,
|
|
|
|
required=False,
|
|
|
|
initial=initial)
|
2016-05-27 17:52:02 +02:00
|
|
|
field.option_id = option.id
|
|
|
|
self.fields["option_%d" % option.id] = field
|
|
|
|
|
|
|
|
def choices(self):
|
|
|
|
for name, value in self.cleaned_data.items():
|
|
|
|
if name.startswith('option_'):
|
|
|
|
yield (self.fields[name].option_id, value)
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
class SurveyForm(forms.Form):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
survey = kwargs.pop("survey")
|
|
|
|
current_answers = kwargs.pop("current_answers", None)
|
|
|
|
super(SurveyForm, self).__init__(*args, **kwargs)
|
|
|
|
answers = {}
|
|
|
|
if current_answers:
|
|
|
|
for answer in current_answers.all():
|
|
|
|
if answer.survey_question.id not in answers:
|
|
|
|
answers[answer.survey_question.id] = [answer.id]
|
|
|
|
else:
|
|
|
|
answers[answer.survey_question.id].append(answer.id)
|
|
|
|
for question in survey.questions.all():
|
2016-07-09 22:31:56 +02:00
|
|
|
choices = [(answer.id, answer.answer)
|
|
|
|
for answer in question.answers.all()]
|
2016-05-27 17:52:02 +02:00
|
|
|
if question.multi_answers:
|
2016-07-09 22:31:56 +02:00
|
|
|
initial = [] if question.id not in answers\
|
|
|
|
else answers[question.id]
|
|
|
|
field = forms.MultipleChoiceField(
|
|
|
|
label=question.question,
|
|
|
|
choices=choices,
|
|
|
|
widget=CheckboxSelectMultiple,
|
|
|
|
required=False,
|
|
|
|
initial=initial)
|
2016-05-27 17:52:02 +02:00
|
|
|
else:
|
2016-07-09 22:31:56 +02:00
|
|
|
initial = None if question.id not in answers\
|
|
|
|
else answers[question.id][0]
|
2016-07-09 21:19:37 +02:00
|
|
|
field = forms.ChoiceField(label=question.question,
|
|
|
|
choices=choices,
|
|
|
|
widget=RadioSelect,
|
|
|
|
required=False,
|
|
|
|
initial=initial)
|
2016-05-27 17:52:02 +02:00
|
|
|
field.question_id = question.id
|
|
|
|
self.fields["question_%d" % question.id] = field
|
|
|
|
|
2016-06-13 19:08:14 +02:00
|
|
|
def answers(self):
|
|
|
|
for name, value in self.cleaned_data.items():
|
|
|
|
if name.startswith('question_'):
|
|
|
|
yield (self.fields[name].question_id, value)
|
2016-07-09 21:19:37 +02:00
|
|
|
|
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
class SurveyStatusFilterForm(forms.Form):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
survey = kwargs.pop("survey")
|
|
|
|
super(SurveyStatusFilterForm, self).__init__(*args, **kwargs)
|
|
|
|
for question in survey.questions.all():
|
|
|
|
for answer in question.answers.all():
|
|
|
|
name = "question_%d_answer_%d" % (question.id, answer.id)
|
2016-07-09 23:59:25 +02:00
|
|
|
if self.is_bound \
|
|
|
|
and self.data.get(self.add_prefix(name), None):
|
2016-05-27 17:52:02 +02:00
|
|
|
initial = self.data.get(self.add_prefix(name), None)
|
|
|
|
else:
|
|
|
|
initial = "none"
|
2016-07-09 22:31:56 +02:00
|
|
|
field = forms.ChoiceField(
|
2016-07-10 00:26:02 +02:00
|
|
|
label="%s : %s" % (question.question, answer.answer),
|
2016-07-09 22:31:56 +02:00
|
|
|
choices=[("yes", "yes"), ("no", "no"), ("none", "none")],
|
|
|
|
widget=TriStateCheckbox,
|
|
|
|
required=False,
|
|
|
|
initial=initial)
|
2016-05-27 17:52:02 +02:00
|
|
|
field.question_id = question.id
|
|
|
|
field.answer_id = answer.id
|
|
|
|
self.fields[name] = field
|
|
|
|
|
|
|
|
def filters(self):
|
|
|
|
for name, value in self.cleaned_data.items():
|
|
|
|
if name.startswith('question_'):
|
2016-07-09 22:31:56 +02:00
|
|
|
yield (self.fields[name].question_id,
|
|
|
|
self.fields[name].answer_id, value)
|
2016-05-27 17:52:02 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
class EventStatusFilterForm(forms.Form):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
event = kwargs.pop("event")
|
|
|
|
super(EventStatusFilterForm, self).__init__(*args, **kwargs)
|
|
|
|
for option in event.options.all():
|
|
|
|
for choice in option.choices.all():
|
|
|
|
name = "option_%d_choice_%d" % (option.id, choice.id)
|
2016-07-09 23:59:25 +02:00
|
|
|
if self.is_bound \
|
|
|
|
and self.data.get(self.add_prefix(name), None):
|
2016-05-27 17:52:02 +02:00
|
|
|
initial = self.data.get(self.add_prefix(name), None)
|
|
|
|
else:
|
|
|
|
initial = "none"
|
2016-07-09 22:31:56 +02:00
|
|
|
field = forms.ChoiceField(
|
|
|
|
label="%s : %s" % (option.name, choice.value),
|
|
|
|
choices=[("yes", "yes"), ("no", "no"), ("none", "none")],
|
|
|
|
widget=TriStateCheckbox,
|
|
|
|
required=False,
|
|
|
|
initial=initial)
|
2016-05-27 17:52:02 +02:00
|
|
|
field.option_id = option.id
|
|
|
|
field.choice_id = choice.id
|
|
|
|
self.fields[name] = field
|
|
|
|
# has_paid
|
|
|
|
name = "event_has_paid"
|
|
|
|
if self.is_bound and self.data.get(self.add_prefix(name), None):
|
|
|
|
initial = self.data.get(self.add_prefix(name), None)
|
|
|
|
else:
|
|
|
|
initial = "none"
|
2016-07-09 21:19:37 +02:00
|
|
|
field = forms.ChoiceField(label="Événement payé",
|
2016-07-09 22:31:56 +02:00
|
|
|
choices=[("yes", "yes"), ("no", "no"),
|
|
|
|
("none", "none")],
|
2016-07-09 21:19:37 +02:00
|
|
|
widget=TriStateCheckbox,
|
|
|
|
required=False,
|
|
|
|
initial=initial)
|
2016-05-27 17:52:02 +02:00
|
|
|
self.fields[name] = field
|
|
|
|
|
|
|
|
def filters(self):
|
|
|
|
for name, value in self.cleaned_data.items():
|
|
|
|
if name.startswith('option_'):
|
2016-07-09 22:31:56 +02:00
|
|
|
yield (self.fields[name].option_id,
|
|
|
|
self.fields[name].choice_id, value)
|
2016-05-27 17:52:02 +02:00
|
|
|
elif name == "event_has_paid":
|
|
|
|
yield ("has_paid", None, value)
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
class UserProfileForm(forms.ModelForm):
|
2016-07-08 23:04:34 +02:00
|
|
|
first_name = forms.CharField(label=_('Prénom'), max_length=30)
|
|
|
|
last_name = forms.CharField(label=_('Nom'), max_length=30)
|
2016-05-27 17:52:02 +02:00
|
|
|
|
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
super(UserProfileForm, self).__init__(*args, **kw)
|
|
|
|
self.fields['first_name'].initial = self.instance.user.first_name
|
|
|
|
self.fields['last_name'].initial = self.instance.user.last_name
|
|
|
|
self.fields.keyOrder = [
|
|
|
|
'first_name',
|
|
|
|
'last_name',
|
|
|
|
'phone',
|
|
|
|
'mailing_cof',
|
|
|
|
'mailing_bda',
|
|
|
|
'mailing_bda_revente',
|
|
|
|
]
|
|
|
|
|
|
|
|
def save(self, *args, **kw):
|
|
|
|
super(UserProfileForm, self).save(*args, **kw)
|
|
|
|
self.instance.user.first_name = self.cleaned_data.get('first_name')
|
|
|
|
self.instance.user.last_name = self.cleaned_data.get('last_name')
|
|
|
|
self.instance.user.save()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = CofProfile
|
2016-07-09 22:31:56 +02:00
|
|
|
fields = ("phone", "mailing_cof", "mailing_bda",
|
|
|
|
"mailing_bda_revente", )
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
|
|
|
|
class RegistrationUserForm(forms.ModelForm):
|
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
super(RegistrationUserForm, self).__init__(*args, **kw)
|
|
|
|
self.fields['username'].help_text = ""
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = User
|
|
|
|
fields = ("username", "first_name", "last_name", "email")
|
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
class RegistrationProfileForm(forms.ModelForm):
|
|
|
|
def __init__(self, *args, **kw):
|
|
|
|
super(RegistrationProfileForm, self).__init__(*args, **kw)
|
|
|
|
self.fields['mailing_cof'].initial = True
|
|
|
|
self.fields['mailing_bda'].initial = True
|
|
|
|
self.fields['mailing_bda_revente'].initial = True
|
|
|
|
self.fields['num'].widget.attrs['readonly'] = True
|
|
|
|
|
|
|
|
self.fields.keyOrder = [
|
|
|
|
'login_clipper',
|
|
|
|
'phone',
|
|
|
|
'occupation',
|
|
|
|
'departement',
|
|
|
|
'is_cof',
|
|
|
|
'num',
|
|
|
|
'type_cotiz',
|
|
|
|
'mailing_cof',
|
|
|
|
'mailing_bda',
|
|
|
|
'mailing_bda_revente',
|
|
|
|
'comments'
|
|
|
|
]
|
|
|
|
|
|
|
|
def save(self, *args, **kw):
|
|
|
|
instance = super(RegistrationProfileForm, self).save(*args, **kw)
|
|
|
|
if instance.is_cof and not instance.num:
|
|
|
|
# Generate new number
|
|
|
|
try:
|
|
|
|
lock_table(CofProfile)
|
|
|
|
aggregate = CofProfile.objects.aggregate(Max('num'))
|
|
|
|
instance.num = aggregate['num__max'] + 1
|
|
|
|
instance.save()
|
|
|
|
self.cleaned_data['num'] = instance.num
|
|
|
|
self.data['num'] = instance.num
|
|
|
|
finally:
|
|
|
|
unlock_table(CofProfile)
|
|
|
|
return instance
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = CofProfile
|
2016-07-09 22:31:56 +02:00
|
|
|
fields = ("login_clipper", "num", "phone", "occupation",
|
|
|
|
"departement", "is_cof", "type_cotiz", "mailing_cof",
|
|
|
|
"mailing_bda", "mailing_bda_revente", "comments")
|
2016-05-27 17:52:02 +02:00
|
|
|
|
2016-07-09 21:19:37 +02:00
|
|
|
STATUS_CHOICES = (('no', 'Non'),
|
|
|
|
('wait', 'Oui mais attente paiement'),
|
|
|
|
('paid', 'Oui payé'),)
|
|
|
|
|
2016-05-27 17:52:02 +02:00
|
|
|
|
|
|
|
class AdminEventForm(forms.Form):
|
2016-07-09 22:31:56 +02:00
|
|
|
status = forms.ChoiceField(label="Inscription",
|
|
|
|
choices=STATUS_CHOICES, widget=RadioSelect)
|
2016-05-27 17:52:02 +02:00
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
event = kwargs.pop("event")
|
|
|
|
self.event = event
|
|
|
|
registration = kwargs.pop("current_registration", None)
|
2016-07-09 22:31:56 +02:00
|
|
|
current_choices = \
|
|
|
|
registration.options.all() if registration is not None\
|
|
|
|
else []
|
2016-05-27 17:52:02 +02:00
|
|
|
paid = kwargs.pop("paid", None)
|
2016-07-09 21:19:37 +02:00
|
|
|
if paid is True:
|
|
|
|
kwargs["initial"] = {"status": "paid"}
|
|
|
|
elif paid is False:
|
|
|
|
kwargs["initial"] = {"status": "wait"}
|
2016-05-27 17:52:02 +02:00
|
|
|
else:
|
2016-07-09 21:19:37 +02:00
|
|
|
kwargs["initial"] = {"status": "no"}
|
2016-05-27 17:52:02 +02:00
|
|
|
super(AdminEventForm, self).__init__(*args, **kwargs)
|
|
|
|
choices = {}
|
|
|
|
for choice in current_choices:
|
|
|
|
if choice.event_option.id not in choices:
|
|
|
|
choices[choice.event_option.id] = [choice.id]
|
|
|
|
else:
|
|
|
|
choices[choice.event_option.id].append(choice.id)
|
|
|
|
all_choices = choices
|
|
|
|
for option in event.options.all():
|
2016-07-09 22:31:56 +02:00
|
|
|
choices = [(choice.id, choice.value)
|
|
|
|
for choice in option.choices.all()]
|
2016-05-27 17:52:02 +02:00
|
|
|
if option.multi_choices:
|
2016-07-09 22:31:56 +02:00
|
|
|
initial = [] if option.id not in all_choices\
|
|
|
|
else all_choices[option.id]
|
|
|
|
field = forms.MultipleChoiceField(
|
|
|
|
label=option.name,
|
|
|
|
choices=choices,
|
|
|
|
widget=CheckboxSelectMultiple,
|
|
|
|
required=False,
|
|
|
|
initial=initial)
|
2016-05-27 17:52:02 +02:00
|
|
|
else:
|
2016-07-09 22:31:56 +02:00
|
|
|
initial = None if option.id not in all_choices\
|
|
|
|
else all_choices[option.id][0]
|
2016-07-09 21:19:37 +02:00
|
|
|
field = forms.ChoiceField(label=option.name,
|
|
|
|
choices=choices,
|
|
|
|
widget=RadioSelect,
|
|
|
|
required=False,
|
|
|
|
initial=initial)
|
2016-05-27 17:52:02 +02:00
|
|
|
field.option_id = option.id
|
|
|
|
self.fields["option_%d" % option.id] = field
|
|
|
|
for commentfield in event.commentfields.all():
|
|
|
|
initial = commentfield.default
|
|
|
|
if registration is not None:
|
|
|
|
try:
|
2016-07-09 23:59:25 +02:00
|
|
|
initial = registration.comments \
|
|
|
|
.get(commentfield=commentfield).content
|
2016-05-27 17:52:02 +02:00
|
|
|
except EventCommentValue.DoesNotExist:
|
|
|
|
pass
|
2016-07-09 22:31:56 +02:00
|
|
|
widget = forms.Textarea if commentfield.fieldtype == "text" \
|
|
|
|
else forms.TextInput
|
2016-07-09 21:19:37 +02:00
|
|
|
field = forms.CharField(label=commentfield.name,
|
|
|
|
widget=widget,
|
|
|
|
required=False,
|
|
|
|
initial=initial)
|
2016-05-27 17:52:02 +02:00
|
|
|
field.comment_id = commentfield.id
|
|
|
|
self.fields["comment_%d" % commentfield.id] = field
|
|
|
|
|
|
|
|
def choices(self):
|
|
|
|
for name, value in self.cleaned_data.items():
|
|
|
|
if name.startswith('option_'):
|
|
|
|
yield (self.fields[name].option_id, value)
|
|
|
|
|
|
|
|
def comments(self):
|
|
|
|
for name, value in self.cleaned_data.items():
|
|
|
|
if name.startswith('comment_'):
|
|
|
|
yield (self.fields[name].comment_id, value)
|