From de48c33ec3cad602828d9406ffe2dd379cfa3c2e Mon Sep 17 00:00:00 2001 From: Weitian LI Date: Sat, 7 Jun 2014 11:58:26 +0800 Subject: Validate username field on registration * validate username with regex on registration (bootstrap3 frontent does not handle regex correctly. XXX) -> account/forms.py * small update to registration page title display style --- account/forms.py | 24 +++++++++++++++++++++++- templates/registration/registration_form.html | 4 +++- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/account/forms.py b/account/forms.py index daa6683..aa021ac 100644 --- a/account/forms.py +++ b/account/forms.py @@ -10,11 +10,13 @@ from django.contrib.auth.models import User from django.contrib.sites.models import Site, RequestSite from django.utils.translation import ugettext_lazy as _ -from registration.forms import RegistrationFormUniqueEmail +from registration.forms import RegistrationForm, RegistrationFormUniqueEmail from captcha.fields import ReCaptchaField from account.models import UserProfile, UserFile +import re + class UserRegForm(RegistrationFormUniqueEmail): """ @@ -44,6 +46,26 @@ class UserRegForm(RegistrationFormUniqueEmail): # 'identity', # ] + def clean_username(self): + """ + It is required to check whether the username matches the + specified regular expression. + Because the frontend does not correctly validate RegexField. + XXX: howto deal with 'RegexField' in 'django-bootstrap3' + """ + username = self.cleaned_data['username'] + # check whether matches + username_p = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_.@+-]{3,29}') + username_m = username_p.match(username) + if not username_m: + raise forms.ValidationError(_("4-30 characters. Start with letters and underscore. Contains letters, digits and _.@+- characters."), code='invalid') + # check whether exists + existing = User.objects.filter(username__iexact=username) + if existing.exists(): + raise forms.ValidationError(_("A user with that username already exists."), code='invalid') + else: + return username + class ResendEmailForm(forms.Form): """ diff --git a/templates/registration/registration_form.html b/templates/registration/registration_form.html index 61de0ff..1bf7642 100644 --- a/templates/registration/registration_form.html +++ b/templates/registration/registration_form.html @@ -9,7 +9,9 @@ {% block content %}
-

注册账户

+
+

注册账户

+

-- cgit v1.2.2