|
1 import string |
|
2 |
1 from django.contrib.auth.models import User |
3 from django.contrib.auth.models import User |
2 from django import forms |
4 from django import forms |
3 |
5 |
4 from django.contrib.auth import authenticate |
6 from django.contrib.auth import authenticate |
|
7 |
|
8 from workshop.reg.models import Profile |
5 |
9 |
6 class LoginForm(forms.Form): |
10 class LoginForm(forms.Form): |
7 """ a form to handle login. |
11 """ a form to handle login. |
8 """ |
12 """ |
9 |
13 |
24 user = authenticate(username=username, password=password) |
28 user = authenticate(username=username, password=password) |
25 if not user: |
29 if not user: |
26 raise forms.ValidationError("Incorrect e-mail or password") |
30 raise forms.ValidationError("Incorrect e-mail or password") |
27 return email |
31 return email |
28 |
32 |
|
33 class RegisterForm(forms.ModelForm): |
|
34 """ add the fields email and password |
|
35 and then generate form using profile model. |
|
36 """ |
|
37 |
|
38 email = forms.EmailField() |
|
39 password = forms.CharField(widget=forms.PasswordInput, help_text="Choose a good password which 8 to 30 chars long") |
|
40 confirm_password = forms.CharField(widget=forms.PasswordInput) |
|
41 |
|
42 first_name = forms.CharField(required=True) |
|
43 last_name = forms.CharField() |
|
44 |
|
45 class Meta: |
|
46 model = Profile |
|
47 fields = ['email', 'password', 'confirm_password', 'first_name', 'last_name', 'gender', 'profession', 'affiliated_to', 'interests'] |
|
48 |
|
49 def clean_email(self): |
|
50 email = self.cleaned_data['email'] |
|
51 try: |
|
52 User.objects.get(email__iexact=email) |
|
53 raise forms.ValidationError("An account already exists with this email.\ |
|
54 Click on forgot password if you have forgotten your password") |
|
55 except User.DoesNotExist: |
|
56 return email |
|
57 |
|
58 def clean_password(self): |
|
59 password = self.cleaned_data['password'] |
|
60 |
|
61 if password.strip(string.ascii_letters+string.punctuation+string.digits): |
|
62 raise forms.ValidationError("Only alphabets, digits and punctuation symbols are allowed in password") |
|
63 |
|
64 if not 8 <= len(password) <= 30: |
|
65 raise forms.ValidationError("Password must be atleast 8 characters and at most 30 characters") |
|
66 |
|
67 if not password == self.data['confirm_password']: |
|
68 raise forms.ValidationError("Passwords do not match") |