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

5
# Django Imports
6 7 8
from django.core.management.base import BaseCommand
from django.utils import timezone

9 10
# App Imports
from go.models import URL
11 12

class Command(BaseCommand):
13
    """
14 15
    Define a new custom django-admin command to remove expired links from the
    database
16
    """
17

18
    # Define help text for this command
19 20 21
    help = 'Removes expired links from the database'

    def handle(self, *args, **options):
22
        """
David Haynes's avatar
David Haynes committed
23 24
        The handle function handles the main component of the django-admin 
        command.
25 26
        """

27 28
        # Loop through a list of all URL objects that have expired
        # (expires field is less than or equal to today's date)
29
        for toexpire in URL.objects.filter(expires__lte=timezone.now()):
30
            # Delete the current URL
31
            toexpire.delete()