pytask/profile/forms.py
author Madhusudan.C.S <madhusudancs@gmail.com>
Tue, 18 Jan 2011 17:34:29 +0530
changeset 463 c7c595c0bed3
parent 412 ddd52b52dd9d
child 466 8ecd503354de
permissions -rw-r--r--
Make changes to the code style so that it is consistent across the code base.

import os

from django import forms

from registration.forms import RegistrationFormUniqueEmail
from registration.models import RegistrationProfile

from pytask.profile.models import GENDER_CHOICES, Profile

class CustomRegistrationForm(RegistrationFormUniqueEmail):
    """Used instead of RegistrationForm used by default django-registration
    backend, this adds aboutme, dob, gender, address, phonenum to the default 
    django-registration RegistrationForm"""

    full_name = forms.CharField(required=True, max_length=50, 
                                label="Name as on your bank account", 
                                help_text="Any DD/Cheque will be issued on \
                                           this name")

    aboutme = forms.CharField(required=True, widget=forms.Textarea, 
                              max_length=1000, label=u"About Me",
                              help_text="A write up about yourself to aid the\
                              reviewer in judging your eligibility for a task.\
                              It can have your educational background, CGPA,\
                              field of interests etc.,"
                             )

    
    dob = forms.DateField(help_text = "YYYY-MM-DD", required=True,
                          label=u'Date of birth')

    gender = forms.ChoiceField(choices = GENDER_CHOICES,
                               required=True, label=u'Gender')

    address = forms.CharField(
      required=True, max_length=200, widget=forms.Textarea,
      help_text="This information will be used while sending DD/Cheque")

    phonenum = forms.CharField(required=True, max_length=10, 
                               label="Phone Number")

    def clean_aboutme(self):
        """ Empty not allowed """

        data = self.cleaned_data['aboutme']
        if not data.strip():
            raise forms.ValidationError("Please write something about\
                                        yourself")

        return data

    def clean_address(self):
        """ Empty not allowed """

        data = self.cleaned_data['address']
        if not data.strip():
            raise forms.ValidationError("Please enter an address")
        
        return data

    def clean_phonenum(self):
        """ should be of 10 digits """

        data = self.cleaned_data['phonenum']

        if (not data.strip()) or \
           (data.strip("1234567890")) or \
           (len(data)!= 10):
               raise forms.ValidationError("This is not a valid phone number")

        return data

    
    def save(self,profile_callback=None):

        new_user = RegistrationProfile.objects.create_inactive_user(
                       username=self.cleaned_data['username'],
                       password=self.cleaned_data['password1'],
                       email=self.cleaned_data['email'])
        
        new_profile = Profile(user=new_user,
                              aboutme=self.cleaned_data['aboutme'],
                              dob=self.cleaned_data['dob'],
                              gender=self.cleaned_data['gender'],
                              address=self.cleaned_data['address'],
                              phonenum=self.cleaned_data['phonenum'],
                              uniq_key=make_key(Profile),
                             )
        new_profile.save()
        
        return new_user

class CreateProfileForm(forms.ModelForm):

    class Meta:
        model = Profile
        exclude = ['pynts', 'rights']

class EditProfileForm(forms.ModelForm):

    class Meta:
        model = Profile
        fields = ['full_name', 'aboutme', 'gender', 'dob', 'address', 'phonenum']

    def clean_aboutme(self):
        """ Empty not allowed """

        data = self.cleaned_data['aboutme']
        if not data.strip():
            raise forms.ValidationError("Please write something about\
                                        yourself")

        return data

    def clean_address(self):
        """ Empty not allowed """

        data = self.cleaned_data['address']
        if not data.strip():
            raise forms.ValidationError("Please enter an address")
        
        return data

    def clean_phonenum(self):
        """ should be of 10 digits """

        data = self.cleaned_data['phonenum']

        if (not data.strip()) or \
           (data.strip("1234567890")) or \
           (len(data)!= 10):
               raise forms.ValidationError("This is not a valid phone number")

        return data