Advertisement
Guest User

Untitled

a guest
Mar 29th, 2017
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.59 KB | None | 0 0
  1. class UserSubFactory(factory.DjangoModelFactory):
  2.     """
  3.    Used as a related sub factory for UserProfileFactory.
  4.    """
  5.     class Meta:
  6.         model = 'auth.User'
  7.  
  8.     @classmethod
  9.     def _create(cls, model_class, *args, **kwargs):
  10.         """Override the default ``_create`` with the create_user call. This allows setting the password directly"""
  11.         manager = cls._get_manager(model_class)
  12.         # The default would use ``manager.create(*args, **kwargs)``
  13.         return manager.create_user(*args, **kwargs)
  14.  
  15.  
  16. class UserProfileFactory(factory.DjangoModelFactory):
  17.     """
  18.    Will be mainly used for testing, as it will always create an associated user
  19.    with identical information to the profile.
  20.    """
  21.     class Meta:
  22.         model = 'profiles.UserProfile'
  23.  
  24.     # If no company is specifically provided beforehand, perform a get_or_create for a company called rodeapps.
  25.     company = factory.SubFactory('sibylsurveys.modules.company.factories.CompanyFactory', name='Rodeapps')
  26.  
  27.     first_name = 'John'
  28.     last_name = 'Doe'
  29.     username = factory.Sequence(lambda n: 'user%d' % n)
  30.     email = factory.LazyAttribute(lambda obj: '%s@gmail.com' % obj.username)
  31.  
  32.     # This will always create a new corresponding user.
  33.     user = factory.SubFactory(
  34.         UserSubFactory,
  35.         username=factory.SelfAttribute('..username'),
  36.         first_name=factory.SelfAttribute('..first_name'),
  37.         last_name=factory.SelfAttribute('..last_name'),
  38.         email=factory.SelfAttribute('..email'),
  39.         password='pass'
  40.     )
  41.  
  42.     has_app_access = True
  43.     has_bo_access = True
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement