from builtins import object
import factory

from django.contrib.auth.models import Group

from bluebottle.members.models import Member


class BlueBottleUserFactory(factory.DjangoModelFactory):
    class Meta(object):
        model = Member

    username = factory.Faker('email')
    email = factory.Faker('email')
    first_name = factory.Sequence(lambda f: u'user_{0}'.format(f))
    last_name = factory.Sequence(lambda l: u'user_{0}'.format(l))
    is_active = True
    is_staff = False
    is_superuser = False

    @classmethod
    def _create(cls, model_class, *args, **kwargs):
        user = super(BlueBottleUserFactory, cls)._create(model_class, *args, **kwargs)
        # ensure the raw password gets set after the initial save
        password = kwargs.pop("password", None)
        if password:
            user.set_password(password)
        user.save()
        return user


class GroupFactory(factory.DjangoModelFactory):
    class Meta(object):
        model = Group

    name = factory.Sequence(lambda n: u'group_{0}'.format(n))

from builtins import object
import factory

from django.contrib.auth.models import Group

from bluebottle.members.models import Member


class BlueBottleUserFactory(factory.DjangoModelFactory):
    class Meta(object):
        model = Member

    username = factory.Sequence(lambda n: u'user_{0}'.format(n))
    email = factory.Sequence(lambda o: u'user_{0}@onepercentclub.com'.format(o))
    first_name = factory.Sequence(lambda f: u'user_{0}'.format(f))
    last_name = factory.Sequence(lambda l: u'user_{0}'.format(l))
    is_active = True
    is_staff = False
    is_superuser = False

    @classmethod
    def _create(cls, model_class, *args, **kwargs):
        user = super(BlueBottleUserFactory, cls)._create(model_class, *args, **kwargs)
        # ensure the raw password gets set after the initial save
        password = kwargs.pop("password", None)
        if password:
            user.set_password(password)
        user.save()
        return user


class GroupFactory(factory.DjangoModelFactory):
    class Meta(object):
        model = Group

    name = factory.Sequence(lambda n: u'group_{0}'.format(n))

Fix duplicate users during tests
Fix duplicate users during tests

  from builtins import object
  import factory
  
  from django.contrib.auth.models import Group
  
  from bluebottle.members.models import Member
  
  
  class BlueBottleUserFactory(factory.DjangoModelFactory):
      class Meta(object):
          model = Member
  
-     username = factory.Faker('email')
-     email = factory.Faker('email')
+     username = factory.Sequence(lambda n: u'user_{0}'.format(n))
+     email = factory.Sequence(lambda o: u'user_{0}@onepercentclub.com'.format(o))
      first_name = factory.Sequence(lambda f: u'user_{0}'.format(f))
      last_name = factory.Sequence(lambda l: u'user_{0}'.format(l))
      is_active = True
      is_staff = False
      is_superuser = False
  
      @classmethod
      def _create(cls, model_class, *args, **kwargs):
          user = super(BlueBottleUserFactory, cls)._create(model_class, *args, **kwargs)
          # ensure the raw password gets set after the initial save
          password = kwargs.pop("password", None)
          if password:
              user.set_password(password)
          user.save()
          return user
  
  
  class GroupFactory(factory.DjangoModelFactory):
      class Meta(object):
          model = Group
  
      name = factory.Sequence(lambda n: u'group_{0}'.format(n))
  
Fix duplicate users during tests
## Code Before:
from builtins import object
import factory

from django.contrib.auth.models import Group

from bluebottle.members.models import Member


class BlueBottleUserFactory(factory.DjangoModelFactory):
    class Meta(object):
        model = Member

    username = factory.Faker('email')
    email = factory.Faker('email')
    first_name = factory.Sequence(lambda f: u'user_{0}'.format(f))
    last_name = factory.Sequence(lambda l: u'user_{0}'.format(l))
    is_active = True
    is_staff = False
    is_superuser = False

    @classmethod
    def _create(cls, model_class, *args, **kwargs):
        user = super(BlueBottleUserFactory, cls)._create(model_class, *args, **kwargs)
        # ensure the raw password gets set after the initial save
        password = kwargs.pop("password", None)
        if password:
            user.set_password(password)
        user.save()
        return user


class GroupFactory(factory.DjangoModelFactory):
    class Meta(object):
        model = Group

    name = factory.Sequence(lambda n: u'group_{0}'.format(n))

## Instruction:
Fix duplicate users during tests
## Code After:
from builtins import object
import factory

from django.contrib.auth.models import Group

from bluebottle.members.models import Member


class BlueBottleUserFactory(factory.DjangoModelFactory):
    class Meta(object):
        model = Member

    username = factory.Sequence(lambda n: u'user_{0}'.format(n))
    email = factory.Sequence(lambda o: u'user_{0}@onepercentclub.com'.format(o))
    first_name = factory.Sequence(lambda f: u'user_{0}'.format(f))
    last_name = factory.Sequence(lambda l: u'user_{0}'.format(l))
    is_active = True
    is_staff = False
    is_superuser = False

    @classmethod
    def _create(cls, model_class, *args, **kwargs):
        user = super(BlueBottleUserFactory, cls)._create(model_class, *args, **kwargs)
        # ensure the raw password gets set after the initial save
        password = kwargs.pop("password", None)
        if password:
            user.set_password(password)
        user.save()
        return user


class GroupFactory(factory.DjangoModelFactory):
    class Meta(object):
        model = Group

    name = factory.Sequence(lambda n: u'group_{0}'.format(n))
