expirelinks.py 702 Bytes
Newer Older
1
# Django Imports
2 3 4
from django.core.management.base import BaseCommand
from django.utils import timezone

5 6
# App Imports
from go.models import URL
7

8
# Define a new custom django-admin command
9
class Command(BaseCommand):
10
    # Define help text for this command
11 12
    help = 'Removes expired links from the database'

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