1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
|
# -*- coding: utf-8 -*-
from __future__ import absolute_import
#django
from django import forms
from django.contrib.auth.models import User
class RegistrantForm(forms.Form):
"""Form to register an attendee
"""
username = forms.RegexField(label="Nickname", max_length=30,
regex=r'^\w+$',
help_text = "30 characters or fewer. Alphanumeric" \
+ " characters only (letters, digits and underscores).",
error_message = "This value must contain only letters, numbers and underscores.")
name = forms.CharField(label=u"Name", max_length=50, required=True)
email = forms.EmailField(label=u"E-mail", max_length=50, required=True)
def clean_email(self):
"""Validates that the entered e-mail is unique.
"""
email = self.cleaned_data.get("email")
if email and User.objects.filter(email=email).count() > 0:
raise forms.ValidationError(
u"That email address is already in use. Are you a member of " \
"site? Please log in.")
return email
def clean_username(self):
"""Validates that the entered username is unique.
"""
username = self.cleaned_data.get("username")
if username and User.objects.filter(username=username).count() > 0:
raise forms.ValidationError(
u"That username is already in use.")
return username
class RegisterForm(forms.Form):
"""Form to register speaker
"""
username = forms.RegexField(label="Username", max_length=30,
regex=r'^\w+$',
help_text = "Required. 30 characters or fewer. Alphanumeric" \
+ " characters only (letters, digits and underscores).",
error_message = "This value must contain only letters, numbers and underscores.")
first_name = forms.CharField(label=u"First name", max_length=50)
last_name = forms.CharField(label=u"Last name", max_length=50)
email = forms.EmailField(label=u"E-mail", max_length=50)
url = forms.URLField(required=False)
about = forms.CharField(label=u'Short Bio', max_length=50, required=False)
photo = forms.FileField(label=u'Profile Photo', required=False)
password_1 = forms.CharField(
label=u"Password", widget=forms.PasswordInput(), max_length=20)
password_2 = forms.CharField(
label=u"Confirm password", widget=forms.PasswordInput(), max_length=20)
def clean_password_2(self):
"""Validates that password 1 and password 2 are the same.
"""
p1 = self.cleaned_data.get('password_1')
p2 = self.cleaned_data.get('password_2')
if not (p1 and p2 and p1 == p2):
raise forms.ValidationError(u"The two passwords do not match.")
return p2
def clean_email(self):
"""Validates that the entered e-mail is unique.
"""
email = self.cleaned_data.get("email")
if email and User.objects.filter(email=email).count() > 0:
raise forms.ValidationError(
u"That email address is already in use.")
return email
def clean_username(self):
"""Validates that the entered username is unique.
"""
username = self.cleaned_data.get("username")
if username and User.objects.filter(username=username).count() > 0:
raise forms.ValidationError(
u"That username is already in use.")
return username
class EditProfileForm(forms.Form):
"""Edit user profile form
"""
first_name = forms.CharField(max_length=50)
last_name = forms.CharField(max_length=50)
email = forms.EmailField(max_length=50)
email2 = forms.CharField(widget=forms.HiddenInput)
url = forms.URLField(required=False)
about = forms.CharField(label=u'Short Bio',
widget=forms.Textarea, required=False)
photo = forms.FileField(label=u'Profile Photo',
required=False)
def clean_email(self):
"""Validates that the entered e-mail is unique.
"""
email = self.cleaned_data.get("email")
email2 = self.data.get("email2").strip()
print email, email2
if email != email2: # email has been changed
if email and User.objects.filter(email=email).count() > 0:
raise forms.ValidationError(
u"That email address is already in use.")
return email
class UsernameForm(forms.Form):
"""Form to edit email address
"""
username = forms.RegexField(label="Username", max_length=30,
regex=r'^\w+$',
help_text = "Required. 30 characters or fewer. Alphanumeric" \
+ " characters only (letters, digits and underscores).",
error_message = "This value must contain only letters, numbers and underscores.")
def clean_username(self):
"""Validates that the entered username is unique.
"""
username = self.cleaned_data.get("username")
if username and User.objects.filter(username=username).count() > 0:
raise forms.ValidationError(
u"That username is already in use.")
return username
|