Django 1.7 compatibility fix.
[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
10 from django.core.management.base import NoArgsCommand
11 from django.conf import settings
12
13 import datetime
14
15 from cas_provider.models import ServiceTicket, LoginTicket
16
17 class Command(NoArgsCommand):
18     help = "Delete expired service tickets from the database"
19
20     def handle_noargs(self, **options):
21         print "Service tickets:"
22         tickets = ServiceTicket.objects.all()
23         for ticket in tickets:
24             expiration = datetime.timedelta(minutes=settings.CAS_TICKET_EXPIRATION)
25             if datetime.datetime.now() > ticket.created + expiration:
26                 print "Deleting %s..." % ticket.ticket
27                 ticket.delete()
28             else:
29                 print "%s not expired..." % ticket.ticket
30         tickets = LoginTicket.objects.all()
31         print "Login tickets:"
32         for ticket in tickets:
33             expiration = datetime.timedelta(minutes=settings.CAS_TICKET_EXPIRATION)
34             if datetime.datetime.now() > ticket.created + expiration:
35                 print "Deleting %s..." % ticket.ticket
36                 ticket.delete()
37             else:
38                 print "%s not expired..." % ticket.ticket