createfakethreads.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. from __future__ import unicode_literals
  2. import random
  3. import time
  4. from faker import Factory
  5. from django.contrib.auth import get_user_model
  6. from django.core.management.base import BaseCommand
  7. from django.db.transaction import atomic
  8. from django.template.defaultfilters import linebreaks_filter
  9. from django.utils import timezone
  10. from django.utils.six.moves import range
  11. from misago.categories.models import Category
  12. from misago.core.management.progressbar import show_progress
  13. from misago.threads.checksums import update_post_checksum
  14. from misago.threads.models import Post, Thread
  15. from ...englishcorpus import EnglishCorpus
  16. corpus = EnglishCorpus()
  17. corpus_short = EnglishCorpus(max_length=150)
  18. class Command(BaseCommand):
  19. help = 'Creates random threads and posts for dev and testing purposes.'
  20. def add_arguments(self, parser):
  21. parser.add_argument(
  22. 'threads',
  23. help="number of threads to create",
  24. nargs='?',
  25. type=int,
  26. default=5
  27. )
  28. def handle(self, *args, **options):
  29. items_to_create = options['threads']
  30. categories = list(Category.objects.all_categories())
  31. fake = Factory.create()
  32. User = get_user_model()
  33. total_users = User.objects.count()
  34. self.stdout.write('Creating fake threads...\n')
  35. message = '\nSuccessfully created %s fake threads in %s'
  36. created_threads = 0
  37. start_time = time.time()
  38. show_progress(self, created_threads, items_to_create)
  39. while created_threads < items_to_create:
  40. with atomic():
  41. datetime = timezone.now()
  42. category = random.choice(categories)
  43. user = User.objects.order_by('?')[:1][0]
  44. thread_is_unapproved = random.randint(0, 100) > 90
  45. thread_is_hidden = random.randint(0, 100) > 90
  46. thread_is_closed = random.randint(0, 100) > 90
  47. thread = Thread(
  48. category=category,
  49. started_on=datetime,
  50. starter_name='-',
  51. starter_slug='-',
  52. last_post_on=datetime,
  53. last_poster_name='-',
  54. last_poster_slug='-',
  55. replies=0,
  56. is_unapproved=thread_is_unapproved,
  57. is_hidden=thread_is_hidden,
  58. is_closed=thread_is_closed
  59. )
  60. thread.set_title(corpus_short.random_choice())
  61. thread.save()
  62. paragraphs = []
  63. for i in range(random.randint(1, 20)):
  64. paragraphs.append(' '.join(corpus.random_sentences(random.randint(1, 20))))
  65. fake_message = "\n\n".join(paragraphs)
  66. post = Post.objects.create(
  67. category=category,
  68. thread=thread,
  69. poster=user,
  70. poster_name=user.username,
  71. poster_ip=fake.ipv4(),
  72. original=fake_message,
  73. parsed=linebreaks_filter(fake_message),
  74. posted_on=datetime,
  75. updated_on=datetime
  76. )
  77. update_post_checksum(post)
  78. post.save(update_fields=['checksum'])
  79. thread.set_first_post(post)
  80. thread.set_last_post(post)
  81. thread.save()
  82. user.threads += 1
  83. user.posts += 1
  84. user.save()
  85. thread_type = random.randint(0, 100)
  86. if thread_type > 98:
  87. thread_replies = random.randint(200, 2500)
  88. elif thread_type > 50:
  89. thread_replies = random.randint(5, 30)
  90. else:
  91. thread_replies = random.randint(0, 10)
  92. for x in range(thread_replies):
  93. datetime = timezone.now()
  94. user = User.objects.order_by('?')[:1][0]
  95. paragraphs = []
  96. for i in range(random.randint(1, 20)):
  97. paragraphs.append(' '.join(corpus.random_sentences(random.randint(1, 20))))
  98. fake_message = "\n\n".join(paragraphs)
  99. is_unapproved = random.randint(0, 100) > 97
  100. post = Post.objects.create(
  101. category=category,
  102. thread=thread,
  103. poster=user,
  104. poster_name=user.username,
  105. poster_ip=fake.ipv4(),
  106. original=fake_message,
  107. parsed=linebreaks_filter(fake_message),
  108. is_unapproved=is_unapproved,
  109. posted_on=datetime,
  110. updated_on=datetime
  111. )
  112. if not is_unapproved:
  113. is_hidden = random.randint(0, 100) > 97
  114. else:
  115. is_hidden = False
  116. if is_hidden:
  117. post.is_hidden = True
  118. if random.randint(0, 100) < 80:
  119. user = User.objects.order_by('?')[:1][0]
  120. post.hidden_by = user
  121. post.hidden_by_name = user.username
  122. post.hidden_by_slug = user.username
  123. else:
  124. post.hidden_by_name = fake.first_name()
  125. post.hidden_by_slug = post.hidden_by_name.lower()
  126. update_post_checksum(post)
  127. post.save()
  128. user.posts += 1
  129. user.save()
  130. thread.synchronize()
  131. thread.save()
  132. created_threads += 1
  133. show_progress(
  134. self, created_threads, items_to_create, start_time)
  135. pinned_threads = random.randint(0, int(created_threads * 0.025)) or 1
  136. self.stdout.write('\nPinning %s threads...' % pinned_threads)
  137. for i in range(0, pinned_threads):
  138. thread = Thread.objects.order_by('?')[:1][0]
  139. if random.randint(0, 100) > 75:
  140. thread.weight = 2
  141. else:
  142. thread.weight = 1
  143. thread.save()
  144. for category in categories:
  145. category.synchronize()
  146. category.save()
  147. total_time = time.time() - start_time
  148. total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
  149. self.stdout.write(message % (created_threads, total_humanized))