categories.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. import random
  2. from ..categories.models import Category, RoleCategoryACL
  3. def fake_category(fake, parent, copy_acl_from=None):
  4. category = Category()
  5. category.set_name(fake_category_name(fake))
  6. if random.randint(1, 100) > 50:
  7. category.description = fake_category_description(fake)
  8. category.insert_at(parent, position="last-child", save=True)
  9. if copy_acl_from:
  10. copy_acl_to_fake_category(copy_acl_from, category)
  11. return category
  12. def fake_closed_category(fake, parent, copy_acl_from=None):
  13. category = fake_category(fake, parent, copy_acl_from)
  14. category.is_closed = True
  15. category.save(update_fields=["is_closed"])
  16. return category
  17. def copy_acl_to_fake_category(source, category):
  18. copied_acls = []
  19. for acl in source.category_role_set.all():
  20. copied_acls.append(
  21. RoleCategoryACL(
  22. role_id=acl.role_id,
  23. category=category,
  24. category_role_id=acl.category_role_id,
  25. )
  26. )
  27. if copied_acls:
  28. RoleCategoryACL.objects.bulk_create(copied_acls)
  29. def fake_category_name(fake):
  30. if random.randint(1, 100) > 75:
  31. return fake.catch_phrase().title()
  32. return fake.street_name()
  33. def fake_category_description(fake):
  34. if random.randint(1, 100) > 80:
  35. return "\r\n".join(fake.paragraphs())
  36. return fake.paragraph()