Petits changements

- Ajout d'un FIXME dans `gestioncof/forms.py` au niveua de la surcharge de
  la méthode privée `_construct_form` : ce trick ne sera plus nécessaire à
  partir de Django 1.9
- Utilisation correcte des `form.is_valid` dans `gestioncof.views.registration`
This commit is contained in:
Martin Pépin 2016-08-14 12:10:50 +02:00
parent 23ac3b7222
commit 12a4b8efa7

View file

@ -379,46 +379,49 @@ def registration_form2(request, login_clipper=None, username=None):
def registration(request): def registration(request):
if request.POST: if request.POST:
request_dict = request.POST.copy() request_dict = request.POST.copy()
# num ne peut pas être définit manuellement member = None
login_clipper = None
success = False
events = Event.objects.filter(old=False).all()
# num ne peut pas être défini manuellement
if "num" in request_dict: if "num" in request_dict:
del request_dict["num"] del request_dict["num"]
success = False
# -----
# Remplissage des formulaires
# -----
user_form = RegistrationUserForm(request_dict) user_form = RegistrationUserForm(request_dict)
profile_form = RegistrationProfileForm(request_dict)
events = Event.objects.filter(old=False).all()
EventFormset = formset_factory(AdminEventForm, EventFormset = formset_factory(AdminEventForm,
BaseEventRegistrationFormset) BaseEventRegistrationFormset)
event_formset = EventFormset(events=events, data=request_dict, event_formset = EventFormset(events=events, data=request_dict,
prefix='events') prefix='events')
user_form.is_valid()
profile_form.is_valid()
event_formset.is_valid()
member = None
login_clipper = None
if "user_exists" in request_dict and request_dict["user_exists"]: if "user_exists" in request_dict and request_dict["user_exists"]:
username = request_dict["username"] username = request_dict["username"]
try: try:
member = User.objects.filter(username=username).get() member = User.objects.get(username=username)
(profile, _) = CofProfile.objects.get_or_create(user=member)
user_form = RegistrationUserForm(request_dict, instance=member) user_form = RegistrationUserForm(request_dict, instance=member)
profile_form = RegistrationProfileForm(request_dict,
instance=profile)
except User.DoesNotExist: except User.DoesNotExist:
try: try:
clipper = Clipper.objects.filter(username=username).get() clipper = Clipper.objects.get(username=username)
login_clipper = clipper.username login_clipper = clipper.username
except Clipper.DoesNotExist: except Clipper.DoesNotExist:
pass pass
if user_form.is_valid() and profile_form.is_valid() \
and event_formset.is_valid(): # -----
# Validation des formulaires
# -----
if user_form.is_valid():
member = user_form.save() member = user_form.save()
(profile, _) = CofProfile.objects.get_or_create(user=member) profile, _ = CofProfile.objects.get_or_create(user=member)
was_cof = profile.is_cof was_cof = profile.is_cof
request_dict["num"] = profile.num request_dict["num"] = profile.num
# Maintenant on remplit le formulaire de profil
profile_form = RegistrationProfileForm(request_dict, profile_form = RegistrationProfileForm(request_dict,
instance=profile) instance=profile)
profile_form.save() if profile_form.is_valid() and event_formset.is_valid():
(profile, _) = CofProfile.objects.get_or_create(user=member) profile = profile_form.save()
if profile.is_cof and not was_cof: if profile.is_cof and not was_cof:
send_custom_mail(member, "bienvenue") send_custom_mail(member, "bienvenue")
for form in event_formset: for form in event_formset:
@ -426,16 +429,16 @@ def registration(request):
form.cleaned_data['status'] = 'no' form.cleaned_data['status'] = 'no'
if form.cleaned_data['status'] == 'no': if form.cleaned_data['status'] == 'no':
try: try:
current_registration = EventRegistration.objects.get( current_registration = EventRegistration.objects \
user=member, event=form.event) .get(user=member, event=form.event)
current_registration.delete() current_registration.delete()
except EventRegistration.DoesNotExist: except EventRegistration.DoesNotExist:
pass pass
continue continue
all_choices = get_event_form_choices(form.event, form) all_choices = get_event_form_choices(form.event, form)
(current_registration, created_reg) = \ (current_registration, created_reg) = \
EventRegistration.objects.get_or_create(user=member, EventRegistration.objects.get_or_create(
event=form.event) user=member, event=form.event)
update_event_form_comments(form.event, form, update_event_form_comments(form.event, form,
current_registration) current_registration)
current_registration.options = all_choices current_registration.options = all_choices
@ -443,15 +446,16 @@ def registration(request):
(form.cleaned_data['status'] == 'paid') (form.cleaned_data['status'] == 'paid')
current_registration.save() current_registration.save()
if form.event.title == "Mega 15" and created_reg: if form.event.title == "Mega 15" and created_reg:
field = EventCommentField.objects.get(event=form.event, field = EventCommentField.objects.get(
name="Commentaires") event=form.event, name="Commentaires")
try: try:
comments = EventCommentValue.objects.get( comments = EventCommentValue.objects.get(
commentfield=field, commentfield=field,
registration=current_registration).content registration=current_registration).content
except EventCommentValue.DoesNotExist: except EventCommentValue.DoesNotExist:
comments = field.default comments = field.default
send_custom_mail(member, "mega", {"remarques": comments}) send_custom_mail(member, "mega",
{"remarques": comments})
success = True success = True
return render(request, "registration_post.html", return render(request, "registration_post.html",
{"success": success, {"success": success,