added clean methods for each attribute in search form
authornishanth
Sat, 29 May 2010 18:41:04 +0530
changeset 24 f79be1dd4a22
parent 23 2aae8293f3a7
child 25 30baf3c635c5
added clean methods for each attribute in search form
sdi/forms.py
sdi/views.py
--- a/sdi/forms.py	Sat May 29 17:26:48 2010 +0530
+++ b/sdi/forms.py	Sat May 29 18:41:04 2010 +0530
@@ -27,3 +27,111 @@
 
         topics = self.cleaned_data['topics_interested']
         return "|".join(topics)
+
+class SearchForm(forms.Form):
+    """ Search form for filtering registrants.
+    """
+
+    topics_interested = forms.CharField(required=False, help_text=" Use numbers seperated by spaces and prefix a minus to exclude")
+    knowledge_of_python = forms.CharField(required=False, help_text=" Give single number from 1 to 5 or a range seperated by hyphen")
+    knowledge_of_sage = forms.CharField(required=False, help_text=" Give single number from 1 to 5 or a range seperated by hyphen")
+    likeliness_of_attending = forms.CharField(required=False, help_text=" Give single number from 1 to 5 or a range seperated by hyphen")
+    need_for_python_workshop = forms.BooleanField(required=False)
+
+    def clean_topics_interested(self):
+        """ split and return include list and exclude list
+        """
+
+        include_list = []
+        exclude_list = []
+
+        topics = self.cleaned_data['topics_interested']
+        for number in topics.split():
+            if number.startswith('-'):
+                exclude_list.append(number[1:])
+            else:
+                include_list.append(number)
+
+        return (include_list, exclude_list)
+
+    def clean_knowledge_of_python(self):
+        """ if two numbers are given take start and stop.
+        else take highest as default.
+        """
+
+        range_str = self.cleaned_data['knowledge_of_python'].strip()
+        if not range_str:
+            return (1,5)
+
+        start_stop = range_str.split("-")
+
+        if len(start_stop) == 1:
+            if start_stop[0].isdigit():
+                print "only one"
+                return (start_stop[0], 5)
+            else:
+                raise forms.ValidationError("Invalid range for knowledge of Python")
+
+        elif len(start_stop) == 2:
+            start, stop = start_stop
+            if start.isdigit() and stop.isdigit():
+                return (start, stop)
+            else:
+                 raise forms.ValidationError("Invalid range for knowledge of Python")
+        else:
+            raise forms.ValidationError("Invalid range for knowledge of Python")
+ 
+    def clean_knowledge_of_sage(self):
+        """ if two numbers are given take start and stop.
+        else take highest as default.
+        """
+
+        range_str = self.cleaned_data['knowledge_of_sage'].strip()
+        if not range_str:
+            return (1,5)
+
+        start_stop = range_str.split("-")
+
+        if len(start_stop) == 1:
+            if start_stop[0].isdigit():
+                print "only one"
+                return (start_stop[0], 5)
+            else:
+                raise forms.ValidationError("Invalid range for knowledge of Sage")
+
+        elif len(start_stop) == 2:
+            start, stop = start_stop
+            if start.isdigit() and stop.isdigit():
+                return (start, stop)
+            else:
+                 raise forms.ValidationError("Invalid range for knowledge of Sage")
+        else:
+            raise forms.ValidationError("Invalid range for knowledge of Sage")
+ 
+    def clean_likeliness_of_attending(self):
+        """ if two numbers are given take start and stop.
+        else take highest as default.
+        """
+
+        range_str = self.cleaned_data['likeliness_of_attending'].strip()
+        if not range_str:
+            return (1,5)
+
+        start_stop = range_str.split("-")
+
+        if len(start_stop) == 1:
+            if start_stop[0].isdigit():
+                print "only one"
+                return (start_stop[0], 5)
+            else:
+                raise forms.ValidationError("Invalid range for Likeliness of attending the workshop")
+
+        elif len(start_stop) == 2:
+            start, stop = start_stop
+            if start.isdigit() and stop.isdigit():
+                return (start, stop)
+            else:
+                 raise forms.ValidationError("Invalid range for Likeliness of attending the workshop")
+        else:
+            raise forms.ValidationError("Invalid range for Likeliness of attending the workshop")
+                   
--- a/sdi/views.py	Sat May 29 17:26:48 2010 +0530
+++ b/sdi/views.py	Sat May 29 18:41:04 2010 +0530
@@ -1,7 +1,7 @@
 from django.shortcuts import render_to_response, redirect
 
 from sage_days.sdi.models import Registrant
-from sage_days.sdi.forms import RegisterForm
+from sage_days.sdi.forms import RegisterForm, SearchForm
 
 def register(request):
     """ The user register page.
@@ -28,4 +28,12 @@
     """ List the statiscs of registered participants.
     """
 
-    return redirect("/registration/complete")
+    if request.method == "POST":
+        form = SearchForm(request.POST)
+        if form.is_valid():
+            pass
+        else:
+            return render_to_response("list_stats.html", {"form":form})
+    else:
+        form = SearchForm()
+        return render_to_response("list_stats.html", {"form":form})