profile/forms.py
author Nishanth Amuluru <nishanth@fossee.in>
Fri, 07 Jan 2011 08:56:24 +0530
changeset 252 5a92fcacdd5a
parent 247 2ba2ef66a482
child 253 beb830b0e744
permissions -rw-r--r--
Created a form for user creation
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
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    13
class RegistrationFormCustom(RegistrationFormUniqueEmail):
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
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    18
    aboutme = forms.TextField(required=True, max_length=1000, label=u"About Me",
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
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    29
    address = forms.TextField(required=True, max_length=200, help_text="This \
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")
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    31
    phonenum = forms.TextField(required=True, max_length=10. label="Phone
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    32
                               Number")
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():
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    39
            raise forms.ValidationError("Please write something about
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
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    58
        if (not data.strip()) or 
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    59
           (data.strip("1234567890")) or 
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'],
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    77
                              address=self.cleaned_data['address']
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    78
                              phonenum=self.cleaned_data['phonenum'],
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    79
                             )
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    80
        new_profile.save()
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    81
        
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    82
        return new_user
5a92fcacdd5a Created a form for user creation
Nishanth Amuluru <nishanth@fossee.in>
parents: 247
diff changeset
    83