updatepostschecksums.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  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. message = "Updating %s posts checksums...\n"
  17. self.stdout.write(message % posts_to_update)
  18. updated_count = 0
  19. show_progress(self, updated_count, posts_to_update)
  20. start_time = time.time()
  21. queryset = Post.objects.filter(is_event=False)
  22. for post in chunk_queryset(queryset):
  23. update_post_checksum(post)
  24. post.save(update_fields=['checksum'])
  25. updated_count += 1
  26. show_progress(self, updated_count, posts_to_update, start_time)
  27. self.stdout.write("\n\nUpdated %s posts checksums" % updated_count)