updatepostschecksums.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  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.checksums import update_post_checksum
  6. from misago.threads.models import Post
  7. class Command(BaseCommand):
  8. help = "Updates posts checksums"
  9. def handle(self, *args, **options):
  10. posts_to_update = Post.objects.filter(is_event=False).count()
  11. if not posts_to_update:
  12. self.stdout.write("\n\nNo posts were found")
  13. else:
  14. self.update_posts_checksums(posts_to_update)
  15. def update_posts_checksums(self, posts_to_update):
  16. self.stdout.write("Updating %s posts checksums...\n" % posts_to_update)
  17. updated_count = 0
  18. show_progress(self, updated_count, posts_to_update)
  19. start_time = time.time()
  20. queryset = Post.objects.filter(is_event=False)
  21. for post in chunk_queryset(queryset):
  22. update_post_checksum(post)
  23. post.save(update_fields=['checksum'])
  24. updated_count += 1
  25. show_progress(self, updated_count, posts_to_update, start_time)
  26. self.stdout.write("\n\nUpdated %s posts checksums" % updated_count)