From 20541d04865d99854d21b7a8f36e1dbe298f2ab3 Mon Sep 17 00:00:00 2001 From: Chris Beaven Date: Thu, 17 Feb 2011 17:42:13 +1300 Subject: [PATCH] Add a ACCOUNT_USERNAME_REQUIRED setting which, if false, generates a random username rather than prompting for one at signup --- allauth/account/app_settings.py | 3 +++ allauth/account/forms.py | 39 ++++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/allauth/account/app_settings.py b/allauth/account/app_settings.py index 911bd01022..70f95c9d8e 100644 --- a/allauth/account/app_settings.py +++ b/allauth/account/app_settings.py @@ -13,6 +13,9 @@ # Enforce uniqueness of e-mail addresses UNIQUE_EMAIL = getattr(settings, "ACCOUNT_UNIQUE_EMAIL", 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 diff --git a/allauth/account/forms.py b/allauth/account/forms.py index 1759bddd40..f2b17f72be 100644 --- a/allauth/account/forms.py +++ b/allauth/account/forms.py @@ -1,4 +1,6 @@ +import base64 import re +import uuid from django import forms from django.conf import settings @@ -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"] @@ -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: @@ -160,10 +178,11 @@ class SignupForm(BaseSignupForm): def __init__(self, *args, **kwargs): super(SignupForm, self).__init__(*args, **kwargs) - self.fields.keyOrder = ["username", - "password1", - "password2", - "email"] + if USERNAME_REQUIRED: + self.fields.keyOrder = ["username", "password1", "password2", + "email"] + else: + self.fields.keyOrder = ["email", "password1", "password2"] def clean(self): if "password1" in self.cleaned_data and "password2" in self.cleaned_data: