synchronizeusers.py 1.6 KB

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