expirelinks.py 967 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
# Django Imports
10 11 12
from django.core.management.base import BaseCommand
from django.utils import timezone

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

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

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

    def handle(self, *args, **options):
26
        """
David Haynes's avatar
David Haynes committed
27 28
        The handle function handles the main component of the django-admin 
        command.
29 30
        """

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()