pytask/profile/forms.py
changeset 520 958eb8854b63
parent 478 863dba311ba2
equal deleted inserted replaced
519:84709567f47a 520:958eb8854b63
     1 import os
     1 import os
       
     2 import re
     2 
     3 
     3 from django import forms
     4 from django import forms
       
     5 from django.utils.translation import ugettext
     4 
     6 
     5 from registration.forms import RegistrationFormUniqueEmail
     7 from registration.forms import RegistrationFormUniqueEmail
       
     8 from registration.forms import attrs_dict
     6 from registration.models import RegistrationProfile
     9 from registration.models import RegistrationProfile
     7 
    10 
     8 from pytask.profile.models import GENDER_CHOICES, Profile
    11 from pytask.profile.models import GENDER_CHOICES, Profile
     9 
    12 
    10 class CustomRegistrationForm(RegistrationFormUniqueEmail):
    13 class CustomRegistrationForm(RegistrationFormUniqueEmail):
    11     """Used instead of RegistrationForm used by default django-registration
    14     """Used instead of RegistrationForm used by default django-registration
    12     backend, this adds aboutme, dob, gender, address, phonenum to the default 
    15     backend, this adds aboutme, dob, gender, address, phonenum to the default 
    13     django-registration RegistrationForm"""
    16     django-registration RegistrationForm"""
       
    17 
       
    18     # overriding only this field from the parent Form Class since we 
       
    19     # don't like the restriction imposed by the registration app on username
       
    20     # GMail has more or less set the standard for user names
       
    21     username = forms.CharField(
       
    22       max_length=30, widget=forms.TextInput(attrs=attrs_dict),
       
    23       label=ugettext('Username'), help_text='Username can contain alphabet, '
       
    24       'numbers or special characters underscore (_) and (.)')
    14 
    25 
    15     full_name = forms.CharField(required=True, max_length=50, 
    26     full_name = forms.CharField(required=True, max_length=50, 
    16                                 label="Name as on your bank account", 
    27                                 label="Name as on your bank account", 
    17                                 help_text="Any DD/Cheque will be issued on \
    28                                 help_text="Any DD/Cheque will be issued on \
    18                                            this name")
    29                                            this name")
    36       required=True, max_length=200, widget=forms.Textarea,
    47       required=True, max_length=200, widget=forms.Textarea,
    37       help_text="This information will be used while sending DD/Cheque")
    48       help_text="This information will be used while sending DD/Cheque")
    38 
    49 
    39     phonenum = forms.CharField(required=True, max_length=10, 
    50     phonenum = forms.CharField(required=True, max_length=10, 
    40                                label="Phone Number")
    51                                label="Phone Number")
       
    52 
       
    53     def clean_username(self):
       
    54         """Add additional cleaner for username than the parent class
       
    55         supplied cleaner.
       
    56         """
       
    57 
       
    58         username = self.cleaned_data['username']
       
    59 
       
    60         # None of the regular expression works better than this custom
       
    61         # username check.
       
    62         if not re.match(r'^\w+', username):
       
    63             raise forms.ValidationError(
       
    64               ugettext('Username can start only with an alphabet or a number'))
       
    65         elif not re.search(r'\w+$', username):
       
    66             raise forms.ValidationError(
       
    67               ugettext('Username can end only with an alphabet or a number'))
       
    68         elif re.search(r'\.\.+', username):
       
    69             raise forms.ValidationError(
       
    70               ugettext('Username cannot not have consecutive periods(.)'))
       
    71 
       
    72         return super(CustomRegistrationForm, self).clean_username()
    41 
    73 
    42     def clean_aboutme(self):
    74     def clean_aboutme(self):
    43         """ Empty not allowed """
    75         """ Empty not allowed """
    44 
    76 
    45         data = self.cleaned_data['aboutme']
    77         data = self.cleaned_data['aboutme']