Guest User

Untitled

a guest
Sep 30th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. import datetime
  2. import md5
  3.  
  4. from django.contrib.auth.models import User
  5.  
  6. import factory
  7.  
  8.  
  9. class UserFactory(factory.Factory):
  10. """
  11. Creates a new ``User`` object.
  12.  
  13. Username will be a random 30 character md5 value.
  14. Email will be ``userN@example.com`` with ``N`` being a counter.
  15. Password will be ``test123`` by default.
  16.  
  17. """
  18. FACTORY_FOR = User
  19.  
  20. username = factory.LazyAttribute(
  21. lambda x: md5.new(datetime.datetime.now().strftime(
  22. '%Y%,%d%H%M%S')).hexdigest()[0:30])
  23. email = factory.Sequence(lambda n: 'user{0}@example.com'.format(n))
  24.  
  25. @classmethod
  26. def _prepare(cls, create, **kwargs):
  27. password = 'test123'
  28. if 'password' in kwargs:
  29. password = kwargs.pop('password')
  30. user = super(UserFactory, cls)._prepare(create, **kwargs)
  31. user.set_password(password)
  32. if create:
  33. user.save()
  34. return user
Add Comment
Please, Sign In to add comment