sdi/forms.py
author nishanth
Fri, 16 Jul 2010 10:07:13 +0530
branchanoop
changeset 101 9def10724853
parent 93 505989755cd8
child 124 d4a7644e7fe8
permissions -rw-r--r--
added more fields in selecting users for workshop
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")
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
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
    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
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
    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
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
    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
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
   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
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
   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
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
   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
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   140
class EmailForm(forms.Form):
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   141
    """ Take a list of csv seperated email addresses.
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
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   144
    emails = forms.CharField(widget=forms.Textarea, required=False)
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   145
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   146
    def clean_emails(self):
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   147
        emails = self.cleaned_data['emails']
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   148
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   149
        to_emails = [csv_list.split(',') for csv_list in emails.split()]
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   150
        return to_emails
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   151
71
c736873f35cb fixed a bug
nishanth
parents: 68
diff changeset
   152
class LoginForm(forms.Form):
68
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   153
    """ login form.
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
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   156
    username = forms.CharField()
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   157
    password = forms.CharField(widget=forms.PasswordInput)
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   158
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   159
    def clean_username(self):
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   160
        username = self.cleaned_data['username']
72
3673ed3ca27c fixed a few bugs
nishanth
parents: 71
diff changeset
   161
        try:
3673ed3ca27c fixed a few bugs
nishanth
parents: 71
diff changeset
   162
            password = self.data['password']
3673ed3ca27c fixed a few bugs
nishanth
parents: 71
diff changeset
   163
        except KeyError:
3673ed3ca27c fixed a few bugs
nishanth
parents: 71
diff changeset
   164
            raise forms.ValidationError("Invalid username or password")
68
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   165
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   166
        if not authenticate(username=username, password=password):
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   167
            raise forms.ValidationError("Invalid username or password")
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   168
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   169
        return username
b0e630caa6e6 added required forms for login and logout
nishanth
parents: 56
diff changeset
   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