From: Matías Aguirre Date: Wed, 4 May 2011 18:21:32 +0000 (-0300) Subject: Move UUID max length to settings. Closes gh-62 X-Git-Url: https://git.parisson.com/?a=commitdiff_plain;h=0c885b94c3f3c9a7fd8db49cafe33fa97043af1e;p=django-social-auth.git Move UUID max length to settings. Closes gh-62 --- diff --git a/README.rst b/README.rst index cb3e37d..c065668 100644 --- a/README.rst +++ b/README.rst @@ -199,7 +199,10 @@ Configuration in case your user layout needs to purify username on some weird way. - Final user name will have an integer suffix in case it's already taken. + Final user name will have a random UUID-generated suffix in case it's already + taken. The UUID token max length can be changed with the setting:: + + SOCIAL_AUTH_UUID_LENGTH = 16 - Backends will store extra values from response by default, set this to False to avoid such behavior:: diff --git a/doc/configuration.rst b/doc/configuration.rst index 5330fe5..6248972 100644 --- a/doc/configuration.rst +++ b/doc/configuration.rst @@ -110,7 +110,10 @@ Configuration in case your user layout needs to purify username on some weird way. - Final user name will have an integer suffix in case it's already taken. + Final user name will have a random UUID-generated suffix in case it's already + taken. The UUID token max length can be changed with the setting:: + + SOCIAL_AUTH_UUID_LENGTH = 16 - Backends will store extra values from response by default, set this to False to avoid such behavior:: diff --git a/social_auth/backends/__init__.py b/social_auth/backends/__init__.py index 76d3dc7..4eb1f15 100644 --- a/social_auth/backends/__init__.py +++ b/social_auth/backends/__init__.py @@ -65,8 +65,6 @@ USERNAME = 'username' User = UserSocialAuth._meta.get_field('user').rel.to # username field max length USERNAME_MAX_LENGTH = User._meta.get_field(USERNAME).max_length -# uuid hex characters to keep while generating unique usernames -UUID_MAX_LENGTH = 16 # a few settings values def _setting(name, default=None): @@ -192,9 +190,10 @@ class SocialAuthBackend(ModelBackend): # breaking the field max_length value, this reduces the # uniqueness, but it's less likely to happen repetitions than # increasing an index. - if len(username) + UUID_MAX_LENGTH > USERNAME_MAX_LENGTH: - username = username[:USERNAME_MAX_LENGTH - UUID_MAX_LENGTH] - name = username + uuid4().get_hex()[:UUID_MAX_LENGTH] + uuid_length = getattr(settings, 'SOCIAL_AUTH_UUID_LENGTH', 16) + if len(username) + uuid_length > USERNAME_MAX_LENGTH: + username = username[:USERNAME_MAX_LENGTH - uuid_length] + name = username + uuid4().get_hex()[:uuid_length] return final_username