pytask/profile/forms.py
author Nishanth Amuluru <nishanth@fossee.in>
Sun, 09 Jan 2011 19:45:42 +0530
changeset 129 13e171f09941
parent 81 70726699ca80
permissions -rw-r--r--
added unpublished tasks to home_page
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
     1
import os
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
     2
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
     3
from django import forms
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
     4
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
     5
from registration.forms import RegistrationFormUniqueEmail
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
     6
from registration.models import RegistrationProfile
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
     7
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
     8
from pytask.utils import make_key
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
     9
from pytask.profile.models import GENDER_CHOICES, Profile
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    10
16
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    11
class CustomRegistrationForm(RegistrationFormUniqueEmail):
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    12
    """Used instead of RegistrationForm used by default django-registration
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    13
    backend, this adds aboutme, dob, gender, address, phonenum to the default 
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    14
    django-registration RegistrationForm"""
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    15
62
ce5f3cdbd545 Used the new field in forms
Nishanth Amuluru <nishanth@fossee.in>
parents: 31
diff changeset
    16
    full_name = forms.CharField(required=True, max_length=50, 
ce5f3cdbd545 Used the new field in forms
Nishanth Amuluru <nishanth@fossee.in>
parents: 31
diff changeset
    17
                                label="Name as on your bank account", 
ce5f3cdbd545 Used the new field in forms
Nishanth Amuluru <nishanth@fossee.in>
parents: 31
diff changeset
    18
                                help_text="Any DD/Cheque will be issued on \
ce5f3cdbd545 Used the new field in forms
Nishanth Amuluru <nishanth@fossee.in>
parents: 31
diff changeset
    19
                                           this name")
ce5f3cdbd545 Used the new field in forms
Nishanth Amuluru <nishanth@fossee.in>
parents: 31
diff changeset
    20
81
70726699ca80 now the text area is displayed in a more elegant way
Nishanth Amuluru <nishanth@fossee.in>
parents: 77
diff changeset
    21
    aboutme = forms.CharField(required=True, widget=forms.Textarea, 
70726699ca80 now the text area is displayed in a more elegant way
Nishanth Amuluru <nishanth@fossee.in>
parents: 77
diff changeset
    22
                              max_length=1000, label=u"About Me",
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    23
                              help_text="A write up about yourself to aid the\
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    24
                              reviewer in judging your eligibility for a task.\
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    25
                              It can have your educational background, CGPA,\
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    26
                              field of interests etc.,"
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    27
                             )
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    28
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    29
    
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    30
    dob = forms.DateField(help_text = "YYYY-MM-DD", required=True, label=u'date of birth')
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    31
    gender = forms.ChoiceField(choices = GENDER_CHOICES, required=True, label=u'gender')
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    32
81
70726699ca80 now the text area is displayed in a more elegant way
Nishanth Amuluru <nishanth@fossee.in>
parents: 77
diff changeset
    33
    address = forms.CharField(required=True, max_length=200,
70726699ca80 now the text area is displayed in a more elegant way
Nishanth Amuluru <nishanth@fossee.in>
parents: 77
diff changeset
    34
                              widget=forms.Textarea, help_text="This \
70726699ca80 now the text area is displayed in a more elegant way
Nishanth Amuluru <nishanth@fossee.in>
parents: 77
diff changeset
    35
                              information will be used while sending DD/Cheque")
16
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    36
    phonenum = forms.CharField(required=True, max_length=10, 
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    37
                               label="Phone Number")
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    38
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    39
    def clean_aboutme(self):
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    40
        """ Empty not allowed """
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    41
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    42
        data = self.cleaned_data['aboutme']
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    43
        if not data.strip():
16
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    44
            raise forms.ValidationError("Please write something about\
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    45
                                        yourself")
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    46
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    47
        return data
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    48
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    49
    def clean_address(self):
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    50
        """ Empty not allowed """
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    51
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    52
        data = self.cleaned_data['address']
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    53
        if not data.strip():
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    54
            raise forms.ValidationError("Please enter an address")
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    55
        
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    56
        return data
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    57
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    58
    def clean_phonenum(self):
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    59
        """ should be of 10 digits """
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    60
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    61
        data = self.cleaned_data['phonenum']
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    62
16
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    63
        if (not data.strip()) or \
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    64
           (data.strip("1234567890")) or \
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    65
           (len(data)!= 10):
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    66
               raise forms.ValidationError("This is not a valid phone number")
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    67
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    68
        return data
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    69
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    70
    
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    71
    def save(self,profile_callback=None):
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    72
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    73
        new_user = RegistrationProfile.objects.create_inactive_user(
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    74
                       username=self.cleaned_data['username'],
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    75
                       password=self.cleaned_data['password1'],
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    76
                       email=self.cleaned_data['email'])
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    77
        
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    78
        new_profile = Profile(user=new_user,
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    79
                              aboutme=self.cleaned_data['aboutme'],
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    80
                              dob=self.cleaned_data['dob'],
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    81
                              gender=self.cleaned_data['gender'],
16
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    82
                              address=self.cleaned_data['address'],
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    83
                              phonenum=self.cleaned_data['phonenum'],
15
beb830b0e744 modified make_key and used it in the form
Nishanth Amuluru <nishanth@fossee.in>
parents: 14
diff changeset
    84
                              uniq_key=make_key(Profile),
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    85
                             )
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    86
        new_profile.save()
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    87
        
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    88
        return new_user
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    89
74
a8fbe291385c Created a form for creating profile
Nishanth Amuluru <nishanth@fossee.in>
parents: 73
diff changeset
    90
class CreateProfileForm(forms.ModelForm):
a8fbe291385c Created a form for creating profile
Nishanth Amuluru <nishanth@fossee.in>
parents: 73
diff changeset
    91
a8fbe291385c Created a form for creating profile
Nishanth Amuluru <nishanth@fossee.in>
parents: 73
diff changeset
    92
    class Meta:
a8fbe291385c Created a form for creating profile
Nishanth Amuluru <nishanth@fossee.in>
parents: 73
diff changeset
    93
        model = Profile
77
c63a9519dc37 fixed 2 bugs
Nishanth Amuluru <nishanth@fossee.in>
parents: 74
diff changeset
    94
        exclude = ['pynts', 'rights']
74
a8fbe291385c Created a form for creating profile
Nishanth Amuluru <nishanth@fossee.in>
parents: 73
diff changeset
    95
31
dde894b36370 created view for editing profile and created corresponding template
Nishanth Amuluru <nishanth@fossee.in>
parents: 16
diff changeset
    96
class EditProfileForm(forms.ModelForm):
dde894b36370 created view for editing profile and created corresponding template
Nishanth Amuluru <nishanth@fossee.in>
parents: 16
diff changeset
    97
dde894b36370 created view for editing profile and created corresponding template
Nishanth Amuluru <nishanth@fossee.in>
parents: 16
diff changeset
    98
    class Meta:
dde894b36370 created view for editing profile and created corresponding template
Nishanth Amuluru <nishanth@fossee.in>
parents: 16
diff changeset
    99
        model = Profile
62
ce5f3cdbd545 Used the new field in forms
Nishanth Amuluru <nishanth@fossee.in>
parents: 31
diff changeset
   100
        fields = ['full_name', 'aboutme', 'gender', 'dob', 'address', 'phonenum']
73
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   101
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   102
    def clean_aboutme(self):
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   103
        """ Empty not allowed """
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   104
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   105
        data = self.cleaned_data['aboutme']
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   106
        if not data.strip():
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   107
            raise forms.ValidationError("Please write something about\
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   108
                                        yourself")
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   109
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   110
        return data
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   111
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   112
    def clean_address(self):
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   113
        """ Empty not allowed """
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   114
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   115
        data = self.cleaned_data['address']
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   116
        if not data.strip():
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   117
            raise forms.ValidationError("Please enter an address")
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   118
        
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   119
        return data
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   120
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   121
    def clean_phonenum(self):
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   122
        """ should be of 10 digits """
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   123
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   124
        data = self.cleaned_data['phonenum']
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   125
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   126
        if (not data.strip()) or \
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   127
           (data.strip("1234567890")) or \
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   128
           (len(data)!= 10):
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   129
               raise forms.ValidationError("This is not a valid phone number")
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   130
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   131
        return data