synchronizeusers.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. import time
  2. from django.contrib.auth import get_user_model
  3. from django.core.management.base import BaseCommand
  4. from misago.core.management.progressbar import show_progress
  5. from misago.core.pgutils import batch_update
  6. class Command(BaseCommand):
  7. help = 'Synchronizes users'
  8. def handle(self, *args, **options):
  9. users_to_sync = get_user_model().objects.count()
  10. if not users_to_sync:
  11. self.stdout.write('\n\nNo users were found')
  12. else:
  13. self.sync_users(users_to_sync)
  14. def sync_users(self, users_to_sync):
  15. message = 'Synchronizing %s users...\n'
  16. self.stdout.write(message % users_to_sync)
  17. message = '\n\nSynchronized %s users'
  18. synchronized_count = 0
  19. show_progress(self, synchronized_count, users_to_sync)
  20. start_time = time.time()
  21. for user in batch_update(get_user_model().objects.all()):
  22. user.threads = user.thread_set.count()
  23. user.posts = user.post_set.count()
  24. user.followers = user.followed_by.count()
  25. user.following = user.follows.count()
  26. user.save()
  27. synchronized_count += 1
  28. show_progress(self, synchronized_count, users_to_sync, start_time)
  29. self.stdout.write(message % synchronized_count)