You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
95 lines
2.7 KiB
95 lines
2.7 KiB
from itertools import combinations
|
|
|
|
from factory.django import DjangoModelFactory, ImageField
|
|
import factory
|
|
import factory.fuzzy
|
|
from unidecode import unidecode
|
|
from django.utils.text import slugify
|
|
|
|
|
|
from apps.course.models import *
|
|
from apps.content.models import *
|
|
from apps.user.models import *
|
|
|
|
|
|
ADMIN_EMAIL = 'admin@mail.com'
|
|
|
|
|
|
def create_admin():
|
|
admin = UserFactory(username=ADMIN_EMAIL, email=ADMIN_EMAIL, role=User.ADMIN_ROLE,
|
|
is_staff=True, is_superuser=True)
|
|
admin.set_password('admin')
|
|
admin.save()
|
|
return admin
|
|
|
|
|
|
def create_users(count_multiplier=1):
|
|
create_admin()
|
|
UserFactory.create_batch(10 * count_multiplier, role=User.USER_ROLE)
|
|
UserFactory.create_batch(5 * count_multiplier, role=User.AUTHOR_ROLE)
|
|
UserFactory.create_batch(5 * count_multiplier, role=User.TEACHER_ROLE)
|
|
UserFactory.create_batch(5, role=User.ADMIN_ROLE, is_staff=True)
|
|
|
|
|
|
def login_admin(fn):
|
|
def wrap(self, *args, **kwargs):
|
|
admin = User.objects.get(username=ADMIN_EMAIL)
|
|
self.client.force_login(admin)
|
|
try:
|
|
fn(self, *args, **kwargs)
|
|
finally:
|
|
self.client.logout()
|
|
return wrap
|
|
|
|
|
|
def create_batch_unique(factory_class, **kwargs):
|
|
model = factory_class._meta.model
|
|
values = []
|
|
for k, v in kwargs.items():
|
|
try:
|
|
f = model._meta.get_field(k)
|
|
except:
|
|
del kwargs[k]
|
|
if getattr(f, 'choices', None):
|
|
v = [c[0] for c in v]
|
|
values += ((k, val) for val in v)
|
|
for params in combinations(values, len(kwargs)):
|
|
data = dict(params)
|
|
if len(data) == len(kwargs):
|
|
factory_class(**data)
|
|
|
|
|
|
class ImageObjectFactory(DjangoModelFactory):
|
|
image = ImageField()
|
|
image_thumbnail = ImageField()
|
|
|
|
|
|
class UserFactory(DjangoModelFactory):
|
|
class Meta:
|
|
model = User
|
|
|
|
first_name = factory.Faker('first_name')
|
|
last_name = factory.Faker('last_name')
|
|
email = factory.Sequence(lambda n: "test_user%d@mail.com" % n)
|
|
username = factory.LazyAttribute(lambda o: o.email)
|
|
gallery = None
|
|
photo = None #factory.SubFactory(ImageObjectFactory)
|
|
is_active = True
|
|
|
|
|
|
class CategoryFactory(DjangoModelFactory):
|
|
class Meta:
|
|
model = Category
|
|
|
|
|
|
class CourseFactory(DjangoModelFactory):
|
|
class Meta:
|
|
model = Course
|
|
|
|
author = factory.SubFactory(UserFactory, role=User.AUTHOR_ROLE)
|
|
title = factory.Faker('sentence', nb_words=6)
|
|
slug = factory.LazyAttribute(lambda o: slugify(unidecode(o.title[:90])))
|
|
category = factory.SubFactory(CategoryFactory)
|
|
cover = None #factory.SubFactory(ImageObjectFactory)
|
|
gallery = None
|
|
status = factory.Iterator([s[0] for s in Course.STATUS_CHOICES])
|
|
|