forked from DGNum/gestioCOF
Utilisation d'un formset pour l'inscription
- Changements mineurs dans `AdminEventForm` - Ajout d'une base pour le formset : `BaseEventRegistrationFormset` - Adaptation des vues de l'inscription et suppression d'une vue inutile.
This commit is contained in:
parent
19456756e4
commit
b60b9f4e17
3 changed files with 54 additions and 82 deletions
|
@ -8,6 +8,7 @@ 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
|
||||
from django.forms.formsets import BaseFormSet
|
||||
from django.db.models import Max
|
||||
|
||||
from gestioncof.models import CofProfile, EventCommentValue, \
|
||||
|
@ -263,17 +264,15 @@ STATUS_CHOICES = (('no', 'Non'),
|
|||
|
||||
|
||||
class AdminEventForm(forms.Form):
|
||||
status = forms.ChoiceField(label="Inscription",
|
||||
status = forms.ChoiceField(label="Inscription", initial="no",
|
||||
choices=STATUS_CHOICES, widget=RadioSelect)
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
event = kwargs.pop("event")
|
||||
self.event = event
|
||||
self.event = kwargs.pop("event")
|
||||
registration = kwargs.pop("current_registration", None)
|
||||
current_choices = \
|
||||
registration.options.all() if registration is not None\
|
||||
else []
|
||||
paid = kwargs.pop("paid", None)
|
||||
current_choices, paid = \
|
||||
(registration.options.all(), registration.paid) \
|
||||
if registration is not None else ([], None)
|
||||
if paid is True:
|
||||
kwargs["initial"] = {"status": "paid"}
|
||||
elif paid is False:
|
||||
|
@ -288,7 +287,7 @@ class AdminEventForm(forms.Form):
|
|||
else:
|
||||
choices[choice.event_option.id].append(choice.id)
|
||||
all_choices = choices
|
||||
for option in event.options.all():
|
||||
for option in self.event.options.all():
|
||||
choices = [(choice.id, choice.value)
|
||||
for choice in option.choices.all()]
|
||||
if option.multi_choices:
|
||||
|
@ -310,7 +309,7 @@ class AdminEventForm(forms.Form):
|
|||
initial=initial)
|
||||
field.option_id = option.id
|
||||
self.fields["option_%d" % option.id] = field
|
||||
for commentfield in event.commentfields.all():
|
||||
for commentfield in self.event.commentfields.all():
|
||||
initial = commentfield.default
|
||||
if registration is not None:
|
||||
try:
|
||||
|
@ -338,6 +337,21 @@ class AdminEventForm(forms.Form):
|
|||
yield (self.fields[name].comment_id, value)
|
||||
|
||||
|
||||
class BaseEventRegistrationFormset(BaseFormSet):
|
||||
def __init__(self, *args, **kwargs):
|
||||
self.events = kwargs.pop('events')
|
||||
self.current_registrations = kwargs.pop('current_registrations', None)
|
||||
self.extra = len(self.events)
|
||||
super(BaseEventRegistrationFormset, self).__init__(*args, **kwargs)
|
||||
|
||||
def _construct_form(self, index, **kwargs):
|
||||
kwargs['event'] = self.events[index]
|
||||
if self.current_registrations is not None:
|
||||
kwargs['current_registration'] = self.current_registrations[index]
|
||||
return super(BaseEventRegistrationFormset, self)._construct_form(
|
||||
index, **kwargs)
|
||||
|
||||
|
||||
class CalendarForm(forms.ModelForm):
|
||||
subscribe_to_events = forms.BooleanField(
|
||||
initial=True,
|
||||
|
|
|
@ -12,16 +12,15 @@
|
|||
<table>
|
||||
{{ user_form | bootstrap }}
|
||||
{{ profile_form | bootstrap }}
|
||||
{% if event_forms %}
|
||||
</table>
|
||||
{% for event_form in event_forms %}
|
||||
{{ event_formset.management_form }}
|
||||
{% for event_form in event_formset %}
|
||||
<hr />
|
||||
<h3>Inscription {{ event_form.event.title }} :</h2>
|
||||
<h3>Inscription {{ event_form.event.title }} :</h3>
|
||||
<table>
|
||||
{{ event_form | bootstrap }}
|
||||
{{ event_form | bootstrap }}
|
||||
</table>
|
||||
{% endfor %}
|
||||
{% endif %}
|
||||
{% if login_clipper or member %}
|
||||
<input type="hidden" name="user_exists" value="1" />
|
||||
{% endif %}
|
||||
|
|
|
@ -14,6 +14,7 @@ from django.http import Http404, HttpResponse
|
|||
from django.contrib.auth.decorators import login_required
|
||||
from django.contrib.auth.views import login as django_login_view
|
||||
from django.contrib.auth.models import User
|
||||
from django.forms.models import formset_factory
|
||||
import django.utils.six as six
|
||||
|
||||
from gestioncof.models import Survey, SurveyAnswer, SurveyQuestion, \
|
||||
|
@ -27,7 +28,8 @@ from gestioncof.models import CofProfile, Clipper
|
|||
from gestioncof.decorators import buro_required, cof_required
|
||||
from gestioncof.forms import UserProfileForm, EventStatusFilterForm, \
|
||||
SurveyForm, SurveyStatusFilterForm, RegistrationUserForm, \
|
||||
RegistrationProfileForm, AdminEventForm, EventForm, CalendarForm
|
||||
RegistrationProfileForm, AdminEventForm, EventForm, CalendarForm, \
|
||||
BaseEventRegistrationFormset
|
||||
|
||||
from bda.models import Tirage, Spectacle
|
||||
|
||||
|
@ -313,49 +315,12 @@ def registration_set_ro_fields(user_form, profile_form):
|
|||
profile_form.fields['login_clipper'].widget.attrs['readonly'] = True
|
||||
|
||||
|
||||
@buro_required
|
||||
def registration_form(request, login_clipper=None, username=None):
|
||||
member = None
|
||||
if login_clipper:
|
||||
clipper = get_object_or_404(Clipper, username=login_clipper)
|
||||
try: # check if the given user is already registered
|
||||
member = User.objects.filter(username=login_clipper).get()
|
||||
username = member.username
|
||||
login_clipper = None
|
||||
except User.DoesNotExist:
|
||||
# new user, but prefill
|
||||
user_form = RegistrationUserForm()
|
||||
profile_form = RegistrationProfileForm()
|
||||
user_form.fields['username'].initial = login_clipper
|
||||
user_form.fields['email'].initial = \
|
||||
login_clipper + "@clipper.ens.fr"
|
||||
profile_form.fields['login_clipper'].initial = login_clipper
|
||||
if clipper.fullname:
|
||||
bits = clipper.fullname.split(" ")
|
||||
user_form.fields['first_name'].initial = bits[0]
|
||||
if len(bits) > 1:
|
||||
user_form.fields['last_name'].initial = " ".join(bits[1:])
|
||||
registration_set_ro_fields(user_form, profile_form)
|
||||
if username:
|
||||
member = get_object_or_404(User, username=username)
|
||||
(profile, _) = CofProfile.objects.get_or_create(user=member)
|
||||
# already existing, prefill
|
||||
user_form = RegistrationUserForm(instance=member)
|
||||
profile_form = RegistrationProfileForm(instance=profile)
|
||||
registration_set_ro_fields(user_form, profile_form)
|
||||
elif not login_clipper:
|
||||
# new user
|
||||
user_form = RegistrationUserForm()
|
||||
profile_form = RegistrationProfileForm()
|
||||
return render(request, "registration_form.html",
|
||||
{"user_form": user_form, "profile_form": profile_form,
|
||||
"member": member, "login_clipper": login_clipper})
|
||||
|
||||
|
||||
@buro_required
|
||||
def registration_form2(request, login_clipper=None, username=None):
|
||||
events = Event.objects.filter(old=False).all()
|
||||
member = None
|
||||
EventFormset = formset_factory(AdminEventForm,
|
||||
BaseEventRegistrationFormset)
|
||||
if login_clipper:
|
||||
clipper = get_object_or_404(Clipper, username=login_clipper)
|
||||
try: # check if the given user is already registered
|
||||
|
@ -366,10 +331,10 @@ def registration_form2(request, login_clipper=None, username=None):
|
|||
# new user, but prefill
|
||||
user_form = RegistrationUserForm()
|
||||
profile_form = RegistrationProfileForm()
|
||||
event_forms = [AdminEventForm(event=event) for event in events]
|
||||
event_formset = EventFormset(events=events, prefix='events')
|
||||
user_form.fields['username'].initial = login_clipper
|
||||
user_form.fields['email'].initial = \
|
||||
login_clipper + "@clipper.ens.fr"
|
||||
user_form.fields['email'].initial = "%s@clipper.ens.fr" \
|
||||
% login_clipper
|
||||
profile_form.fields['login_clipper'].initial = login_clipper
|
||||
if clipper.fullname:
|
||||
bits = clipper.fullname.split(" ")
|
||||
|
@ -384,27 +349,25 @@ def registration_form2(request, login_clipper=None, username=None):
|
|||
user_form = RegistrationUserForm(instance=member)
|
||||
profile_form = RegistrationProfileForm(instance=profile)
|
||||
registration_set_ro_fields(user_form, profile_form)
|
||||
event_forms = []
|
||||
current_registrations = []
|
||||
for event in events:
|
||||
try:
|
||||
current_registration = EventRegistration.objects.get(
|
||||
user=member, event=event)
|
||||
form = AdminEventForm(
|
||||
event=event,
|
||||
current_registration=current_registration,
|
||||
paid=current_registration.paid)
|
||||
current_registrations.append(
|
||||
EventRegistration.objects.get(user=member, event=event))
|
||||
except EventRegistration.DoesNotExist:
|
||||
form = AdminEventForm(event=event)
|
||||
event_forms.append(form)
|
||||
current_registrations.append(None)
|
||||
event_formset = EventFormset(
|
||||
events=events, prefix='events',
|
||||
current_registrations=current_registrations)
|
||||
elif not login_clipper:
|
||||
# new user
|
||||
user_form = RegistrationUserForm()
|
||||
profile_form = RegistrationProfileForm()
|
||||
event_forms = [AdminEventForm(event=event) for event in events]
|
||||
event_formset = EventFormset(events=events, prefix='events')
|
||||
return render(request, "registration_form.html",
|
||||
{"user_form": user_form, "profile_form": profile_form,
|
||||
"member": member, "login_clipper": login_clipper,
|
||||
"event_forms": event_forms})
|
||||
"event_formset": event_formset})
|
||||
|
||||
|
||||
@buro_required
|
||||
|
@ -417,12 +380,13 @@ def registration(request):
|
|||
user_form = RegistrationUserForm(request_dict)
|
||||
profile_form = RegistrationProfileForm(request_dict)
|
||||
events = Event.objects.filter(old=False).all()
|
||||
event_forms = \
|
||||
[AdminEventForm(request_dict, event=event) for event in events]
|
||||
EventFormset = formset_factory(AdminEventForm,
|
||||
BaseEventRegistrationFormset)
|
||||
event_formset = EventFormset(events=events, data=request_dict,
|
||||
prefix='events')
|
||||
user_form.is_valid()
|
||||
profile_form.is_valid()
|
||||
for event_form in event_forms:
|
||||
event_form.is_valid()
|
||||
event_formset.is_valid()
|
||||
member = None
|
||||
login_clipper = None
|
||||
if "user_exists" in request_dict and request_dict["user_exists"]:
|
||||
|
@ -439,26 +403,21 @@ def registration(request):
|
|||
login_clipper = clipper.username
|
||||
except Clipper.DoesNotExist:
|
||||
pass
|
||||
for form in event_forms:
|
||||
if not form.is_valid():
|
||||
break
|
||||
if form.cleaned_data['status'] == 'no':
|
||||
continue
|
||||
all_choices = get_event_form_choices(form.event, form)
|
||||
if user_form.is_valid() and profile_form.is_valid() \
|
||||
and not any([not form.is_valid() for form in event_forms]):
|
||||
and event_formset.is_valid():
|
||||
member = user_form.save()
|
||||
(profile, _) = CofProfile.objects.get_or_create(user=member)
|
||||
was_cof = profile.is_cof
|
||||
request_dict["num"] = profile.num
|
||||
profile_form = RegistrationProfileForm(request_dict,
|
||||
instance=profile)
|
||||
profile_form.is_valid()
|
||||
profile_form.save()
|
||||
(profile, _) = CofProfile.objects.get_or_create(user=member)
|
||||
if profile.is_cof and not was_cof:
|
||||
send_custom_mail(member, "bienvenue")
|
||||
for form in event_forms:
|
||||
for form in event_formset:
|
||||
if 'status' not in form.cleaned_data:
|
||||
form.cleaned_data['status'] = 'no'
|
||||
if form.cleaned_data['status'] == 'no':
|
||||
try:
|
||||
current_registration = EventRegistration.objects.get(
|
||||
|
@ -494,7 +453,7 @@ def registration(request):
|
|||
"profile_form": profile_form,
|
||||
"member": member,
|
||||
"login_clipper": login_clipper,
|
||||
"event_forms": event_forms})
|
||||
"event_formset": event_formset})
|
||||
else:
|
||||
return render(request, "registration.html")
|
||||
|
||||
|
|
Loading…
Reference in a new issue