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

5
# Future Imports
David Haynes's avatar
David Haynes committed
6 7 8
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

9

10
# Django Imports
11 12 13
from django.core.management.base import BaseCommand
from django.utils import timezone

14 15
# App Imports
from go.models import URL
16 17

class Command(BaseCommand):
18
    """
19 20
    Define a new custom django-admin command to remove expired links from the
    database
21
    """
22

23
    # Define help text for this command
24 25 26
    help = 'Removes expired links from the database'

    def handle(self, *args, **options):
27 28 29 30
        """
        The handle function handles the main component of the django-admin command.
        """

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