sdi/forms.py
author nishanth
Sun, 06 Jun 2010 17:32:50 +0530
branchanoop
changeset 56 7dfacad8adee
parent 45 29d7d70c9273
child 68 b0e630caa6e6
permissions -rw-r--r--
provided an interface for sending out emails
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
45
29d7d70c9273 added new fields to registrant.
nishanth
parents: 44
diff changeset
    13
        exclude = ['priority_for_attending', 'selected_for_attending']
44
ce2b84e6f24a changed the name of captcha.
nishanth
parents: 41
diff changeset
    14
    verification_code = CaptchaField()
19
3932d9426c44 added the topics field in register form
nishanth
parents: 8
diff changeset
    15
21
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    16
    def clean_email(self):
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    17
        """ See if the user has already registered using the email.
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    18
        """
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    19
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    20
        email = self.cleaned_data['email'].strip()
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    21
        try:
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    22
            Registrant.objects.get(email__iexact=email)
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    23
            raise forms.ValidationError("This email is already registered. Did you register earlier??")
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    24
        except Registrant.DoesNotExist:
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    25
            return email
3d7a52d9ed2e added clean email
nishanth
parents: 20
diff changeset
    26
20
9db4ee082d4e added topics choices and corresponding clean method
nishanth
parents: 19
diff changeset
    27
    def clean_topics_interested(self):
9db4ee082d4e added topics choices and corresponding clean method
nishanth
parents: 19
diff changeset
    28
        """ Join the choices using PIPE character and store them.
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
9db4ee082d4e added topics choices and corresponding clean method
nishanth
parents: 19
diff changeset
    31
        topics = self.cleaned_data['topics_interested']
9db4ee082d4e added topics choices and corresponding clean method
nishanth
parents: 19
diff changeset
    32
        return "|".join(topics)
24
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
class SearchForm(forms.Form):
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    35
    """ Search form for filtering registrants.
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
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    38
    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
    39
    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
    40
    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
    41
    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
    42
    need_for_python_workshop = forms.BooleanField(required=False)
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
    def clean_topics_interested(self):
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    45
        """ split and return include list and exclude list
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
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    48
        include_list = []
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    49
        exclude_list = []
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    50
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    51
        topics = self.cleaned_data['topics_interested']
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    52
        for number in topics.split():
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    53
            if number.startswith('-'):
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    54
                exclude_list.append(number[1:])
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    55
            else:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    56
                include_list.append(number)
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    57
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    58
        return (include_list, exclude_list)
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
    def clean_knowledge_of_python(self):
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    61
        """ if two numbers are given take start and stop.
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    62
        else take highest as default.
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
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    65
        range_str = self.cleaned_data['knowledge_of_python'].strip()
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    66
        if not range_str:
25
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
    67
            return ("","")
24
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    68
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    69
        start_stop = range_str.split("-")
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
        if len(start_stop) == 1:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    72
            if start_stop[0].isdigit():
25
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
    73
                return (start_stop[0], "")
24
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    74
            else:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    75
                raise forms.ValidationError("Invalid range for knowledge of Python")
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    76
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    77
        elif len(start_stop) == 2:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    78
            start, stop = start_stop
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    79
            if start.isdigit() and stop.isdigit():
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    80
                return (start, stop)
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    81
            else:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    82
                 raise forms.ValidationError("Invalid range for knowledge of Python")
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
 
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    86
    def clean_knowledge_of_sage(self):
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    87
        """ if two numbers are given take start and stop.
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    88
        else take highest as default.
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
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    91
        range_str = self.cleaned_data['knowledge_of_sage'].strip()
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    92
        if not range_str:
25
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
    93
            return ("","")
24
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    94
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    95
        start_stop = range_str.split("-")
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
        if len(start_stop) == 1:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
    98
            if start_stop[0].isdigit():
25
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
    99
                return (start_stop[0], "")
24
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   100
            else:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   101
                raise forms.ValidationError("Invalid range for knowledge of Sage")
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   102
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   103
        elif len(start_stop) == 2:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   104
            start, stop = start_stop
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   105
            if start.isdigit() and stop.isdigit():
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   106
                return (start, stop)
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   107
            else:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   108
                 raise forms.ValidationError("Invalid range for knowledge of Sage")
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
 
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   112
    def clean_likeliness_of_attending(self):
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   113
        """ if two numbers are given take start and stop.
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   114
        else take highest as default.
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
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   117
        range_str = self.cleaned_data['likeliness_of_attending'].strip()
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   118
        if not range_str:
25
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
   119
            return ("","")
24
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   120
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   121
        start_stop = range_str.split("-")
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
        if len(start_stop) == 1:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   124
            if start_stop[0].isdigit():
25
30baf3c635c5 added list statiscs page
nishanth
parents: 24
diff changeset
   125
                return (start_stop[0], "")
24
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   126
            else:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   127
                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
   128
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   129
        elif len(start_stop) == 2:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   130
            start, stop = start_stop
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   131
            if start.isdigit() and stop.isdigit():
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   132
                return (start, stop)
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   133
            else:
f79be1dd4a22 added clean methods for each attribute in search form
nishanth
parents: 21
diff changeset
   134
                 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
   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
                   
56
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   138
class EmailForm(forms.Form):
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   139
    """ Take a list of csv seperated email addresses.
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   140
    """
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
    emails = forms.CharField(widget=forms.Textarea, required=False)
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
    def clean_emails(self):
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   145
        emails = self.cleaned_data['emails']
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   146
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   147
        to_emails = [csv_list.split(',') for csv_list in emails.split()]
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   148
        return to_emails
7dfacad8adee provided an interface for sending out emails
nishanth
parents: 45
diff changeset
   149