createfakethreads.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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 = 'Creates random threads and posts for dev and testing purposes.'
  16. def add_arguments(self, parser):
  17. parser.add_argument(
  18. 'threads',
  19. help="number of threads to create",
  20. nargs='?',
  21. type=int,
  22. default=5
  23. )
  24. def handle(self, *args, **options):
  25. items_to_create = options['threads']
  26. categories = list(Category.objects.all_categories())
  27. fake = Factory.create()
  28. User = get_user_model()
  29. total_users = User.objects.count()
  30. self.stdout.write('Creating fake threads...\n')
  31. message = '\nSuccessfully created %s fake threads in %s'
  32. created_threads = 0
  33. start_time = time.time()
  34. show_progress(self, created_threads, items_to_create)
  35. while created_threads < items_to_create:
  36. with atomic():
  37. datetime = timezone.now()
  38. category = random.choice(categories)
  39. user = User.objects.order_by('?')[:1][0]
  40. thread_is_unapproved = random.randint(0, 100) > 90
  41. thread_is_hidden = random.randint(0, 100) > 90
  42. thread_is_closed = random.randint(0, 100) > 90
  43. thread = Thread(
  44. category=category,
  45. started_on=datetime,
  46. starter_name='-',
  47. starter_slug='-',
  48. last_post_on=datetime,
  49. last_poster_name='-',
  50. last_poster_slug='-',
  51. replies=0,
  52. is_unapproved=thread_is_unapproved,
  53. is_hidden=thread_is_hidden,
  54. is_closed=thread_is_closed
  55. )
  56. thread.set_title(fake.sentence())
  57. thread.save()
  58. fake_message = "\n\n".join(fake.paragraphs())
  59. post = Post.objects.create(
  60. category=category,
  61. thread=thread,
  62. poster=user,
  63. poster_name=user.username,
  64. poster_ip=fake.ipv4(),
  65. original=fake_message,
  66. parsed=linebreaks_filter(fake_message),
  67. posted_on=datetime,
  68. updated_on=datetime
  69. )
  70. update_post_checksum(post)
  71. post.save(update_fields=['checksum'])
  72. thread.set_first_post(post)
  73. thread.set_last_post(post)
  74. thread.save()
  75. user.threads += 1
  76. user.posts += 1
  77. user.save()
  78. thread_type = random.randint(0, 100)
  79. if thread_type > 95:
  80. thread_replies = random.randint(200, 2500)
  81. elif thread_type > 50:
  82. thread_replies = random.randint(5, 30)
  83. else:
  84. thread_replies = random.randint(0, 10)
  85. for x in range(thread_replies):
  86. datetime = timezone.now()
  87. user = User.objects.order_by('?')[:1][0]
  88. fake_message = "\n\n".join(fake.paragraphs())
  89. is_unapproved = random.randint(0, 100) > 97
  90. if not is_unapproved:
  91. is_hidden = random.randint(0, 100) > 97
  92. else:
  93. is_hidden = False
  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=fake_message,
  101. parsed=linebreaks_filter(fake_message),
  102. is_hidden=is_hidden,
  103. is_unapproved=is_unapproved,
  104. posted_on=datetime,
  105. updated_on=datetime
  106. )
  107. update_post_checksum(post)
  108. post.save(update_fields=['checksum'])
  109. user.posts += 1
  110. user.save()
  111. thread.synchronize()
  112. thread.save()
  113. created_threads += 1
  114. show_progress(
  115. self, created_threads, items_to_create, start_time)
  116. pinned_threads = random.randint(0, int(created_threads * 0.025)) or 1
  117. self.stdout.write('\nPinning %s threads...' % pinned_threads)
  118. for i in range(0, pinned_threads):
  119. thread = Thread.objects.order_by('?')[:1][0]
  120. if random.randint(0, 100) > 75:
  121. thread.weight = 2
  122. else:
  123. thread.weight = 1
  124. thread.save()
  125. for category in categories:
  126. category.synchronize()
  127. category.save()
  128. total_time = time.time() - start_time
  129. total_humanized = time.strftime('%H:%M:%S', time.gmtime(total_time))
  130. self.stdout.write(message % (created_threads, total_humanized))