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