profile/forms.py
author Nishanth Amuluru <nishanth@fossee.in>
Fri, 07 Jan 2011 09:57:52 +0530
changeset 259 3efb40900b87
parent 254 5c000bf6f241
child 269 dde894b36370
permissions -rw-r--r--
added the url for creating a login
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
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
     2
import os
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
     3
import PIL
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
     4
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
     5
from django import forms
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
     6
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
     7
from registration.forms import RegistrationFormUniqueEmail
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
     8
from registration.models import RegistrationProfile
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
     9
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    10
from pytask.utils import make_key
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    11
from pytask.profile.models import GENDER_CHOICES, Profile
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    12
254
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 253
diff changeset
    13
class CustomRegistrationForm(RegistrationFormUniqueEmail):
252
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    14
    """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
    15
    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
    16
    django-registration RegistrationForm"""
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    17
254
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 253
diff changeset
    18
    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
    19
                              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
    20
                              reviewer in judging your eligibility for a task.\
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    21
                              It can have your educational background, CGPA,\
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    22
                              field of interests etc.,"
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    23
                             )
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    24
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    25
    
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    26
    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
    27
    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
    28
254
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 253
diff changeset
    29
    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
    30
                             information will be used while sending DD/Cheque")
254
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 253
diff changeset
    31
    phonenum = forms.CharField(required=True, max_length=10, 
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 253
diff changeset
    32
                               label="Phone Number")
252
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    33
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    34
    def clean_aboutme(self):
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    35
        """ Empty not allowed """
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    36
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    37
        data = self.cleaned_data['aboutme']
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    38
        if not data.strip():
254
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 253
diff changeset
    39
            raise forms.ValidationError("Please write something about\
252
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    40
                                        yourself")
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    41
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    42
        return data
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    43
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    44
    def clean_address(self):
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    45
        """ Empty not allowed """
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    46
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    47
        data = self.cleaned_data['address']
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    48
        if not data.strip():
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    49
            raise forms.ValidationError("Please enter an address")
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
        return data
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    52
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    53
    def clean_phonenum(self):
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    54
        """ should be of 10 digits """
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    55
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    56
        data = self.cleaned_data['phonenum']
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    57
254
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 253
diff changeset
    58
        if (not data.strip()) or \
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 253
diff changeset
    59
           (data.strip("1234567890")) or \
252
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    60
           (len(data)!= 10):
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    61
               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
    62
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    63
        return data
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    64
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    65
    
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    66
    def save(self,profile_callback=None):
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    67
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    68
        new_user = RegistrationProfile.objects.create_inactive_user(
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    69
                       username=self.cleaned_data['username'],
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    70
                       password=self.cleaned_data['password1'],
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    71
                       email=self.cleaned_data['email'])
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    72
        
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    73
        new_profile = Profile(user=new_user,
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    74
                              aboutme=self.cleaned_data['aboutme'],
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    75
                              dob=self.cleaned_data['dob'],
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    76
                              gender=self.cleaned_data['gender'],
254
5c000bf6f241 Fixed a few typos
Nishanth Amuluru <nishanth@fossee.in>
parents: 253
diff changeset
    77
                              address=self.cleaned_data['address'],
252
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    78
                              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
    79
                              uniq_key=make_key(Profile),
252
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    80
                             )
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    81
        new_profile.save()
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    82
        
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    83
        return new_user
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    84