Override the username field provided by Registration app.
authorMadhusudan.C.S <madhusudancs@gmail.com>
Fri, 28 Jan 2011 02:27:56 +0530
changeset 520 958eb8854b63
parent 519 84709567f47a
child 521 fe1914114802
Override the username field provided by Registration app.
pytask/profile/forms.py
--- a/pytask/profile/forms.py	Fri Jan 28 02:27:40 2011 +0530
+++ b/pytask/profile/forms.py	Fri Jan 28 02:27:56 2011 +0530
@@ -1,8 +1,11 @@
 import os
+import re
 
 from django import forms
+from django.utils.translation import ugettext
 
 from registration.forms import RegistrationFormUniqueEmail
+from registration.forms import attrs_dict
 from registration.models import RegistrationProfile
 
 from pytask.profile.models import GENDER_CHOICES, Profile
@@ -12,6 +15,14 @@
     backend, this adds aboutme, dob, gender, address, phonenum to the default 
     django-registration RegistrationForm"""
 
+    # overriding only this field from the parent Form Class since we 
+    # don't like the restriction imposed by the registration app on username
+    # GMail has more or less set the standard for user names
+    username = forms.CharField(
+      max_length=30, widget=forms.TextInput(attrs=attrs_dict),
+      label=ugettext('Username'), help_text='Username can contain alphabet, '
+      'numbers or special characters underscore (_) and (.)')
+
     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 \
@@ -39,6 +50,27 @@
     phonenum = forms.CharField(required=True, max_length=10, 
                                label="Phone Number")
 
+    def clean_username(self):
+        """Add additional cleaner for username than the parent class
+        supplied cleaner.
+        """
+
+        username = self.cleaned_data['username']
+
+        # None of the regular expression works better than this custom
+        # username check.
+        if not re.match(r'^\w+', username):
+            raise forms.ValidationError(
+              ugettext('Username can start only with an alphabet or a number'))
+        elif not re.search(r'\w+$', username):
+            raise forms.ValidationError(
+              ugettext('Username can end only with an alphabet or a number'))
+        elif re.search(r'\.\.+', username):
+            raise forms.ValidationError(
+              ugettext('Username cannot not have consecutive periods(.)'))
+
+        return super(CustomRegistrationForm, self).clean_username()
+
     def clean_aboutme(self):
         """ Empty not allowed """