createfakethreads.py 7.1 KB

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