expirelinks.py 871 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
# Define a new custom django-admin command
12
class Command(BaseCommand):
13 14 15 16
    """
        Remove expired links from the database
    """
    
17
    # Define help text for this command
18 19
    help = 'Removes expired links from the database'

20
    # The handle function handles the main component of the django-admin command
21
    def handle(self, *args, **options):
22 23
        # Loop through a list of all URL objects that have expired
        # (expires field is less than or equal to today's date)
24
        for toexpire in URL.objects.filter(expires__lte=timezone.now()):
25
            # Delete the current URL
26
            toexpire.delete()