invalidatebans.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. from django.core.management.base import BaseCommand
  2. from django.utils import timezone
  3. from misago.cache.versions import get_cache_versions
  4. from misago.users import BANS_CACHE
  5. from misago.users.models import Ban, BanCache
  6. class Command(BaseCommand):
  7. help = (
  8. "Runs maintenance on Misago bans system, "
  9. "invalidating expired bans and pruning caches."
  10. )
  11. def handle(self, *args, **options):
  12. self.handle_expired_bans()
  13. self.handle_bans_caches()
  14. def handle_expired_bans(self):
  15. queryset = Ban.objects.filter(is_checked=True)
  16. queryset = queryset.filter(expires_on__lt=timezone.now())
  17. expired_count = queryset.update(is_checked=False)
  18. self.stdout.write("Bans invalidated: %s" % expired_count)
  19. def handle_bans_caches(self):
  20. queryset = BanCache.objects.filter(expires_on__lt=timezone.now())
  21. expired_count = queryset.count()
  22. queryset.delete()
  23. cache_versions = get_cache_versions()
  24. queryset = BanCache.objects.exclude(cache_version=cache_versions[BANS_CACHE])
  25. expired_count += queryset.count()
  26. queryset.delete()
  27. self.stdout.write("Ban caches emptied: %s" % expired_count)