Ticket #5786: form.1.patch

File form.1.patch, 3.6 KB (added by Bob Gao, 17 years ago)

an improved version

  • django/conf/global_settings.py

     
    335335
    336336LOGIN_REDIRECT_URL = '/accounts/profile/'
    337337
     338AUTH_USERNAME_PATTERN = r'^\w+$'
    338339###########
    339340# TESTING #
    340341###########
  • django/contrib/auth/forms.py

     
    66from django import oldforms
    77from django.utils.translation import ugettext as _
    88
     9import re
     10from django.conf import settings
     11
    912class UserCreationForm(oldforms.Manipulator):
    1013    "A form that creates a user, with no privileges, from the given username and password."
    1114    def __init__(self):
    1215        self.fields = (
    1316            oldforms.TextField(field_name='username', length=30, max_length=30, is_required=True,
    14                 validator_list=[validators.isAlphaNumeric, self.isValidUsername]),
     17                validator_list=[self.isValidUsername]),
    1518            oldforms.PasswordField(field_name='password1', length=30, max_length=60, is_required=True),
    1619            oldforms.PasswordField(field_name='password2', length=30, max_length=60, is_required=True,
    1720                validator_list=[validators.AlwaysMatchesOtherField('password1', _("The two password fields didn't match."))]),
    1821        )
    1922
    2023    def isValidUsername(self, field_data, all_data):
     24        if not re.search(getattr(settings, 'AUTH_USERNAME_PATTERN'), field_data, re.U):
     25            raise validators.ValidationError, _('Username contains illegal characters.')
     26
    2127        try:
    2228            User.objects.get(username=field_data)
    2329        except User.DoesNotExist:
    24             return
     30            return     
    2531        raise validators.ValidationError, _('A user with that username already exists.')
    2632
    2733    def save(self, new_data):
  • django/contrib/auth/tests.py

     
    2323[]
    2424>>> a.user_permissions.all()
    2525[]
    26 """
    27  No newline at end of file
     26>>> from django.contrib.auth.forms import *
     27>>> cf = UserCreationForm()
     28>>> cf.isValidUsername("aa", '')
     29>>> cf.isValidUsername("b!", '')
     30Traceback (most recent call last):
     31...
     32ValidationError: [u'Username contains illegal characters.']
     33   
     34"""
  • docs/settings.txt

     
    225225``CommonMiddleware`` is installed (see the `middleware docs`_). See also
    226226``PREPEND_WWW``.
    227227
     228AUTH_USERNAME_PATTERN
     229---------------------
     230
     231Default: r''^\w+$''
     232
     233A pattern which the username in auth moduel has to match, which can be coded with UTF-8.
     234
    228235AUTHENTICATION_BACKENDS
    229236-----------------------
    230237
  • docs/tutorial02.txt

     
    359359filters, date-hierarchies and column-header-ordering all work together like you
    360360think they should.
    361361
     362Customize the username pattern
     363======================================
     364
     365Open your settings file (``mysite/settings.py``, remember) and look at the
     366``AUTH_USERNAME_PATTERN`` setting. The default value is ''^\w+$'', which you
     367may change it to any pattern in UTF-8, for example, '^ab.*$'.
     368
    362369Customize the admin look and feel
    363370=================================
    364371
Back to Top