author | anoop |
Thu, 29 Jul 2010 17:40:16 +0530 | |
branch | anoop |
changeset 195 | 145053464556 |
parent 175 | b48d4c18a0aa |
child 234 | cf3de6d222ce |
permissions | -rw-r--r-- |
8 | 1 |
from django import forms |
68 | 2 |
from django.contrib.auth import authenticate |
8 | 3 |
|
131 | 4 |
from sage_days.sdi.models import Registrant, TOPICS_CHOICES, ParticipantInfo, SPRINT_CHOICES |
41 | 5 |
from captcha.fields import CaptchaField |
8 | 6 |
|
7 |
class RegisterForm(forms.ModelForm): |
|
8 |
""" The form that is displayed to user. |
|
9 |
""" |
|
10 |
||
20
9db4ee082d4e
added topics choices and corresponding clean method
nishanth
parents:
19
diff
changeset
|
11 |
topics_interested = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=TOPICS_CHOICES, required=False) |
8 | 12 |
class Meta: |
13 |
model = Registrant |
|
45 | 14 |
exclude = ['priority_for_attending', 'selected_for_attending'] |
44 | 15 |
verification_code = CaptchaField() |
19 | 16 |
|
21 | 17 |
def clean_email(self): |
18 |
""" See if the user has already registered using the email. |
|
19 |
""" |
|
20 |
||
21 |
email = self.cleaned_data['email'].strip() |
|
22 |
try: |
|
23 |
Registrant.objects.get(email__iexact=email) |
|
24 |
raise forms.ValidationError("This email is already registered. Did you register earlier??") |
|
25 |
except Registrant.DoesNotExist: |
|
26 |
return email |
|
27 |
||
20
9db4ee082d4e
added topics choices and corresponding clean method
nishanth
parents:
19
diff
changeset
|
28 |
def clean_topics_interested(self): |
9db4ee082d4e
added topics choices and corresponding clean method
nishanth
parents:
19
diff
changeset
|
29 |
""" Join the choices using PIPE character and store them. |
9db4ee082d4e
added topics choices and corresponding clean method
nishanth
parents:
19
diff
changeset
|
30 |
""" |
9db4ee082d4e
added topics choices and corresponding clean method
nishanth
parents:
19
diff
changeset
|
31 |
|
9db4ee082d4e
added topics choices and corresponding clean method
nishanth
parents:
19
diff
changeset
|
32 |
topics = self.cleaned_data['topics_interested'] |
9db4ee082d4e
added topics choices and corresponding clean method
nishanth
parents:
19
diff
changeset
|
33 |
return "|".join(topics) |
24
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
34 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
35 |
class SearchForm(forms.Form): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
36 |
""" Search form for filtering registrants. |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
37 |
""" |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
38 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
39 |
topics_interested = forms.CharField(required=False, help_text=" Use numbers seperated by spaces and prefix a minus to exclude") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
40 |
knowledge_of_python = forms.CharField(required=False, help_text=" Give single number from 1 to 5 or a range seperated by hyphen") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
41 |
knowledge_of_sage = forms.CharField(required=False, help_text=" Give single number from 1 to 5 or a range seperated by hyphen") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
42 |
likeliness_of_attending = forms.CharField(required=False, help_text=" Give single number from 1 to 5 or a range seperated by hyphen") |
79
064ff60025d9
removed the need_python_workshop and need_acco fields since they are very confusing
nishanth
parents:
77
diff
changeset
|
43 |
#need_for_python_workshop = forms.BooleanField(required=False) |
064ff60025d9
removed the need_python_workshop and need_acco fields since they are very confusing
nishanth
parents:
77
diff
changeset
|
44 |
#need_accomodation = forms.BooleanField(required=False) |
24
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
45 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
46 |
def clean_topics_interested(self): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
47 |
""" split and return include list and exclude list |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
48 |
""" |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
49 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
50 |
include_list = [] |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
51 |
exclude_list = [] |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
52 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
53 |
topics = self.cleaned_data['topics_interested'] |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
54 |
for number in topics.split(): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
55 |
if number.startswith('-'): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
56 |
exclude_list.append(number[1:]) |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
57 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
58 |
include_list.append(number) |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
59 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
60 |
return (include_list, exclude_list) |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
61 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
62 |
def clean_knowledge_of_python(self): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
63 |
""" if two numbers are given take start and stop. |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
64 |
else take highest as default. |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
65 |
""" |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
66 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
67 |
range_str = self.cleaned_data['knowledge_of_python'].strip() |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
68 |
if not range_str: |
25 | 69 |
return ("","") |
24
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
70 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
71 |
start_stop = range_str.split("-") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
72 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
73 |
if len(start_stop) == 1: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
74 |
if start_stop[0].isdigit(): |
25 | 75 |
return (start_stop[0], "") |
24
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
76 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
77 |
raise forms.ValidationError("Invalid range for knowledge of Python") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
78 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
79 |
elif len(start_stop) == 2: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
80 |
start, stop = start_stop |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
81 |
if start.isdigit() and stop.isdigit(): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
82 |
return (start, stop) |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
83 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
84 |
raise forms.ValidationError("Invalid range for knowledge of Python") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
85 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
86 |
raise forms.ValidationError("Invalid range for knowledge of Python") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
87 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
88 |
def clean_knowledge_of_sage(self): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
89 |
""" if two numbers are given take start and stop. |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
90 |
else take highest as default. |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
91 |
""" |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
92 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
93 |
range_str = self.cleaned_data['knowledge_of_sage'].strip() |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
94 |
if not range_str: |
25 | 95 |
return ("","") |
24
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
96 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
97 |
start_stop = range_str.split("-") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
98 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
99 |
if len(start_stop) == 1: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
100 |
if start_stop[0].isdigit(): |
25 | 101 |
return (start_stop[0], "") |
24
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
102 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
103 |
raise forms.ValidationError("Invalid range for knowledge of Sage") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
104 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
105 |
elif len(start_stop) == 2: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
106 |
start, stop = start_stop |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
107 |
if start.isdigit() and stop.isdigit(): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
108 |
return (start, stop) |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
109 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
110 |
raise forms.ValidationError("Invalid range for knowledge of Sage") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
111 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
112 |
raise forms.ValidationError("Invalid range for knowledge of Sage") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
113 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
114 |
def clean_likeliness_of_attending(self): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
115 |
""" if two numbers are given take start and stop. |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
116 |
else take highest as default. |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
117 |
""" |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
118 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
119 |
range_str = self.cleaned_data['likeliness_of_attending'].strip() |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
120 |
if not range_str: |
25 | 121 |
return ("","") |
24
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
122 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
123 |
start_stop = range_str.split("-") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
124 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
125 |
if len(start_stop) == 1: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
126 |
if start_stop[0].isdigit(): |
25 | 127 |
return (start_stop[0], "") |
24
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
128 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
129 |
raise forms.ValidationError("Invalid range for Likeliness of attending the workshop") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
130 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
131 |
elif len(start_stop) == 2: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
132 |
start, stop = start_stop |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
133 |
if start.isdigit() and stop.isdigit(): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
134 |
return (start, stop) |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
135 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
136 |
raise forms.ValidationError("Invalid range for Likeliness of attending the workshop") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
137 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
138 |
raise forms.ValidationError("Invalid range for Likeliness of attending the workshop") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
139 |
|
56 | 140 |
class EmailForm(forms.Form): |
141 |
""" Take a list of csv seperated email addresses. |
|
142 |
""" |
|
143 |
||
144 |
emails = forms.CharField(widget=forms.Textarea, required=False) |
|
145 |
||
146 |
def clean_emails(self): |
|
147 |
emails = self.cleaned_data['emails'] |
|
148 |
||
149 |
to_emails = [csv_list.split(',') for csv_list in emails.split()] |
|
150 |
return to_emails |
|
151 |
||
71 | 152 |
class LoginForm(forms.Form): |
68 | 153 |
""" login form. |
154 |
""" |
|
155 |
||
156 |
username = forms.CharField() |
|
157 |
password = forms.CharField(widget=forms.PasswordInput) |
|
158 |
||
159 |
def clean_username(self): |
|
160 |
username = self.cleaned_data['username'] |
|
72 | 161 |
try: |
162 |
password = self.data['password'] |
|
163 |
except KeyError: |
|
164 |
raise forms.ValidationError("Invalid username or password") |
|
68 | 165 |
|
166 |
if not authenticate(username=username, password=password): |
|
167 |
raise forms.ValidationError("Invalid username or password") |
|
168 |
||
169 |
return username |
|
170 |
||
93
505989755cd8
modified the form and implemented the view to display selected users
nishanth
parents:
90
diff
changeset
|
171 |
def UserSelectForm(users, post_data=None ): |
90
1a6e1af98624
now selecting users through forms. created a form for that
nishanth
parents:
79
diff
changeset
|
172 |
|
1a6e1af98624
now selecting users through forms. created a form for that
nishanth
parents:
79
diff
changeset
|
173 |
choices = [ (_.id, _.first_name) for _ in users ] |
1a6e1af98624
now selecting users through forms. created a form for that
nishanth
parents:
79
diff
changeset
|
174 |
class myF(forms.Form): |
1a6e1af98624
now selecting users through forms. created a form for that
nishanth
parents:
79
diff
changeset
|
175 |
|
1a6e1af98624
now selecting users through forms. created a form for that
nishanth
parents:
79
diff
changeset
|
176 |
selected_users = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=choices, required=False) |
1a6e1af98624
now selecting users through forms. created a form for that
nishanth
parents:
79
diff
changeset
|
177 |
|
93
505989755cd8
modified the form and implemented the view to display selected users
nishanth
parents:
90
diff
changeset
|
178 |
def clean_selected_users(self): |
505989755cd8
modified the form and implemented the view to display selected users
nishanth
parents:
90
diff
changeset
|
179 |
selected_ids = self.cleaned_data['selected_users'] |
505989755cd8
modified the form and implemented the view to display selected users
nishanth
parents:
90
diff
changeset
|
180 |
return [ Registrant.objects.get(id=_) for _ in selected_ids ] |
505989755cd8
modified the form and implemented the view to display selected users
nishanth
parents:
90
diff
changeset
|
181 |
|
505989755cd8
modified the form and implemented the view to display selected users
nishanth
parents:
90
diff
changeset
|
182 |
return myF(post_data) if post_data else myF() |
505989755cd8
modified the form and implemented the view to display selected users
nishanth
parents:
90
diff
changeset
|
183 |
|
131 | 184 |
class ParticipantInfoForm(forms.Form): |
124
d4a7644e7fe8
created a form for taking participant info and used it in the view
nishanth
parents:
93
diff
changeset
|
185 |
|
133 | 186 |
has_laptop_for_sagedays = forms.ChoiceField(choices=(("Yes","Yes"), ("No", "No"))) |
132 | 187 |
sprinted_already = forms.ChoiceField(choices=(("Yes","Yes"), ("No", "No"))) |
131 | 188 |
will_sprint = forms.ChoiceField(choices=SPRINT_CHOICES) |
189 |
||
175 | 190 |
|
191 |
def SendAccoForm(users, post_data=None): |
|
192 |
||
193 |
choices = [ (_.id, _.first_name) for _ in users ] |
|
194 |
class myF(forms.Form): |
|
195 |
||
196 |
message = forms.CharField(required=True) |
|
197 |
selected_users = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=choices, required=False) |
|
198 |
||
199 |
def clean_selected_users(self): |
|
200 |
selected_ids = self.cleaned_data['selected_users'] |
|
201 |
return [ Registrant.objects.get(id=_) for _ in selected_ids ] |
|
202 |
||
203 |
def clean_message(self): |
|
204 |
message = self.cleaned_data['message'] |
|
205 |
return message.strip() |
|
206 |
||
207 |
return myF(post_data) if post_data else myF() |
|
208 |