author | nishanth |
Sat, 29 May 2010 19:35:49 +0530 | |
changeset 25 | 30baf3c635c5 |
parent 24 | f79be1dd4a22 |
child 41 | 651b341fd555 |
permissions | -rw-r--r-- |
8 | 1 |
from django import forms |
2 |
||
19 | 3 |
from sage_days.sdi.models import Registrant, TOPICS_CHOICES |
8 | 4 |
|
5 |
class RegisterForm(forms.ModelForm): |
|
6 |
""" The form that is displayed to user. |
|
7 |
""" |
|
8 |
||
20
9db4ee082d4e
added topics choices and corresponding clean method
nishanth
parents:
19
diff
changeset
|
9 |
topics_interested = forms.MultipleChoiceField(widget=forms.CheckboxSelectMultiple, choices=TOPICS_CHOICES, required=False) |
8 | 10 |
class Meta: |
11 |
model = Registrant |
|
19 | 12 |
|
21 | 13 |
def clean_email(self): |
14 |
""" See if the user has already registered using the email. |
|
15 |
""" |
|
16 |
||
17 |
email = self.cleaned_data['email'].strip() |
|
18 |
try: |
|
19 |
Registrant.objects.get(email__iexact=email) |
|
20 |
raise forms.ValidationError("This email is already registered. Did you register earlier??") |
|
21 |
except Registrant.DoesNotExist: |
|
22 |
return email |
|
23 |
||
20
9db4ee082d4e
added topics choices and corresponding clean method
nishanth
parents:
19
diff
changeset
|
24 |
def clean_topics_interested(self): |
9db4ee082d4e
added topics choices and corresponding clean method
nishanth
parents:
19
diff
changeset
|
25 |
""" Join the choices using PIPE character and store them. |
9db4ee082d4e
added topics choices and corresponding clean method
nishanth
parents:
19
diff
changeset
|
26 |
""" |
9db4ee082d4e
added topics choices and corresponding clean method
nishanth
parents:
19
diff
changeset
|
27 |
|
9db4ee082d4e
added topics choices and corresponding clean method
nishanth
parents:
19
diff
changeset
|
28 |
topics = self.cleaned_data['topics_interested'] |
9db4ee082d4e
added topics choices and corresponding clean method
nishanth
parents:
19
diff
changeset
|
29 |
return "|".join(topics) |
24
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
30 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
31 |
class SearchForm(forms.Form): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
32 |
""" Search form for filtering registrants. |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
33 |
""" |
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 |
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
|
36 |
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
|
37 |
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
|
38 |
likeliness_of_attending = 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
|
39 |
need_for_python_workshop = forms.BooleanField(required=False) |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
40 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
41 |
def clean_topics_interested(self): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
42 |
""" split and return include list and exclude list |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
43 |
""" |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
44 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
45 |
include_list = [] |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
46 |
exclude_list = [] |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
47 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
48 |
topics = self.cleaned_data['topics_interested'] |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
49 |
for number in topics.split(): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
50 |
if number.startswith('-'): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
51 |
exclude_list.append(number[1:]) |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
52 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
53 |
include_list.append(number) |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
54 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
55 |
return (include_list, exclude_list) |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
56 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
57 |
def clean_knowledge_of_python(self): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
58 |
""" if two numbers are given take start and stop. |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
59 |
else take highest as default. |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
60 |
""" |
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 |
range_str = self.cleaned_data['knowledge_of_python'].strip() |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
63 |
if not range_str: |
25 | 64 |
return ("","") |
24
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 |
start_stop = range_str.split("-") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
67 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
68 |
if len(start_stop) == 1: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
69 |
if start_stop[0].isdigit(): |
25 | 70 |
return (start_stop[0], "") |
24
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
71 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
72 |
raise forms.ValidationError("Invalid range for knowledge of Python") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
73 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
74 |
elif len(start_stop) == 2: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
75 |
start, stop = start_stop |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
76 |
if start.isdigit() and stop.isdigit(): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
77 |
return (start, stop) |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
78 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
79 |
raise forms.ValidationError("Invalid range for knowledge of Python") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
80 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
81 |
raise forms.ValidationError("Invalid range for knowledge of Python") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
82 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
83 |
def clean_knowledge_of_sage(self): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
84 |
""" if two numbers are given take start and stop. |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
85 |
else take highest as default. |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
86 |
""" |
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 |
range_str = self.cleaned_data['knowledge_of_sage'].strip() |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
89 |
if not range_str: |
25 | 90 |
return ("","") |
24
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 |
start_stop = range_str.split("-") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
93 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
94 |
if len(start_stop) == 1: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
95 |
if start_stop[0].isdigit(): |
25 | 96 |
return (start_stop[0], "") |
24
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
97 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
98 |
raise forms.ValidationError("Invalid range for knowledge of Sage") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
99 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
100 |
elif len(start_stop) == 2: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
101 |
start, stop = start_stop |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
102 |
if start.isdigit() and stop.isdigit(): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
103 |
return (start, stop) |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
104 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
105 |
raise forms.ValidationError("Invalid range for knowledge of Sage") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
106 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
107 |
raise forms.ValidationError("Invalid range for knowledge of Sage") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
108 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
109 |
def clean_likeliness_of_attending(self): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
110 |
""" if two numbers are given take start and stop. |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
111 |
else take highest as default. |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
112 |
""" |
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 |
range_str = self.cleaned_data['likeliness_of_attending'].strip() |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
115 |
if not range_str: |
25 | 116 |
return ("","") |
24
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 |
start_stop = range_str.split("-") |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
119 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
120 |
if len(start_stop) == 1: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
121 |
if start_stop[0].isdigit(): |
25 | 122 |
return (start_stop[0], "") |
24
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
123 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
124 |
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
|
125 |
|
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
126 |
elif len(start_stop) == 2: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
127 |
start, stop = start_stop |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
128 |
if start.isdigit() and stop.isdigit(): |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
129 |
return (start, stop) |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
130 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
131 |
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
|
132 |
else: |
f79be1dd4a22
added clean methods for each attribute in search form
nishanth
parents:
21
diff
changeset
|
133 |
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
|
134 |