synchronizethreads.py 1.1 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 batch_update
  5. from misago.threads.models import Thread
  6. class Command(BaseCommand):
  7. help = "Synchronizes threads"
  8. def handle(self, *args, **options):
  9. threads_to_sync = Thread.objects.count()
  10. if not threads_to_sync:
  11. self.stdout.write("\n\nNo threads were found")
  12. else:
  13. self.sync_threads(threads_to_sync)
  14. def sync_threads(self, threads_to_sync):
  15. message = "Synchronizing %s threads...\n"
  16. self.stdout.write(message % threads_to_sync)
  17. message = "\n\nSynchronized %s threads"
  18. synchronized_count = 0
  19. show_progress(self, synchronized_count, threads_to_sync)
  20. start_time = time.time()
  21. for thread in batch_update(Thread.objects.all()):
  22. thread.synchronize()
  23. thread.save()
  24. synchronized_count += 1
  25. show_progress(self, synchronized_count, threads_to_sync, start_time)
  26. self.stdout.write(message % synchronized_count)