expirelinks.py 907 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
# Future Imports
David Haynes's avatar
David Haynes committed
7 8 9
from __future__ import (absolute_import, division, print_function,
                        unicode_literals)

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
    Define a new custom django-admin command to remove expired links from the
20
    database.
21
    """
22 23 24
    help = 'Removes expired links from the database'

    def handle(self, *args, **options):
25
        """
26 27 28
        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)
29
        """
30 31
        for expired_url in URL.objects.filter(expires__lte=timezone.now()):
            expired_url.delete()