author | nishanth |
Mon, 12 Apr 2010 02:57:25 +0530 | |
changeset 9 | e29ecb7819e7 |
parent 8 | e2699e042129 |
child 17 | 125b6fc8f20b |
permissions | -rw-r--r-- |
6
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
1 |
import string |
8 | 2 |
from datetime import datetime |
6
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
3 |
|
7 | 4 |
from django import forms |
3
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
5 |
from django.contrib.auth.models import User |
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
6 |
from django.contrib.auth import authenticate |
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
7 |
|
8 | 8 |
from workshop.reg.models import Profile, Event |
6
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
9 |
|
4 | 10 |
class LoginForm(forms.Form): |
3
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
11 |
""" a form to handle login. |
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
12 |
""" |
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
13 |
|
4 | 14 |
email = forms.EmailField() |
15 |
password = forms.CharField(widget=forms.PasswordInput) |
|
3
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
16 |
|
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
17 |
def clean_email(self): |
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
18 |
""" see if a user exists for this email. |
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
19 |
""" |
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
20 |
|
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
21 |
email = self.cleaned_data['email'] |
4 | 22 |
password = self.data['password'] |
3
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
23 |
try: |
4 | 24 |
username = User.objects.get(email__iexact=email).username |
3
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
25 |
except User.DoesNotExist: |
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
26 |
raise forms.ValidationError("Incorrect e-mail or password") |
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
27 |
|
4 | 28 |
user = authenticate(username=username, password=password) |
3
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
29 |
if not user: |
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
30 |
raise forms.ValidationError("Incorrect e-mail or password") |
4 | 31 |
return email |
3
182f216da4a8
made the login view. have to write templates and check it.
nishanth
parents:
diff
changeset
|
32 |
|
6
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
33 |
class RegisterForm(forms.ModelForm): |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
34 |
""" add the fields email and password |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
35 |
and then generate form using profile model. |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
36 |
""" |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
37 |
|
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
38 |
email = forms.EmailField() |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
39 |
password = forms.CharField(widget=forms.PasswordInput, help_text="Choose a good password which 8 to 30 chars long") |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
40 |
confirm_password = forms.CharField(widget=forms.PasswordInput) |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
41 |
|
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
42 |
first_name = forms.CharField(required=True) |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
43 |
last_name = forms.CharField() |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
44 |
|
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
45 |
class Meta: |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
46 |
model = Profile |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
47 |
fields = ['email', 'password', 'confirm_password', 'first_name', 'last_name', 'gender', 'profession', 'affiliated_to', 'interests'] |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
48 |
|
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
49 |
def clean_email(self): |
7 | 50 |
""" check if a user exists with same email. |
51 |
""" |
|
52 |
||
6
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
53 |
email = self.cleaned_data['email'] |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
54 |
try: |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
55 |
User.objects.get(email__iexact=email) |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
56 |
raise forms.ValidationError("An account already exists with this email.\ |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
57 |
Click on forgot password if you have forgotten your password") |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
58 |
except User.DoesNotExist: |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
59 |
return email |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
60 |
|
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
61 |
def clean_password(self): |
7 | 62 |
""" check if the password contains only alphabets or digits or punctuation. |
63 |
then check if the size of the password is optimal. |
|
64 |
then check if both the given passwords match. |
|
65 |
""" |
|
66 |
||
6
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
67 |
password = self.cleaned_data['password'] |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
68 |
|
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
69 |
if password.strip(string.ascii_letters+string.punctuation+string.digits): |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
70 |
raise forms.ValidationError("Only alphabets, digits and punctuation symbols are allowed in password") |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
71 |
|
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
72 |
if not 8 <= len(password) <= 30: |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
73 |
raise forms.ValidationError("Password must be atleast 8 characters and at most 30 characters") |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
74 |
|
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
75 |
if not password == self.data['confirm_password']: |
057498d12450
users can now register but still there is no concept of activation e-mail .
nishanth
parents:
4
diff
changeset
|
76 |
raise forms.ValidationError("Passwords do not match") |
8 | 77 |
|
78 |
class EventCreateForm(forms.ModelForm): |
|
79 |
""" A form to create an event. |
|
80 |
""" |
|
81 |
||
82 |
class Meta: |
|
83 |
model = Event |
|
84 |
fields = ['title', 'description', 'start_date', 'stop_date'] |
|
85 |
||
86 |
def clean_start_date(self): |
|
87 |
""" see if the start date is greater than today or not. |
|
88 |
""" |
|
89 |
||
90 |
start_date = self.cleaned_data['start_date'] |
|
91 |
if start_date < datetime.now().date(): |
|
92 |
raise forms.ValidationError("The event must start at the latest today.") |
|
93 |
return start_date |
|
94 |
||
95 |
def clean_stop_date(self): |
|
96 |
""" see that stop_date is not less than start_date. |
|
97 |
""" |
|
98 |
||
99 |
stop_date = self.cleaned_data['stop_date'] |
|
100 |
try: |
|
101 |
start_date = datetime.strptime(self.data['start_date'], "%Y-%m-%d").date() |
|
102 |
except ValueError: |
|
103 |
raise forms.ValidationError("Enter a valid date") |
|
104 |
||
105 |
if start_date > stop_date: |
|
106 |
raise forms.ValidationError('The event stops even before it starts. I am starting to wonder what kind of event is this.') |
|
107 |
return stop_date |
|
9 | 108 |
|
109 |
class PasswordResetForm(forms.Form): |
|
110 |
""" check for the existance of user for the email. |
|
111 |
Reset the password irrespective of active status. |
|
112 |
""" |
|
113 |
||
114 |
email = forms.EmailField() |
|
115 |
||
116 |
def clean_email(self): |
|
117 |
||
118 |
email = self.cleaned_data['email'] |
|
119 |
try: |
|
120 |
user = User.objects.get(email__iexact=email) |
|
121 |
return email |
|
122 |
except User.DoesNotExist: |
|
123 |
raise forms.ValidationError("This not a registered email. Please enter a valid email.") |
|
124 |
||
125 |
class PasswordChangeForm(forms.Form): |
|
126 |
||
127 |
old_password = forms.CharField(widget=forms.PasswordInput) |
|
128 |
new_password = forms.CharField(widget=forms.PasswordInput) |
|
129 |
confirm_password = forms.CharField(widget=forms.PasswordInput) |
|
130 |
||
131 |
def clean_old_password(self): |
|
132 |
""" authenticate the given password against username. |
|
133 |
""" |
|
134 |
||
135 |
username = self.data['username'] |
|
136 |
old_password = self.cleaned_data['old_password'] |
|
137 |
user = authenticate(username=username, password=old_password) |
|
138 |
if not user: |
|
139 |
raise forms.ValidationError("Incorrect password") |
|
140 |
return old_password |
|
141 |
||
142 |
||
143 |
def clean_new_password(self): |
|
144 |
""" check if the password contains only alphabets or digits or punctuation. |
|
145 |
then check if the size of the password is optimal. |
|
146 |
then check if both the given passwords match. |
|
147 |
""" |
|
148 |
||
149 |
new_password = self.cleaned_data['new_password'] |
|
150 |
||
151 |
if new_password.strip(string.ascii_letters+string.punctuation+string.digits): |
|
152 |
raise forms.ValidationError("Only alphabets, digits and punctuation symbols are allowed in password") |
|
153 |
||
154 |
if not 8 <= len(new_password) <= 30: |
|
155 |
raise forms.ValidationError("Password must be atleast 8 characters and at most 30 characters") |
|
156 |
||
157 |
if not new_password == self.data['confirm_password']: |
|
158 |
raise forms.ValidationError("Passwords do not match") |
|
159 |