createfakethreads.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 django.db.transaction import atomic
  6. from django.template.defaultfilters import linebreaks_filter
  7. from django.utils import timezone
  8. from django.utils.six.moves import range
  9. from faker import Factory
  10. from misago.categories.models import Category
  11. from misago.core.management.progressbar import show_progress
  12. from misago.threads.checksums import update_post_checksum
  13. from misago.threads.models import Post, Thread
  14. class Command(BaseCommand):
  15. help = 'Adds random threads and posts for testing purposes'
  16. def handle(self, *args, **options):
  17. try:
  18. fake_threads_to_create = int(args[0])
  19. except IndexError:
  20. fake_threads_to_create = 5
  21. except ValueError:
  22. self.stderr.write("\nOptional argument should be integer.")
  23. sys.exit(1)
  24. categories = list(Category.objects.all_categories())
  25. fake = Factory.create()
  26. User = get_user_model()
  27. total_users = User.objects.count()
  28. self.stdout.write('Creating fake threads...\n')
  29. message = '\nSuccessfully created %s fake threads in %s'
  30. created_threads = 0
  31. start_time = time.time()
  32. show_progress(self, created_threads, fake_threads_to_create)
  33. for i in range(fake_threads_to_create):
  34. with atomic():
  35. datetime = timezone.now()
  36. category = random.choice(categories)
  37. user = User.objects.order_by('?')[:1][0]
  38. thread_is_unapproved = random.randint(0, 100) > 90
  39. thread_is_hidden = random.randint(0, 100) > 90
  40. thread_is_closed = random.randint(0, 100) > 90
  41. thread = Thread(
  42. category=category,
  43. started_on=datetime,
  44. starter_name='-',
  45. starter_slug='-',
  46. last_post_on=datetime,
  47. last_poster_name='-',
  48. last_poster_slug='-',
  49. replies=0,
  50. is_unapproved=thread_is_unapproved,
  51. is_hidden=thread_is_hidden,
  52. is_closed=thread_is_closed
  53. )
  54. thread.set_title(fake.sentence())
  55. thread.save()
  56. fake_message = "\n\n".join(fake.paragraphs())
  57. post = Post.objects.create(
  58. category=category,
  59. thread=thread,
  60. poster=user,
  61. poster_name=user.username,
  62. poster_ip=fake.ipv4(),
  63. original=fake_message,
  64. parsed=linebreaks_filter(fake_message),
  65. posted_on=datetime,
  66. updated_on=datetime
  67. )
  68. update_post_checksum(post)
  69. post.save(update_fields=['checksum'])
  70. thread.set_first_post(post)
  71. thread.set_last_post(post)
  72. thread.save()
  73. user.threads += 1
  74. user.posts += 1
  75. user.save()
  76. thread_type = random.randint(0, 100)
  77. if thread_type > 95:
  78. thread_replies = random.randint(200, 2500)
  79. elif thread_type > 50:
  80. thread_replies = random.randint(5, 30)
  81. else:
  82. thread_replies = random.randint(0, 10)
  83. for x in range(thread_replies):
  84. datetime = timezone.now()
  85. user = User.objects.order_by('?')[:1][0]
  86. fake_message = "\n\n".join(fake.paragraphs())
  87. is_unapproved = random.randint(0, 100) > 97
  88. if not is_unapproved:
  89. is_hidden = random.randint(0, 100) > 97
  90. else:
  91. is_hidden = False
  92. post = Post.objects.create(
  93. category=category,
  94. thread=thread,
  95. poster=user,
  96. poster_name=user.username,
  97. poster_ip=fake.ipv4(),
  98. original=fake_message,
  99. parsed=linebreaks_filter(fake_message),
  100. is_hidden=is_hidden,
  101. is_unapproved=is_unapproved,
  102. posted_on=datetime,
  103. updated_on=datetime
  104. )
  105. update_post_checksum(post)
  106. post.save(update_fields=['checksum'])
  107. user.posts += 1
  108. user.save()
  109. thread.synchronize()
  110. thread.save()
  111. created_threads += 1
  112. show_progress(
  113. self, created_threads, fake_threads_to_create, start_time)
  114. pinned_threads = random.randint(0, int(created_threads * 0.025)) or 1
  115. self.stdout.write('\nPinning %s threads...' % pinned_threads)
  116. for i in range(0, pinned_threads):
  117. thread = Thread.objects.order_by('?')[:1][0]
  118. if random.randint(0, 100) > 75:
  119. thread.weight = 2
  120. else:
  121. thread.weight = 1
  122. thread.save()
  123. for category in categories:
  124. category.synchronize()
  125. category.save()
  126. total_time = time.time() - start_time
  127. total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
  128. self.stdout.write(message % (created_threads, total_humanized))