Première version en Python-Django.
[auf_paf.git] / paf / forms.py
... / ...
CommitLineData
1# -*- coding: utf8 -*-
2from django import forms
3from captcha.fields import CaptchaField
4from paf.models import Student
5from django.contrib.auth.models import User, Group
6from string import zfill
7import re
8
9class RegistrationForm(forms.Form):
10 NATIONALITY_CHOICES = (
11 ('KH', 'Cambodgienne'),
12 ('LA', 'Laotienne'),
13 ('VN', 'Vietnamienne'),
14 ('ZZ', 'Autre'),
15 )
16 GENDER_CHOICES = (
17 ('M', 'Male'),
18 ('F', 'Female'),
19 )
20 lastname = forms.CharField(label=u"Nom")
21 firstname= forms.CharField(label=u"Prénom")
22 nationality = forms.ChoiceField(choices=NATIONALITY_CHOICES, label=u"Nationalité")
23 sex = forms.ChoiceField(choices=GENDER_CHOICES, label=u"Sexe")
24 dob = forms.DateField(label='Date de naissance', input_formats=('%d/%m/%Y',), help_text='Entrer la date sous forme jj/mm/aaaa')
25 tel = forms.CharField(label=u"Tél.")
26 parent_tel = forms.CharField(required=False,label=u"Tél. de parents")
27 parent_address = forms.CharField(max_length=120, label=u"Adresse de parents")
28 email = forms.EmailField(required=False, label=u"Email")
29 captcha = CaptchaField(help_text=u"Entrer les caractères dans l'image", label=u"Entrer le code affiché")
30 password = forms.CharField(widget=forms.PasswordInput, label=u"Mot de passe", min_length=6, help_text=u"Au moins 6 caractères contenant <br />([a..z], [A..Z], [0..9], [!, @, #, $, %, ^, &, *])")
31 confirm_password = forms.CharField(widget=forms.PasswordInput, label=u"Confirmation du mot de passe", min_length=6)
32
33 def clean_password(self):
34 pw = self.cleaned_data['password']
35 if not (re.match(r'.*[a-z].*', pw) and re.match(r'.*[A-Z].*', pw) and re.match(r'.*[0-9].*', pw) and re.match(r'.*[!@#$%^&*].*', pw) ):
36 raise forms.ValidationError(u"Mot de passe invalide!")
37
38 def clean_confirm_password(self):
39# if 'password' not in self.cleaned_data: return
40 try:
41 if self.cleaned_data['password'] != self.cleaned_data['confirm_password']:
42 raise forms.ValidationError(u"Le mot de passe confirmé est différent du mot de passe!")
43 except:
44 pass
45
46 def save(self):
47 student = self.create_student()
48 self.create_user(student)
49 return student
50
51 def create_student(self):
52 cd = self.cleaned_data
53 student = Student(lastname=cd['lastname']
54 , firstname=cd['firstname']
55 , nationality=cd['nationality']
56 , sex=cd['sex']
57 , dob=cd['dob']
58 , email=cd['email']
59 , tel=cd['tel']
60 , parent_tel=cd['parent_tel']
61 , parent_address=cd['parent_address'])
62 student.identifier = self.calculate_identifier(student)
63 student.save()
64 return student
65
66 def calculate_identifier(self, student):
67 try:
68 last_identifier = Student.objects.filter(nationality=student.nationality).order_by('-identifier')[0].identifier
69 tmp = int(last_identifier[2:]) + 1
70 return last_identifier[:2] + zfill(tmp, len(last_identifier)-2)
71 except:
72 return student.nationality + '0000001'
73
74 def create_user(self, student):
75 cd = self.cleaned_data
76 user = User(username=student.identifier, is_staff=1, first_name='student')
77 user.set_password(cd['password'])
78 user.save()
79 group = self.get_student_group()
80 user.groups = [group]
81 user.save()
82 return user
83
84 def get_student_group(self):
85 try:
86 group = Group.objects.get(name='student')
87 except:
88 group = Group(name='student')
89 group.save()
90 return group