diff --git a/ChangeLog b/ChangeLog index 145f9132d9..713ae694ed 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,4 +1,8 @@ -2011-05-29 Raymond Penners +2011-05-29 Raymond Penners + + * 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 diff --git a/README.rst b/README.rst index df90eaa234..75c58dbc2d 100644 --- a/README.rst +++ b/README.rst @@ -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 diff --git a/allauth/account/app_settings.py b/allauth/account/app_settings.py index d7d9df3d51..1011503630 100644 --- a/allauth/account/app_settings.py +++ b/allauth/account/app_settings.py @@ -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 diff --git a/allauth/account/forms.py b/allauth/account/forms.py index 1005d598df..04ba4b5b4e 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: @@ -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 \