# Path: profiles/models.py
# class Profile(User):
#     objects = UserManager()
#     _extensions_imported = False
# 
#     @classmethod
#     def remove_field(cls, f_name):
#         # Removes the field form local fields list
#         cls._meta.local_fields = [f for f in cls._meta.local_fields if f.name != f_name]
# 
#         # Removes the field setter if exists
#         if hasattr(cls, f_name):
#             delattr(cls, f_name)
# 
#     @classmethod
#     def register_extension(cls, register_fn):
#         register_fn(cls, ProfileAdmin)
# 
#     @classmethod
#     def register_extensions(cls, *extensions):
#         if not hasattr(cls, '_profile_extensions'):
#             cls._profile_extensions = set()
# 
#         here = cls.__module__.split('.')[:-1]
#         here_path = '.'.join(here + ['extensions'])
# 
#         for ext in extensions:
#             if ext in cls._profile_extensions:
#                 continue
# 
#             try:
#                 if isinstance(ext, basestring):
#                     try:
#                         fn = get_callable(ext + '.register', False)
#                     except ImportError:
#                         fn = get_callable('%s.%s.register' % (here_path, ext), False)
#                 # Not a string, so take our chances and just try to access "register"
#                 else:
#                     fn = ext.register
# 
#                 cls.register_extension(fn)
#                 cls._profile_extensions.add(ext)
#             except Exception:
#                 raise
# 
#     def save(self, *args, **kwargs):
#         # Remove leading and trailing whitespace from name.
#         try:
#             self.first_name = self.first_name.strip()
#             self.last_name = self.last_name.strip()
#         except AttributeError:
#             pass
# 
#         if not self.username:
#             self.username = generate_id(first_name=self.first_name,
#                                         last_name=self.last_name, email=self.email)
#         super(Profile, self).save(*args, **kwargs)
# 
#     def get_full_name(self):
#         full_name = super(Profile, self).get_full_name()
#         if not full_name:
#             return self.username
#         return full_name
# 
#     def get_profile(self):
#         return self


from django.contrib.auth.models import User
from .models import Profile
import factory




class UserFactory(factory.Factory):
    FACTORY_FOR = User
    first_name = factory.Sequence(lambda n: 'Firstname {0}'.format(n))
    last_name = factory.Sequence(lambda n: 'Lastname {0}'.format(n))
    username = factory.Sequence(lambda n: 'user-{0}'.format(n).lower())
    email = factory.LazyAttribute(lambda a: '{0}@example.com'.format(a.username).lower())

    @classmethod
    def _prepare(cls, create, **kwargs):
        password = kwargs.pop('password', 'password')
        user = super(UserFactory, cls)._prepare(create=False, **kwargs)

# Path: profiles/models.py
# class Profile(User):
#     objects = UserManager()
#     _extensions_imported = False
# 
#     @classmethod
#     def remove_field(cls, f_name):
#         # Removes the field form local fields list
#         cls._meta.local_fields = [f for f in cls._meta.local_fields if f.name != f_name]
# 
#         # Removes the field setter if exists
#         if hasattr(cls, f_name):
#             delattr(cls, f_name)
# 
#     @classmethod
#     def register_extension(cls, register_fn):
#         register_fn(cls, ProfileAdmin)
# 
#     @classmethod
#     def register_extensions(cls, *extensions):
#         if not hasattr(cls, '_profile_extensions'):
#             cls._profile_extensions = set()
# 
#         here = cls.__module__.split('.')[:-1]
#         here_path = '.'.join(here + ['extensions'])
# 
#         for ext in extensions:
#             if ext in cls._profile_extensions:
#                 continue
# 
#             try:
#                 if isinstance(ext, basestring):
#                     try:
#                         fn = get_callable(ext + '.register', False)
#                     except ImportError:
#                         fn = get_callable('%s.%s.register' % (here_path, ext), False)
#                 # Not a string, so take our chances and just try to access "register"
#                 else:
#                     fn = ext.register
# 
#                 cls.register_extension(fn)
#                 cls._profile_extensions.add(ext)
#             except Exception:
#                 raise
# 
#     def save(self, *args, **kwargs):
#         # Remove leading and trailing whitespace from name.
#         try:
#             self.first_name = self.first_name.strip()
#             self.last_name = self.last_name.strip()
#         except AttributeError:
#             pass
# 
#         if not self.username:
#             self.username = generate_id(first_name=self.first_name,
#                                         last_name=self.last_name, email=self.email)
#         super(Profile, self).save(*args, **kwargs)
# 
#     def get_full_name(self):
#         full_name = super(Profile, self).get_full_name()
#         if not full_name:
#             return self.username
#         return full_name
# 
#     def get_profile(self):
#         return self


# Path: profiles/tests.py
from django.contrib.auth.models import User
from .models import Profile
import factory





class UserFactory(factory.Factory):
    FACTORY_FOR = User
    first_name = factory.Sequence(lambda n: 'Firstname {0}'.format(n))
    last_name = factory.Sequence(lambda n: 'Lastname {0}'.format(n))
    username = factory.Sequence(lambda n: 'user-{0}'.format(n).lower())
    email = factory.LazyAttribute(lambda a: '{0}@example.com'.format(a.username).lower())

    @classmethod
    def _prepare(cls, create, **kwargs):
        password = kwargs.pop('password', 'password')
        user = super(UserFactory, cls)._prepare(create=False, **kwargs)

        user.set_password(password)