createfakethreads.py 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. import random
  2. import time
  3. from django.contrib.auth import get_user_model
  4. from django.core.management.base import BaseCommand
  5. from faker import Factory
  6. from ....categories.models import Category
  7. from ....core.management.progressbar import show_progress
  8. from ....threads.models import Thread
  9. from ...threads import (
  10. get_fake_closed_thread,
  11. get_fake_hidden_thread,
  12. get_fake_thread,
  13. get_fake_unapproved_thread,
  14. )
  15. User = get_user_model()
  16. class Command(BaseCommand):
  17. help = "Creates random threads for dev and testing purposes."
  18. def add_arguments(self, parser):
  19. parser.add_argument(
  20. "threads",
  21. help="number of threads to create",
  22. nargs="?",
  23. type=int,
  24. default=5,
  25. )
  26. def handle(
  27. self, *args, **options
  28. ): # pylint: disable=too-many-locals, too-many-branches
  29. items_to_create = options["threads"]
  30. fake = Factory.create()
  31. categories = list(Category.objects.all_categories())
  32. message = "Creating %s fake threads...\n"
  33. self.stdout.write(message % items_to_create)
  34. created_threads = 0
  35. start_time = time.time()
  36. show_progress(self, created_threads, items_to_create)
  37. while created_threads < items_to_create:
  38. category = random.choice(categories)
  39. # 10% chance thread poster is anonymous
  40. if random.randint(0, 100) > 90:
  41. starter = None
  42. else:
  43. starter = User.objects.order_by("?")[:1].last()
  44. # There's 10% chance thread is closed
  45. if random.randint(0, 100) > 90:
  46. thread = get_fake_closed_thread(fake, category, starter)
  47. # There's further 5% chance thread is hidden
  48. elif random.randint(0, 100) > 95:
  49. if random.randint(0, 100) > 90:
  50. hidden_by = None
  51. else:
  52. hidden_by = User.objects.order_by("?")[:1].last()
  53. thread = get_fake_hidden_thread(fake, category, starter, hidden_by)
  54. # And further 5% chance thread is unapproved
  55. elif random.randint(0, 100) > 95:
  56. thread = get_fake_unapproved_thread(fake, category, starter)
  57. # Default, standard thread
  58. else:
  59. thread = get_fake_thread(fake, category, starter)
  60. thread.synchronize()
  61. thread.save()
  62. created_threads += 1
  63. show_progress(self, created_threads, items_to_create, start_time)
  64. pinned_threads = random.randint(0, int(created_threads * 0.025)) or 1
  65. self.stdout.write("\nPinning %s threads..." % pinned_threads)
  66. for _ in range(0, pinned_threads):
  67. thread = Thread.objects.order_by("?")[:1][0]
  68. if random.randint(0, 100) > 90:
  69. thread.weight = 2
  70. else:
  71. thread.weight = 1
  72. thread.save()
  73. for category in categories:
  74. category.synchronize()
  75. category.save()
  76. total_time = time.time() - start_time
  77. total_humanized = time.strftime("%H:%M:%S", time.gmtime(total_time))
  78. message = "\nSuccessfully created %s fake threads in %s"
  79. self.stdout.write(message % (created_threads, total_humanized))