sdi/forms.py
author nishanth
Mon, 07 Jun 2010 15:50:01 +0530
branchanoop
changeset 70 58cafee2ee89
parent 68 b0e630caa6e6
child 71 c736873f35cb
permissions -rw-r--r--
now only logged in users can logout
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
e85ae25f920a added forms.py
nishanth
parents:
diff changeset
     1
from django import forms
68
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
     2
from django.contrib.auth import authenticate
8
e85ae25f920a added forms.py
nishanth
parents:
diff changeset
     3
19
3932d9426c44 added the topics field in register form
nishanth
parents: 8
diff changeset
     4
from sage_days.sdi.models import Registrant, TOPICS_CHOICES
41
651b341fd555 added captcha for registration
nishanth
parents: 25
diff changeset
     5
from captcha.fields import CaptchaField
8
e85ae25f920a added forms.py
nishanth
parents:
diff changeset
     6
e85ae25f920a added forms.py
nishanth
parents:
diff changeset
     7
class RegisterForm(forms.ModelForm):
e85ae25f920a added forms.py
nishanth
parents:
diff changeset
     8
    """ The form that is displayed to user.
e85ae25f920a added forms.py
nishanth
parents:
diff changeset
     9
    """
e85ae25f920a added forms.py
nishanth
parents:
diff changeset
    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
e85ae25f920a added forms.py
nishanth
parents:
diff changeset
    12
    class Meta:
e85ae25f920a added forms.py
nishanth
parents:
diff changeset
    13
        model = Registrant
45
29d7d70c9273 added new fields to registrant.
nishanth
parents: 44
diff changeset
    14
        exclude = ['priority_for_attending', 'selected_for_attending']
44
ce2b84e6f24a changed the name of captcha.
nishanth
parents: 41
diff changeset
    15
    verification_code = CaptchaField()
19
3932d9426c44 added the topics field in register form
nishanth
parents: 8
diff changeset
    16
21
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    17
    def clean_email(self):
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    18
        """ See if the user has already registered using the email.
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    19
        """
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    20
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    21
        email = self.cleaned_data['email'].strip()
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    22
        try:
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    23
            Registrant.objects.get(email__iexact=email)
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    24
            raise forms.ValidationError("This email is already registered. Did you register earlier??")
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    25
        except Registrant.DoesNotExist:
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    26
            return email
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    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")
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    43
    need_for_python_workshop = forms.BooleanField(required=False)
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
    def clean_topics_interested(self):
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    46
        """ split and return include list and 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
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    49
        include_list = []
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    50
        exclude_list = []
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    51
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    52
        topics = self.cleaned_data['topics_interested']
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    53
        for number in topics.split():
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    54
            if number.startswith('-'):
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    55
                exclude_list.append(number[1:])
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    56
            else:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    57
                include_list.append(number)
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
        return (include_list, exclude_list)
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
    def clean_knowledge_of_python(self):
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    62
        """ if two numbers are given take start and stop.
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    63
        else take highest as default.
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    64
        """
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
        range_str = self.cleaned_data['knowledge_of_python'].strip()
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    67
        if not range_str:
25
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
    68
            return ("","")
24
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
        start_stop = range_str.split("-")
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    71
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    72
        if len(start_stop) == 1:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    73
            if start_stop[0].isdigit():
25
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
    74
                return (start_stop[0], "")
24
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    75
            else:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    76
                raise forms.ValidationError("Invalid range for knowledge of Python")
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    77
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    78
        elif len(start_stop) == 2:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    79
            start, stop = start_stop
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    80
            if start.isdigit() and stop.isdigit():
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    81
                return (start, stop)
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
        else:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    85
            raise forms.ValidationError("Invalid range for knowledge of Python")
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
    def clean_knowledge_of_sage(self):
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    88
        """ if two numbers are given take start and stop.
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    89
        else take highest as default.
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    90
        """
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
        range_str = self.cleaned_data['knowledge_of_sage'].strip()
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    93
        if not range_str:
25
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
    94
            return ("","")
24
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
        start_stop = range_str.split("-")
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    97
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    98
        if len(start_stop) == 1:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    99
            if start_stop[0].isdigit():
25
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
   100
                return (start_stop[0], "")
24
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   101
            else:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   102
                raise forms.ValidationError("Invalid range for knowledge of Sage")
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   103
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   104
        elif len(start_stop) == 2:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   105
            start, stop = start_stop
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   106
            if start.isdigit() and stop.isdigit():
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   107
                return (start, stop)
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
        else:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   111
            raise forms.ValidationError("Invalid range for knowledge of Sage")
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
    def clean_likeliness_of_attending(self):
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   114
        """ if two numbers are given take start and stop.
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   115
        else take highest as default.
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   116
        """
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
        range_str = self.cleaned_data['likeliness_of_attending'].strip()
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   119
        if not range_str:
25
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
   120
            return ("","")
24
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
        start_stop = range_str.split("-")
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   123
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   124
        if len(start_stop) == 1:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   125
            if start_stop[0].isdigit():
25
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
   126
                return (start_stop[0], "")
24
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   127
            else:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   128
                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
   129
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   130
        elif len(start_stop) == 2:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   131
            start, stop = start_stop
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   132
            if start.isdigit() and stop.isdigit():
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   133
                return (start, stop)
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
        else:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   137
            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
   138
                   
56
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   139
class EmailForm(forms.Form):
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   140
    """ Take a list of csv seperated email addresses.
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   141
    """
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   142
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   143
    emails = forms.CharField(widget=forms.Textarea, required=False)
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   144
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   145
    def clean_emails(self):
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   146
        emails = self.cleaned_data['emails']
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   147
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   148
        to_emails = [csv_list.split(',') for csv_list in emails.split()]
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   149
        return to_emails
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   150
68
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   151
class LoginForm(forms.form):
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   152
    """ login form.
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   153
    """
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   154
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   155
    username = forms.CharField()
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   156
    password = forms.CharField(widget=forms.PasswordInput)
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   157
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   158
    def clean_username(self):
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   159
        username = self.cleaned_data['username']
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   160
        password = self.clean_password()
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   161
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   162
        if not authenticate(username=username, password=password):
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   163
            raise forms.ValidationError("Invalid username or password")
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   164
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   165
        return username
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   166
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   167
    def clean_password(self):
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   168
        return self.cleaned_data['password']