createfakethreads.py 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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.utils import timezone
  9. from django.utils.six.moves import range
  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. from misago.faker.englishcorpus import EnglishCorpus
  15. PLACEKITTEN_URL = 'https://placekitten.com/g/%s/%s'
  16. UserModel = get_user_model()
  17. corpus = EnglishCorpus()
  18. corpus_short = EnglishCorpus(max_length=150)
  19. class Command(BaseCommand):
  20. help = 'Creates random threads and posts for dev and testing purposes.'
  21. def add_arguments(self, parser):
  22. parser.add_argument(
  23. 'threads',
  24. help="number of threads to create",
  25. nargs='?',
  26. type=int,
  27. default=5
  28. )
  29. def handle(self, *args, **options):
  30. items_to_create = options['threads']
  31. categories = list(Category.objects.all_categories())
  32. fake = Factory.create()
  33. total_users = UserModel.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 = UserModel.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. original, parsed = self.fake_post_content()
  63. post = Post.objects.create(
  64. category=category,
  65. thread=thread,
  66. poster=user,
  67. poster_name=user.username,
  68. poster_ip=fake.ipv4(),
  69. original=original,
  70. parsed=parsed,
  71. posted_on=datetime,
  72. updated_on=datetime
  73. )
  74. update_post_checksum(post)
  75. post.save(update_fields=['checksum'])
  76. thread.set_first_post(post)
  77. thread.set_last_post(post)
  78. thread.save()
  79. user.threads += 1
  80. user.posts += 1
  81. user.save()
  82. thread_type = random.randint(0, 100)
  83. if thread_type > 98:
  84. thread_replies = random.randint(200, 2500)
  85. elif thread_type > 50:
  86. thread_replies = random.randint(5, 30)
  87. else:
  88. thread_replies = random.randint(0, 10)
  89. for x in range(thread_replies):
  90. datetime = timezone.now()
  91. user = UserModel.objects.order_by('?')[:1][0]
  92. original, parsed = self.fake_post_content()
  93. is_unapproved = random.randint(0, 100) > 97
  94. post = Post.objects.create(
  95. category=category,
  96. thread=thread,
  97. poster=user,
  98. poster_name=user.username,
  99. poster_ip=fake.ipv4(),
  100. original=original,
  101. parsed=parsed,
  102. is_unapproved=is_unapproved,
  103. posted_on=datetime,
  104. updated_on=datetime
  105. )
  106. if not is_unapproved:
  107. is_hidden = random.randint(0, 100) > 97
  108. else:
  109. is_hidden = False
  110. if is_hidden:
  111. post.is_hidden = True
  112. if random.randint(0, 100) < 80:
  113. user = UserModel.objects.order_by('?')[:1][0]
  114. post.hidden_by = user
  115. post.hidden_by_name = user.username
  116. post.hidden_by_slug = user.username
  117. else:
  118. post.hidden_by_name = fake.first_name()
  119. post.hidden_by_slug = post.hidden_by_name.lower()
  120. update_post_checksum(post)
  121. post.save()
  122. user.posts += 1
  123. user.save()
  124. thread.synchronize()
  125. thread.save()
  126. created_threads += 1
  127. show_progress(
  128. self, created_threads, items_to_create, start_time)
  129. pinned_threads = random.randint(0, int(created_threads * 0.025)) or 1
  130. self.stdout.write('\nPinning %s threads...' % pinned_threads)
  131. for i in range(0, pinned_threads):
  132. thread = Thread.objects.order_by('?')[:1][0]
  133. if random.randint(0, 100) > 75:
  134. thread.weight = 2
  135. else:
  136. thread.weight = 1
  137. thread.save()
  138. for category in categories:
  139. category.synchronize()
  140. category.save()
  141. total_time = time.time() - start_time
  142. total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
  143. self.stdout.write(message % (created_threads, total_humanized))
  144. def fake_post_content(self):
  145. raw = []
  146. parsed = []
  147. if random.randint(0, 100) > 80:
  148. paragraphs_to_make = random.randint(1, 20)
  149. else:
  150. paragraphs_to_make = random.randint(1, 5)
  151. for i in range(paragraphs_to_make):
  152. if random.randint(0, 100) > 95:
  153. cat_width = random.randint(1, 16) * random.choice([100, 90, 80])
  154. cat_height = random.randint(1, 12) * random.choice([100, 90, 80])
  155. cat_url = PLACEKITTEN_URL % (cat_width, cat_height)
  156. raw.append('!(%s)' % cat_url)
  157. parsed.append('<p><img src="%s" alt=""/></p>' % cat_url)
  158. else:
  159. if random.randint(0, 100) > 95:
  160. sentences_to_make = random.randint(1, 20)
  161. else:
  162. sentences_to_make = random.randint(1, 7)
  163. raw.append(' '.join(corpus.random_sentences(sentences_to_make)))
  164. parsed.append('<p>%s</p>' % raw[-1])
  165. return "\n\n".join(raw), "\n".join(parsed)