Skip to content

Instantly share code, notes, and snippets.

@jaywink
Forked from mbrochh/factories.py
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jaywink/c012b99b5be8bc3b10e7 to your computer and use it in GitHub Desktop.
Save jaywink/c012b99b5be8bc3b10e7 to your computer and use it in GitHub Desktop.
import datetime
import md5
from django.contrib.auth.models import User
import factory
class UserFactory(factory.Factory):
"""
Creates a new ``User`` object.
Username will be a random 30 character md5 value.
Email will be ``userN@example.com`` with ``N`` being a counter.
Password will be ``test123`` by default.
"""
FACTORY_FOR = User
username = factory.LazyAttribute(
lambda x: md5.new(datetime.datetime.now().strftime(
'%Y%,%d%H%M%S')).hexdigest()[0:30])
email = factory.Sequence(lambda n: 'user{0}@example.com'.format(n))
@classmethod
def _prepare(cls, create, **kwargs):
password = 'test123'
if 'password' in kwargs:
password = kwargs.pop('password')
user = super(UserFactory, cls)._prepare(create, **kwargs)
user.set_password(password)
if create:
user.save()
return user
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment