Support for Django 3.2
[django-cas-provider.git] / cas_provider / management / commands / cleanuptickets.py
1 """
2 A management command which deletes expired service tickets (e.g.,
3 from the database.
4
5 Calls ``ServiceTickets.objects.delete_expired_users()``, which
6 contains the actual logic for determining which accounts are deleted.
7
8 """
9 from django.core.management.base import NoArgsCommand
10 from django.conf import settings
11
12 import datetime
13
14 from cas_provider.models import ServiceTicket, LoginTicket
15
16 class Command(NoArgsCommand):
17     help = "Delete expired service tickets from the database"
18
19     def handle_noargs(self, **options):
20         print("Service tickets:")
21         tickets = ServiceTicket.objects.all()
22         for ticket in tickets:
23             expiration = datetime.timedelta(minutes=settings.CAS_TICKET_EXPIRATION)
24             if datetime.datetime.now() > ticket.created + expiration:
25                 print("Deleting %s..." % ticket.ticket)
26                 ticket.delete()
27             else:
28                 print("%s not expired..." % ticket.ticket)
29         tickets = LoginTicket.objects.all()
30         print("Login tickets:")
31         for ticket in tickets:
32             expiration = datetime.timedelta(minutes=settings.CAS_TICKET_EXPIRATION)
33             if datetime.datetime.now() > ticket.created + expiration:
34                 print("Deleting %s..." % ticket.ticket)
35                 ticket.delete()
36             else:
37                 print("%s not expired..." % ticket.ticket)