createfakethreads.py 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  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 misago.categories.models import Category
  10. from misago.core.management.progressbar import show_progress
  11. from misago.faker.englishcorpus import EnglishCorpus
  12. from misago.threads.checksums import update_post_checksum
  13. from misago.threads.models import Post, Thread
  14. PLACEKITTEN_URL = 'https://placekitten.com/g/%s/%s'
  15. UserModel = get_user_model()
  16. corpus = EnglishCorpus()
  17. corpus_short = EnglishCorpus(max_length=150)
  18. class Command(BaseCommand):
  19. help = 'Creates random threads 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. 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. with atomic():
  39. datetime = timezone.now()
  40. category = random.choice(categories)
  41. user = UserModel.objects.order_by('?')[:1][0]
  42. thread_is_unapproved = random.randint(0, 100) > 90
  43. thread_is_hidden = random.randint(0, 100) > 90
  44. thread_is_closed = random.randint(0, 100) > 90
  45. thread = Thread(
  46. category=category,
  47. started_on=datetime,
  48. starter_name='-',
  49. starter_slug='-',
  50. last_post_on=datetime,
  51. last_poster_name='-',
  52. last_poster_slug='-',
  53. replies=0,
  54. is_unapproved=thread_is_unapproved,
  55. is_hidden=thread_is_hidden,
  56. is_closed=thread_is_closed,
  57. )
  58. thread.set_title(corpus_short.random_choice())
  59. thread.save()
  60. original, parsed = self.fake_post_content()
  61. post = Post.objects.create(
  62. category=category,
  63. thread=thread,
  64. poster=user,
  65. poster_name=user.username,
  66. original=original,
  67. parsed=parsed,
  68. posted_on=datetime,
  69. updated_on=datetime,
  70. )
  71. update_post_checksum(post)
  72. post.save(update_fields=['checksum'])
  73. thread.set_first_post(post)
  74. thread.set_last_post(post)
  75. thread.save()
  76. user.threads += 1
  77. user.posts += 1
  78. user.save()
  79. thread_type = random.randint(0, 100)
  80. if thread_type > 98:
  81. thread_replies = random.randint(200, 2500)
  82. elif thread_type > 50:
  83. thread_replies = random.randint(5, 30)
  84. else:
  85. thread_replies = random.randint(0, 10)
  86. for _ in range(thread_replies):
  87. datetime = timezone.now()
  88. user = UserModel.objects.order_by('?')[:1][0]
  89. original, parsed = self.fake_post_content()
  90. is_unapproved = random.randint(0, 100) > 97
  91. post = Post.objects.create(
  92. category=category,
  93. thread=thread,
  94. poster=user,
  95. poster_name=user.username,
  96. original=original,
  97. parsed=parsed,
  98. is_unapproved=is_unapproved,
  99. posted_on=datetime,
  100. updated_on=datetime,
  101. )
  102. if not is_unapproved:
  103. is_hidden = random.randint(0, 100) > 97
  104. else:
  105. is_hidden = False
  106. if is_hidden:
  107. post.is_hidden = True
  108. if random.randint(0, 100) < 80:
  109. user = UserModel.objects.order_by('?')[:1][0]
  110. post.hidden_by = user
  111. post.hidden_by_name = user.username
  112. post.hidden_by_slug = user.username
  113. else:
  114. post.hidden_by_name = fake.first_name()
  115. post.hidden_by_slug = post.hidden_by_name.lower()
  116. update_post_checksum(post)
  117. post.save()
  118. user.posts += 1
  119. user.save()
  120. thread.synchronize()
  121. thread.save()
  122. created_threads += 1
  123. show_progress(self, created_threads, items_to_create, start_time)
  124. pinned_threads = random.randint(0, int(created_threads * 0.025)) or 1
  125. self.stdout.write('\nPinning %s threads...' % pinned_threads)
  126. for _ in range(0, pinned_threads):
  127. thread = Thread.objects.order_by('?')[:1][0]
  128. if random.randint(0, 100) > 75:
  129. thread.weight = 2
  130. else:
  131. thread.weight = 1
  132. thread.save()
  133. for category in categories:
  134. category.synchronize()
  135. category.save()
  136. total_time = time.time() - start_time
  137. total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
  138. message = '\nSuccessfully created %s fake threads in %s'
  139. self.stdout.write(message % (created_threads, total_humanized))
  140. def fake_post_content(self):
  141. raw = []
  142. parsed = []
  143. if random.randint(0, 100) > 80:
  144. paragraphs_to_make = random.randint(1, 20)
  145. else:
  146. paragraphs_to_make = random.randint(1, 5)
  147. for _ in range(paragraphs_to_make):
  148. if random.randint(0, 100) > 95:
  149. cat_width = random.randint(1, 16) * random.choice([100, 90, 80])
  150. cat_height = random.randint(1, 12) * random.choice([100, 90, 80])
  151. cat_url = PLACEKITTEN_URL % (cat_width, cat_height)
  152. raw.append('!(%s)' % cat_url)
  153. parsed.append('<p><img src="%s" alt=""/></p>' % cat_url)
  154. else:
  155. if random.randint(0, 100) > 95:
  156. sentences_to_make = random.randint(1, 20)
  157. else:
  158. sentences_to_make = random.randint(1, 7)
  159. raw.append(' '.join(corpus.random_sentences(sentences_to_make)))
  160. parsed.append('<p>%s</p>' % raw[-1])
  161. return "\n\n".join(raw), "\n".join(parsed)