from django.contrib.auth.models import User
from django.db import models
from django.utils import simplejson as json

from .signals import post_associate


class IdentityManager(models.Manager):

    def from_loginza_data(self, loginza_data):
        data = json.dumps(loginza_data)
        identity, created = self.get_or_create(
            identity = loginza_data['identity'],
            provider = loginza_data['provider'],
            defaults = {'data': data}
        )
        if not created:
            identity.data = data
            identity.save()
        return identity


class Identity(models.Model):

    identity = models.CharField(max_length=255)
    provider = models.CharField(max_length=255)
    user = models.ForeignKey(User, null=True)
    data = models.TextField()

    objects = IdentityManager()

    class Meta:
        unique_together = (('identity', 'provider'),)

    def associate(self, user):
        self.user = user
        self.save()
        post_associate.send(sender=type(self), instance=self)

    def create_user(self, username, email, password=None):
        existing_users = 0
        while True:
            existing_users += 1
            try:
                User.objects.get(username=username)
            except User.DoesNotExist:
                break
            username = '%s_%d' % (username, existing_users)
        user = User.objects.create_user(username, email, password)
        self.associate(user)
        return user

@@ -1114,16 +1114,44 @@
 ers = 0%0A
+        new_username = None%0A
         
@@ -1210,55 +1210,84 @@
     
-try:%0A                User.objects.get(username=
+qs = User.objects.all()%0A            qs = qs.filter(username=new_username or 
 user
@@ -1308,32 +1308,26 @@
     
-except User.DoesNotE
+if not qs.e
 xist
+s()
 :%0A  
@@ -1354,24 +1354,28 @@
             
+new_
 username = '
@@ -1450,16 +1450,32 @@
 te_user(
+new_username or 
 username

Fix user creation with unique username