Remove the old user registration material
This commit is contained in:
parent
8a346bf834
commit
f93a68b942
3 changed files with 17 additions and 314 deletions
88
cof/forms.py
88
cof/forms.py
|
@ -1,25 +1,12 @@
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
from __future__ import division
|
|
||||||
from __future__ import print_function
|
|
||||||
from __future__ import unicode_literals
|
|
||||||
|
|
||||||
from django import forms
|
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.widgets import RadioSelect, CheckboxSelectMultiple
|
||||||
from django.forms.formsets import BaseFormSet, formset_factory
|
from django.forms.formsets import BaseFormSet, formset_factory
|
||||||
from django.db.models import Max
|
|
||||||
from django.core.validators import MinLengthValidator
|
from bda.models import Spectacle
|
||||||
|
|
||||||
from .models import CofProfile, EventCommentValue, CalendarSubscription
|
from .models import CofProfile, EventCommentValue, CalendarSubscription
|
||||||
from .widgets import TriStateCheckbox
|
from .widgets import TriStateCheckbox
|
||||||
|
|
||||||
from gestion.models import Profile
|
|
||||||
from gestion.shared import lock_table, unlock_table
|
|
||||||
|
|
||||||
from bda.models import Spectacle
|
|
||||||
|
|
||||||
|
|
||||||
class EventForm(forms.Form):
|
class EventForm(forms.Form):
|
||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
|
@ -178,77 +165,6 @@ class EventStatusFilterForm(forms.Form):
|
||||||
yield ("has_paid", None, value)
|
yield ("has_paid", None, value)
|
||||||
|
|
||||||
|
|
||||||
class RegistrationUserForm(forms.ModelForm):
|
|
||||||
def force_long_username(self):
|
|
||||||
self.fields['username'].validators = [MinLengthValidator(9)]
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = User
|
|
||||||
fields = ["username", "first_name", "last_name", "email"]
|
|
||||||
|
|
||||||
|
|
||||||
class RegistrationPassUserForm(RegistrationUserForm):
|
|
||||||
"""
|
|
||||||
Formulaire pour changer le mot de passe d'un utilisateur.
|
|
||||||
"""
|
|
||||||
password1 = forms.CharField(label=_('Mot de passe'),
|
|
||||||
widget=forms.PasswordInput)
|
|
||||||
password2 = forms.CharField(label=_('Confirmation du mot de passe'),
|
|
||||||
widget=forms.PasswordInput)
|
|
||||||
|
|
||||||
def clean_password2(self):
|
|
||||||
pass1 = self.cleaned_data['password1']
|
|
||||||
pass2 = self.cleaned_data['password2']
|
|
||||||
if pass1 and pass2:
|
|
||||||
if pass1 != pass2:
|
|
||||||
raise forms.ValidationError(_('Mots de passe non identiques.'))
|
|
||||||
return pass2
|
|
||||||
|
|
||||||
def save(self, commit=True, *args, **kwargs):
|
|
||||||
user = super(RegistrationPassUserForm, self).save(commit, *args,
|
|
||||||
**kwargs)
|
|
||||||
user.set_password(self.cleaned_data['password2'])
|
|
||||||
if commit:
|
|
||||||
user.save()
|
|
||||||
return user
|
|
||||||
|
|
||||||
|
|
||||||
class RegistrationCofProfileForm(forms.ModelForm):
|
|
||||||
def __init__(self, *args, **kwargs):
|
|
||||||
super(RegistrationCofProfileForm, self).__init__(*args, **kwargs)
|
|
||||||
self.fields['num'].widget.attrs['readonly'] = True
|
|
||||||
|
|
||||||
class Meta:
|
|
||||||
model = CofProfile
|
|
||||||
fields = [
|
|
||||||
"num", "type_cotiz",
|
|
||||||
"mailing", "mailing_bda", "mailing_bda_revente",
|
|
||||||
]
|
|
||||||
|
|
||||||
def save(self, *args, **kw):
|
|
||||||
instance = super(RegistrationCofProfileForm, 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 RegistrationProfileForm(forms.ModelForm):
|
|
||||||
class Meta:
|
|
||||||
model = Profile
|
|
||||||
fields = [
|
|
||||||
"login_clipper", "phone", "occupation", "departement", "comments"
|
|
||||||
]
|
|
||||||
|
|
||||||
|
|
||||||
STATUS_CHOICES = (('no', 'Non'),
|
STATUS_CHOICES = (('no', 'Non'),
|
||||||
('wait', 'Oui mais attente paiement'),
|
('wait', 'Oui mais attente paiement'),
|
||||||
('paid', 'Oui payé'),)
|
('paid', 'Oui payé'),)
|
||||||
|
|
233
cof/views.py
233
cof/views.py
|
@ -4,34 +4,27 @@ import unicodecsv
|
||||||
import uuid
|
import uuid
|
||||||
from datetime import timedelta
|
from datetime import timedelta
|
||||||
from icalendar import Calendar, Event as Vevent
|
from icalendar import Calendar, Event as Vevent
|
||||||
from custommail.shortcuts import send_custom_mail
|
|
||||||
|
|
||||||
from django.shortcuts import get_object_or_404, render
|
|
||||||
from django.http import Http404, HttpResponse
|
|
||||||
from django.contrib.auth.decorators import login_required
|
|
||||||
from django.contrib.auth.models import User
|
|
||||||
from django.contrib.sites.models import Site
|
|
||||||
from django.utils import timezone
|
|
||||||
from django.contrib import messages
|
from django.contrib import messages
|
||||||
|
from django.contrib.auth.decorators import login_required
|
||||||
|
from django.contrib.sites.models import Site
|
||||||
|
from django.http import Http404, HttpResponse
|
||||||
|
from django.shortcuts import get_object_or_404, render
|
||||||
|
from django.utils import timezone
|
||||||
import django.utils.six as six
|
import django.utils.six as six
|
||||||
|
|
||||||
from .models import Survey, SurveyAnswer, SurveyQuestion, \
|
|
||||||
SurveyQuestionAnswer
|
|
||||||
from .models import Event, EventRegistration, EventOption, \
|
|
||||||
EventOptionChoice
|
|
||||||
from .models import EventCommentField, EventCommentValue, \
|
|
||||||
CalendarSubscription
|
|
||||||
from .models import CofProfile
|
|
||||||
from .decorators import buro_required, cof_required
|
|
||||||
from .forms import (
|
|
||||||
EventStatusFilterForm, SurveyForm, SurveyStatusFilterForm,
|
|
||||||
RegistrationUserForm, RegistrationProfileForm, RegistrationCofProfileForm,
|
|
||||||
EventForm, CalendarForm, EventFormset, RegistrationPassUserForm
|
|
||||||
)
|
|
||||||
|
|
||||||
from bda.models import Tirage, Spectacle
|
from bda.models import Tirage, Spectacle
|
||||||
|
|
||||||
from gestion.models import Profile
|
from .decorators import buro_required, cof_required
|
||||||
|
from .forms import (
|
||||||
|
EventStatusFilterForm, SurveyForm, SurveyStatusFilterForm, EventForm,
|
||||||
|
CalendarForm
|
||||||
|
)
|
||||||
|
from .models import (
|
||||||
|
Survey, SurveyAnswer, SurveyQuestion, SurveyQuestionAnswer, Event,
|
||||||
|
EventRegistration, EventOption, EventOptionChoice, EventCommentField,
|
||||||
|
EventCommentValue, CalendarSubscription, CofProfile
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
@login_required
|
@login_required
|
||||||
|
@ -273,202 +266,6 @@ def survey_status(request, survey_id):
|
||||||
"form": form})
|
"form": form})
|
||||||
|
|
||||||
|
|
||||||
def registration_set_ro_fields(user_form, profile_form):
|
|
||||||
user_form.fields['username'].widget.attrs['readonly'] = True
|
|
||||||
profile_form.fields['login_clipper'].widget.attrs['readonly'] = True
|
|
||||||
|
|
||||||
|
|
||||||
@buro_required
|
|
||||||
def registration_form2(request, login_clipper=None, username=None,
|
|
||||||
fullname=None):
|
|
||||||
events = Event.objects.filter(old=False).all()
|
|
||||||
member = None
|
|
||||||
if login_clipper:
|
|
||||||
try: # check if the given user is already registered
|
|
||||||
member = User.objects.get(username=login_clipper)
|
|
||||||
username = member.username
|
|
||||||
login_clipper = None
|
|
||||||
except User.DoesNotExist:
|
|
||||||
# new user, but prefill
|
|
||||||
# user
|
|
||||||
user_form = RegistrationUserForm(initial={
|
|
||||||
'username': login_clipper,
|
|
||||||
'email': "%s@clipper.ens.fr" % login_clipper})
|
|
||||||
if fullname:
|
|
||||||
bits = fullname.split(" ")
|
|
||||||
user_form.fields['first_name'].initial = bits[0]
|
|
||||||
if len(bits) > 1:
|
|
||||||
user_form.fields['last_name'].initial = " ".join(bits[1:])
|
|
||||||
# profile
|
|
||||||
profile_form = RegistrationProfileForm(initial={
|
|
||||||
'login_clipper': login_clipper})
|
|
||||||
# COF - profile
|
|
||||||
cofprofile_form = RegistrationCofProfileForm()
|
|
||||||
registration_set_ro_fields(user_form, profile_form)
|
|
||||||
# events
|
|
||||||
event_formset = EventFormset(events=events, prefix='events')
|
|
||||||
if username:
|
|
||||||
member = get_object_or_404(User, username=username)
|
|
||||||
(profile, _) = Profile.objects.get_or_create(user=member)
|
|
||||||
(cofprofile, _) = CofProfile.objects.get_or_create(profile=profile)
|
|
||||||
# already existing, prefill
|
|
||||||
user_form = RegistrationUserForm(instance=member)
|
|
||||||
profile_form = RegistrationProfileForm(instance=profile)
|
|
||||||
cofprofile_form = RegistrationCofProfileForm(instance=cofprofile)
|
|
||||||
registration_set_ro_fields(user_form, profile_form)
|
|
||||||
# events
|
|
||||||
current_registrations = []
|
|
||||||
for event in events:
|
|
||||||
try:
|
|
||||||
current_registrations.append(
|
|
||||||
EventRegistration.objects.get(user=member, event=event))
|
|
||||||
except EventRegistration.DoesNotExist:
|
|
||||||
current_registrations.append(None)
|
|
||||||
event_formset = EventFormset(
|
|
||||||
events=events, prefix='events',
|
|
||||||
current_registrations=current_registrations)
|
|
||||||
elif not login_clipper:
|
|
||||||
# new user
|
|
||||||
user_form = RegistrationPassUserForm()
|
|
||||||
user_form.force_long_username()
|
|
||||||
profile_form = RegistrationProfileForm()
|
|
||||||
cofprofile_form = RegistrationCofProfileForm()
|
|
||||||
event_formset = EventFormset(events=events, prefix='events')
|
|
||||||
return render(request, "cof/registration_form.html",
|
|
||||||
{"member": member, "login_clipper": login_clipper,
|
|
||||||
"user_form": user_form,
|
|
||||||
"profile_form": profile_form,
|
|
||||||
"cofprofile_form": cofprofile_form,
|
|
||||||
"event_formset": event_formset})
|
|
||||||
|
|
||||||
|
|
||||||
@buro_required
|
|
||||||
def registration(request):
|
|
||||||
if request.POST:
|
|
||||||
request_dict = request.POST.copy()
|
|
||||||
# num ne peut pas être défini manuellement
|
|
||||||
if "num" in request_dict:
|
|
||||||
del request_dict["num"]
|
|
||||||
member = None
|
|
||||||
login_clipper = None
|
|
||||||
success = False
|
|
||||||
|
|
||||||
# -----
|
|
||||||
# Remplissage des formulaires
|
|
||||||
# -----
|
|
||||||
|
|
||||||
if 'password1' in request_dict or 'password2' in request_dict:
|
|
||||||
user_form = RegistrationPassUserForm(request_dict)
|
|
||||||
else:
|
|
||||||
user_form = RegistrationUserForm(request_dict)
|
|
||||||
profile_form = RegistrationProfileForm(request_dict)
|
|
||||||
cofprofile_form = RegistrationCofProfileForm(request_dict)
|
|
||||||
events = Event.objects.filter(old=False).all()
|
|
||||||
event_formset = EventFormset(events=events, data=request_dict,
|
|
||||||
prefix='events')
|
|
||||||
if "user_exists" in request_dict and request_dict["user_exists"]:
|
|
||||||
username = request_dict["username"]
|
|
||||||
try:
|
|
||||||
member = User.objects.get(username=username)
|
|
||||||
user_form = RegistrationUserForm(request_dict, instance=member)
|
|
||||||
if member.profile.login_clipper:
|
|
||||||
login_clipper = member.profile.login_clipper
|
|
||||||
else:
|
|
||||||
user_form.force_long_username()
|
|
||||||
except User.DoesNotExist:
|
|
||||||
user_form.force_long_username()
|
|
||||||
else:
|
|
||||||
user_form.force_long_username()
|
|
||||||
|
|
||||||
# -----
|
|
||||||
# Validation des formulaires
|
|
||||||
# -----
|
|
||||||
|
|
||||||
if user_form.is_valid():
|
|
||||||
member = user_form.save()
|
|
||||||
cofprofile, _ = (CofProfile.objects
|
|
||||||
.get_or_create(profile=member.profile))
|
|
||||||
was_cof = cofprofile.is_cof
|
|
||||||
request_dict["num"] = cofprofile.num
|
|
||||||
# Maintenant on remplit le formulaire de profil
|
|
||||||
cofprofile_form = RegistrationCofProfileForm(
|
|
||||||
request_dict,
|
|
||||||
instance=cofprofile
|
|
||||||
)
|
|
||||||
profile_form = RegistrationProfileForm(
|
|
||||||
request_dict,
|
|
||||||
instance=member.profile
|
|
||||||
)
|
|
||||||
forms_are_valid = all((
|
|
||||||
profile_form.is_valid(),
|
|
||||||
cofprofile_form.is_valid(),
|
|
||||||
event_formset.is_valid(),
|
|
||||||
))
|
|
||||||
if forms_are_valid:
|
|
||||||
# Enregistrement du profil
|
|
||||||
profile = profile_form.save()
|
|
||||||
if profile.is_cof and not was_cof:
|
|
||||||
send_custom_mail(
|
|
||||||
"welcome", "cof@ens.fr", [member.email],
|
|
||||||
context={'member': member},
|
|
||||||
)
|
|
||||||
# Enregistrement des inscriptions aux événements
|
|
||||||
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(user=member, event=form.event)
|
|
||||||
current_registration.delete()
|
|
||||||
except EventRegistration.DoesNotExist:
|
|
||||||
pass
|
|
||||||
continue
|
|
||||||
all_choices = get_event_form_choices(form.event, form)
|
|
||||||
(current_registration, created_reg) = \
|
|
||||||
EventRegistration.objects.get_or_create(
|
|
||||||
user=member, event=form.event)
|
|
||||||
update_event_form_comments(form.event, form,
|
|
||||||
current_registration)
|
|
||||||
current_registration.options = all_choices
|
|
||||||
current_registration.paid = \
|
|
||||||
(form.cleaned_data['status'] == 'paid')
|
|
||||||
current_registration.save()
|
|
||||||
# if form.event.title == "Mega 15" and created_reg:
|
|
||||||
# field = EventCommentField.objects.get(
|
|
||||||
# event=form.event, name="Commentaires")
|
|
||||||
# try:
|
|
||||||
# comments = EventCommentValue.objects.get(
|
|
||||||
# commentfield=field,
|
|
||||||
# registration=current_registration).content
|
|
||||||
# except EventCommentValue.DoesNotExist:
|
|
||||||
# comments = field.default
|
|
||||||
# FIXME : il faut faire quelque chose de propre ici,
|
|
||||||
# par exemple écrire un mail générique pour
|
|
||||||
# l'inscription aux événements et/ou donner la
|
|
||||||
# possibilité d'associer un mail aux événements
|
|
||||||
# send_custom_mail(...)
|
|
||||||
success = True
|
|
||||||
# Messages
|
|
||||||
if success:
|
|
||||||
msg = ("L'inscription de {:s} (<tt>{:s}</tt>) a été "
|
|
||||||
"enregistrée avec succès"
|
|
||||||
.format(member.get_full_name(), member.email))
|
|
||||||
if member.profile.is_cof:
|
|
||||||
msg += "Il est désormais membre du COF n°{:d} !".format(
|
|
||||||
member.profile.num)
|
|
||||||
messages.success(request, msg, extra_tags='safe')
|
|
||||||
return render(request, "cof/registration_post.html",
|
|
||||||
{"user_form": user_form,
|
|
||||||
"profile_form": profile_form,
|
|
||||||
"cofprofile_form": cofprofile_form,
|
|
||||||
"member": member,
|
|
||||||
"login_clipper": login_clipper,
|
|
||||||
"event_formset": event_formset})
|
|
||||||
else:
|
|
||||||
return render(request, "registration.html")
|
|
||||||
|
|
||||||
|
|
||||||
@buro_required
|
@buro_required
|
||||||
def export_members(request):
|
def export_members(request):
|
||||||
response = HttpResponse(content_type='text/csv')
|
response = HttpResponse(content_type='text/csv')
|
||||||
|
|
|
@ -51,16 +51,6 @@ urlpatterns = [
|
||||||
url(r'^outsider/password-change-done$',
|
url(r'^outsider/password-change-done$',
|
||||||
django_views.password_change_done,
|
django_views.password_change_done,
|
||||||
name='password_change_done'),
|
name='password_change_done'),
|
||||||
# Inscription d'un nouveau membre
|
|
||||||
url(r'^registration$',
|
|
||||||
cof_views.registration,
|
|
||||||
name="registration"),
|
|
||||||
url(r'^registration/clipper/(?P<login_clipper>[\w-]+)$',
|
|
||||||
cof_views.registration_form2, name="clipper-registration"),
|
|
||||||
url(r'^registration/user/(?P<username>.+)$',
|
|
||||||
cof_views.registration_form2, name="user-registration"),
|
|
||||||
url(r'^registration/empty$', cof_views.registration_form2,
|
|
||||||
name="empty-registration"),
|
|
||||||
# Autocompletion
|
# Autocompletion
|
||||||
url(r'^autocomplete/registration$',
|
url(r'^autocomplete/registration$',
|
||||||
autocomplete,
|
autocomplete,
|
||||||
|
|
Loading…
Reference in a new issue