diff options
author | Weitian LI <liweitianux@gmail.com> | 2014-06-07 11:58:26 +0800 |
---|---|---|
committer | Weitian LI <liweitianux@gmail.com> | 2014-06-07 11:58:26 +0800 |
commit | de48c33ec3cad602828d9406ffe2dd379cfa3c2e (patch) | |
tree | f5f8bad29d34d292370f54beb50bba71c7c263ca | |
parent | 49c28ef12fb1ebf5221feeeb22f955f97508f7d4 (diff) | |
download | django-skaschool-de48c33ec3cad602828d9406ffe2dd379cfa3c2e.tar.bz2 |
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
-rw-r--r-- | account/forms.py | 24 | ||||
-rw-r--r-- | 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 %} <div class="container"> - <h2>注册账户</h2> + <div class="col-md-offset-2"> + <h2>注册账户</h2> + </div> <br> <form role="form" class="form-horizontal" method="post"> |