changeset 119:479af7759c6b

Improved the way User objects are created so username collisions don't occur.
author Atul Varma <varmaa@toolness.com>
date Tue, 01 Apr 2008 13:05:35 -0700
parents 6e74f85e3dcb
children 5efa3fdb20de
files PersonasBackend/weaveproxyauth.py
diffstat 1 files changed, 17 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/PersonasBackend/weaveproxyauth.py	Tue Apr 01 12:12:59 2008 -0700
+++ b/PersonasBackend/weaveproxyauth.py	Tue Apr 01 13:05:35 2008 -0700
@@ -27,13 +27,24 @@
                 raise
 
         if auth_successful:
-            # Django auth doesn't like @'s and .'s, so...
-            munged_username = username.replace("@", "_").replace(".", "_")
+            try:
+                user = User.objects.get(email=username)
+            except User.DoesNotExist:
+                # We have to go through this annoyance because Django
+                # auth requires us to enter a username, and the username
+                # can be no more than 30 characters and can't include
+                # anything other than alphanumeric characters.
+                munged_username = "".join(
+                    [char for char in username if char.isalnum()]
+                    )[:25]
 
-            try:
-                user = User.objects.get(username=munged_username)
-            except User.DoesNotExist:
-                user = User(username=munged_username,
+                offset = 0
+                while User.objects.filter(
+                    username = munged_username + str(offset)
+                    ):
+                    offset += 1
+
+                user = User(username=munged_username + str(offset),
                             password="Retrieved from Weave",
                             email=username)
                 user.save()