rebuildpostssearch.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. import time
  2. from django.core.management.base import BaseCommand
  3. from misago.core.management.progressbar import show_progress
  4. from misago.core.pgutils import chunk_queryset
  5. from misago.threads.models import Post
  6. class Command(BaseCommand):
  7. help = "Rebuilds posts search"
  8. def handle(self, *args, **options):
  9. posts_to_sync = Post.objects.filter(is_event=False).count()
  10. if not posts_to_sync:
  11. self.stdout.write("\n\nNo posts were found")
  12. else:
  13. self.sync_threads(posts_to_sync)
  14. def sync_threads(self, posts_to_sync):
  15. message = "Rebuilding %s posts...\n"
  16. self.stdout.write(message % posts_to_sync)
  17. message = "\n\nRebuild %s posts"
  18. synchronized_count = 0
  19. show_progress(self, synchronized_count, posts_to_sync)
  20. start_time = time.time()
  21. queryset = Post.objects.select_related('thread').filter(is_event=False)
  22. for post in chunk_queryset(queryset):
  23. if post.id == post.thread.first_post_id:
  24. post.set_search_document(post.thread.title)
  25. else:
  26. post.set_search_document()
  27. post.save(update_fields=['search_document'])
  28. post.update_search_vector()
  29. post.save(update_fields=['search_vector'])
  30. synchronized_count += 1
  31. show_progress(self, synchronized_count, posts_to_sync, start_time)
  32. self.stdout.write(message % synchronized_count)