expirelinks.py 785 Bytes
Newer Older
David Haynes's avatar
David Haynes committed
1 2 3
"""
go/commands/expirelinks.py

4 5
Remove expired links from the database.
"""
6
# Django Imports
7 8 9
from django.core.management.base import BaseCommand
from django.utils import timezone

10
# App Imports
David Haynes's avatar
David Haynes committed
11
from go_back.models import URL
12 13

class Command(BaseCommand):
14
    """
15
    Define a new custom django-admin command to remove expired links from the
16
    database.
17
    """
18 19 20
    help = 'Removes expired links from the database'

    def handle(self, *args, **options):
21
        """
22 23 24
        Handle the main component of the django-admin command. Loop
        through a list of all URL objects that have expired (expires field is
        less than or equal [lte] to today's date)
25
        """
26 27
        for expired_url in URL.objects.filter(expires__lte=timezone.now()):
            expired_url.delete()