pytask/profile/forms.py
author Nishanth Amuluru <nishanth@fossee.in>
Sat, 08 Jan 2011 13:59:59 +0530
changeset 74 a8fbe291385c
parent 73 de27bb39375f
child 77 c63a9519dc37
permissions -rw-r--r--
Created a form for creating profile
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
16
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    21
    aboutme = forms.CharField(required=True, max_length=1000, label=u"About Me",
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    22
                              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
    23
                              reviewer in judging your eligibility for a task.\
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    24
                              It can have your educational background, CGPA,\
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    25
                              field of interests etc.,"
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    26
                             )
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
    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
    30
    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
    31
16
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    32
    address = forms.CharField(required=True, max_length=200, help_text="This \
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    33
                             information will be used while sending DD/Cheque")
16
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    34
    phonenum = forms.CharField(required=True, max_length=10, 
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    35
                               label="Phone Number")
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    36
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    37
    def clean_aboutme(self):
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    38
        """ Empty not allowed """
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    39
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    40
        data = self.cleaned_data['aboutme']
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    41
        if not data.strip():
16
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    42
            raise forms.ValidationError("Please write something about\
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    43
                                        yourself")
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    44
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    45
        return data
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
    def clean_address(self):
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    48
        """ Empty not allowed """
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    49
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    50
        data = self.cleaned_data['address']
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    51
        if not data.strip():
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    52
            raise forms.ValidationError("Please enter an address")
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    53
        
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    54
        return data
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
    def clean_phonenum(self):
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    57
        """ should be of 10 digits """
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    58
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    59
        data = self.cleaned_data['phonenum']
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    60
16
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    61
        if (not data.strip()) or \
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    62
           (data.strip("1234567890")) or \
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    63
           (len(data)!= 10):
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    64
               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
    65
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    66
        return data
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
    
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    69
    def save(self,profile_callback=None):
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
        new_user = RegistrationProfile.objects.create_inactive_user(
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    72
                       username=self.cleaned_data['username'],
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    73
                       password=self.cleaned_data['password1'],
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    74
                       email=self.cleaned_data['email'])
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    75
        
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    76
        new_profile = Profile(user=new_user,
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    77
                              aboutme=self.cleaned_data['aboutme'],
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    78
                              dob=self.cleaned_data['dob'],
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    79
                              gender=self.cleaned_data['gender'],
16
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 15
diff changeset
    80
                              address=self.cleaned_data['address'],
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    81
                              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
    82
                              uniq_key=make_key(Profile),
14
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    83
                             )
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    84
        new_profile.save()
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
        return new_user
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 9
diff changeset
    87
74
a8fbe291385c Created a form for creating profile
Nishanth Amuluru <nishanth@fossee.in>
parents: 73
diff changeset
    88
class CreateProfileForm(forms.ModelForm):
a8fbe291385c Created a form for creating profile
Nishanth Amuluru <nishanth@fossee.in>
parents: 73
diff changeset
    89
a8fbe291385c Created a form for creating profile
Nishanth Amuluru <nishanth@fossee.in>
parents: 73
diff changeset
    90
    class Meta:
a8fbe291385c Created a form for creating profile
Nishanth Amuluru <nishanth@fossee.in>
parents: 73
diff changeset
    91
        model = Profile
a8fbe291385c Created a form for creating profile
Nishanth Amuluru <nishanth@fossee.in>
parents: 73
diff changeset
    92
        fields = ['full_name', 'aboutme', 'gender', 'dob', 'address', 'phonenum']
a8fbe291385c Created a form for creating profile
Nishanth Amuluru <nishanth@fossee.in>
parents: 73
diff changeset
    93
31
dde894b36370 created view for editing profile and created corresponding template
Nishanth Amuluru <nishanth@fossee.in>
parents: 16
diff changeset
    94
class EditProfileForm(forms.ModelForm):
dde894b36370 created view for editing profile and created corresponding template
Nishanth Amuluru <nishanth@fossee.in>
parents: 16
diff changeset
    95
dde894b36370 created view for editing profile and created corresponding template
Nishanth Amuluru <nishanth@fossee.in>
parents: 16
diff changeset
    96
    class Meta:
dde894b36370 created view for editing profile and created corresponding template
Nishanth Amuluru <nishanth@fossee.in>
parents: 16
diff changeset
    97
        model = Profile
62
ce5f3cdbd545 Used the new field in forms
Nishanth Amuluru <nishanth@fossee.in>
parents: 31
diff changeset
    98
        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
    99
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   100
    def clean_aboutme(self):
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   101
        """ Empty not allowed """
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   102
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   103
        data = self.cleaned_data['aboutme']
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   104
        if not data.strip():
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   105
            raise forms.ValidationError("Please write something about\
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   106
                                        yourself")
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   107
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   108
        return data
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
    def clean_address(self):
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   111
        """ Empty not allowed """
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   112
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   113
        data = self.cleaned_data['address']
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   114
        if not data.strip():
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   115
            raise forms.ValidationError("Please enter an address")
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   116
        
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   117
        return data
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
    def clean_phonenum(self):
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   120
        """ should be of 10 digits """
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   121
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   122
        data = self.cleaned_data['phonenum']
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
        if (not data.strip()) or \
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   125
           (data.strip("1234567890")) or \
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   126
           (len(data)!= 10):
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   127
               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
   128
de27bb39375f Added clean methods to edit profile form
Nishanth Amuluru <nishanth@fossee.in>
parents: 69
diff changeset
   129
        return data