Skip to content

Commit

Permalink
Merged SmileyChris' support for ACCOUNT_USERNAME_REQUIRED
Browse files Browse the repository at this point in the history
  • Loading branch information
pennersr committed May 29, 2011
2 parents 80a336c + 20541d0 commit 6a731ce
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
6 changes: 5 additions & 1 deletion ChangeLog
@@ -1,4 +1,8 @@
2011-05-29 Raymond Penners <pennersr@ubuntosx>
2011-05-29 Raymond Penners <raymond.penners@intenct.nl>

* SmileyChris contributed support for automatically generating a
user name at signup when ACCOUNT_USERNAME_REQUIRED is set to
False.

* Vuong Nguyen contributed support for (optionally) asking for the
password just once during signup
Expand Down
4 changes: 4 additions & 0 deletions README.rst
Expand Up @@ -65,6 +65,10 @@ ACCOUNT_SIGNUP_PASSWORD_VERIFICATION (=True)
ACCOUNT_UNIQUE_EMAIL (=True)
Enforce uniqueness of e-mail addresses

ACCOUNT_USERNAME_REQUIRED (=True)
If false, generates a random username rather than prompting for one
at signup

SOCIALACCOUNT_QUERY_EMAIL (=ACCOUNT_EMAIL_REQUIRED)
Request e-mail address from 3rd party account provider? E.g. using
OpenID AX, or the Facebook "email" permission
Expand Down
3 changes: 3 additions & 0 deletions allauth/account/app_settings.py
Expand Up @@ -16,6 +16,9 @@
# Signup password verification
SIGNUP_PASSWORD_VERIFICATION = getattr(settings, "ACCOUNT_SIGNUP_PASSWORD_VERIFICATION", True)

# The user is required to enter a username when signing up
USERNAME_REQUIRED = getattr(settings, "ACCOUNT_USERNAME_REQUIRED", True)

assert (not EMAIL_AUTHENTICATION) or EMAIL_REQUIRED
assert (not EMAIL_AUTHENTICATION) or UNIQUE_EMAIL
assert (not EMAIL_VERIFICATION) or EMAIL_REQUIRED
Expand Down
36 changes: 29 additions & 7 deletions allauth/account/forms.py
@@ -1,4 +1,6 @@
import base64
import re
import uuid

from django import forms
from django.conf import settings
Expand Down Expand Up @@ -112,15 +114,23 @@ def __init__(self, *args, **kwargs):
else:
self.fields["email"].label = ugettext("E-mail (optional)")
self.fields["email"].required = False
if not USERNAME_REQUIRED:
del self.fields["username"]

def random_username(self):
return base64.urlsafe_b64encode(uuid.uuid4().bytes).strip('=')

def clean_username(self):
if not alnum_re.search(self.cleaned_data["username"]):
raise forms.ValidationError(_("Usernames can only contain letters, numbers and underscores."))
value = self.cleaned_data["username"]
if not alnum_re.search(value):
raise forms.ValidationError(_("Usernames can only contain "
"letters, numbers and underscores."))
try:
user = User.objects.get(username__iexact=self.cleaned_data["username"])
User.objects.get(username__iexact=value)
except User.DoesNotExist:
return self.cleaned_data["username"]
raise forms.ValidationError(_("This username is already taken. Please choose another."))
return value
raise forms.ValidationError(_("This username is already taken. Please "
"choose another."))

def clean_email(self):
value = self.cleaned_data["email"]
Expand All @@ -132,7 +142,15 @@ def clean_email(self):

def create_user(self, commit=True):
user = User()
user.username = self.cleaned_data["username"]
if USERNAME_REQUIRED:
user.username = self.cleaned_data["username"]
else:
while True:
user.username = self.random_username()
try:
User.objects.get(username=user.username)
except User.DoesNotExist:
break
user.email = self.cleaned_data["email"].strip().lower()
user.set_unusable_password()
if EMAIL_VERIFICATION:
Expand Down Expand Up @@ -164,8 +182,12 @@ def __init__(self, *args, **kwargs):
"password1",
"password2",
"email"]
if not USERNAME_REQUIRED:
self.fields.keyOrder = ["email",
"password1",
"password2"]
if not SIGNUP_PASSWORD_VERIFICATION:
del self.fields['password2']
del self.fields["password2"]

def clean(self):
if SIGNUP_PASSWORD_VERIFICATION \
Expand Down

0 comments on commit 6a731ce

Please sign in to comment.