threads.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. from django.utils import timezone
  2. from ..threads.models import Thread
  3. from .englishcorpus import EnglishCorpus
  4. from .posts import get_fake_hidden_post, get_fake_post, get_fake_unapproved_post
  5. corpus_short = EnglishCorpus(max_length=150)
  6. def get_fake_thread(fake, category, starter=None):
  7. thread = _create_base_thread(fake, category)
  8. thread.first_post = get_fake_post(fake, thread, starter)
  9. thread.save(update_fields=["first_post"])
  10. return thread
  11. def get_fake_closed_thread(fake, category, starter=None):
  12. thread = get_fake_thread(fake, category)
  13. thread.is_closed = True
  14. thread.save(update_fields=["is_closed"])
  15. return thread
  16. def get_fake_hidden_thread(fake, category, starter=None, hidden_by=None):
  17. thread = _create_base_thread(fake, category)
  18. thread.first_post = get_fake_hidden_post(fake, thread, starter, hidden_by)
  19. thread.is_hidden = True
  20. thread.save(update_fields=["first_post", "is_hidden"])
  21. return thread
  22. def get_fake_unapproved_thread(fake, category, starter=None):
  23. thread = _create_base_thread(fake, category)
  24. thread.first_post = get_fake_unapproved_post(fake, thread, starter)
  25. thread.is_unapproved = True
  26. thread.save(update_fields=["first_post", "is_unapproved"])
  27. return thread
  28. def _create_base_thread(fake, category):
  29. started_on = timezone.now()
  30. thread = Thread(
  31. category=category,
  32. started_on=started_on,
  33. starter_name="-",
  34. starter_slug="-",
  35. last_post_on=started_on,
  36. last_poster_name="-",
  37. last_poster_slug="-",
  38. replies=0,
  39. )
  40. # Sometimes thread ends with slug being set to empty string
  41. while not thread.slug:
  42. thread.set_title(corpus_short.random_sentence())
  43. thread.save()
  44. return thread