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