clearsessions.py 661 B

123456789101112131415161718192021
  1. from importlib import import_module
  2. from django.conf import settings
  3. from django.core.management.base import BaseCommand, CommandError
  4. class Command(BaseCommand):
  5. help = (
  6. "Can be run as a cronjob or directly to clean out expired sessions "
  7. "when the backend supports it."
  8. )
  9. def handle(self, **options):
  10. engine = import_module(settings.SESSION_ENGINE)
  11. try:
  12. engine.SessionStore.clear_expired()
  13. except NotImplementedError:
  14. raise CommandError(
  15. "Session engine '%s' doesn't support clearing expired "
  16. "sessions." % settings.SESSION_ENGINE
  17. )