expirelinks.py 896 Bytes
Newer Older
1 2 3
# Future Imports
from __future__ import unicode_literals, absolute_import, print_function, division

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

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

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

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

    def handle(self, *args, **options):
21 22 23 24
        """
        The handle function handles the main component of the django-admin command.
        """

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