expirelinks.py 803 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
    # Define help text for this command
14 15
    help = 'Removes expired links from the database'

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