synchronizeusers.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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 chunk_queryset
  7. User = get_user_model()
  8. class Command(BaseCommand):
  9. help = "Synchronizes users"
  10. def handle(self, *args, **options):
  11. users_to_sync = User.objects.count()
  12. if not users_to_sync:
  13. self.stdout.write("\n\nNo users were found")
  14. else:
  15. self.sync_users(users_to_sync)
  16. def sync_users(self, users_to_sync):
  17. categories = Category.objects.root_category().get_descendants()
  18. self.stdout.write("Synchronizing %s users...\n" % 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 chunk_queryset(User.objects.all()):
  23. user.threads = user.thread_set.filter(
  24. category__in=categories, is_hidden=False, is_unapproved=False
  25. ).count()
  26. user.posts = user.post_set.filter(
  27. category__in=categories, is_event=False, is_unapproved=False
  28. ).count()
  29. user.followers = user.followed_by.count()
  30. user.following = user.follows.count()
  31. user.save()
  32. synchronized_count += 1
  33. show_progress(self, synchronized_count, users_to_sync, start_time)
  34. self.stdout.write("\n\nSynchronized %s users" % synchronized_count)