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