import uuid

from django.contrib.auth.models import User


class UserTestsMixin(object):
    """ Mixin base class for tests requiring users. """

    def create_user(self, username=None, password=None):
        """ Create, save and return a new user. """

        if not username:
            # Generate a random username
            username = str(uuid.uuid4())[:30]

        user = User.objects.create_user(username=username)

        return user



import uuid

from django.contrib.auth.models import User


class UserTestsMixin(object):
    """ Mixin base class for tests requiring users. """

    def create_user(self, username=None, password=None):
        """ Create, save and return a new user. """

        # If no username is set, create a random unique username
        while not username or User.objects.filter(username=username).exists():
            # Generate a random username
            username = str(uuid.uuid4())[:30]

        user = User.objects.create_user(username=username)

        return user



Make sure generated usernames are unique.
Make sure generated usernames are unique.

  import uuid
  
  from django.contrib.auth.models import User
  
  
  class UserTestsMixin(object):
      """ Mixin base class for tests requiring users. """
  
      def create_user(self, username=None, password=None):
          """ Create, save and return a new user. """
  
-         if not username:
+         # If no username is set, create a random unique username
+         while not username or User.objects.filter(username=username).exists():
              # Generate a random username
              username = str(uuid.uuid4())[:30]
  
          user = User.objects.create_user(username=username)
  
          return user
  
  
  
Make sure generated usernames are unique.
## Code Before:
import uuid

from django.contrib.auth.models import User


class UserTestsMixin(object):
    """ Mixin base class for tests requiring users. """

    def create_user(self, username=None, password=None):
        """ Create, save and return a new user. """

        if not username:
            # Generate a random username
            username = str(uuid.uuid4())[:30]

        user = User.objects.create_user(username=username)

        return user



## Instruction:
Make sure generated usernames are unique.
## Code After:
import uuid

from django.contrib.auth.models import User


class UserTestsMixin(object):
    """ Mixin base class for tests requiring users. """

    def create_user(self, username=None, password=None):
        """ Create, save and return a new user. """

        # If no username is set, create a random unique username
        while not username or User.objects.filter(username=username).exists():
            # Generate a random username
            username = str(uuid.uuid4())[:30]

        user = User.objects.create_user(username=username)

        return user


