ParticleDecay
2/14/2013 - 8:15 PM

[Django] Custom management command to check for all events starting in an arbitrary number of days, and send event reminder emails for them

[Django] Custom management command to check for all events starting in an arbitrary number of days, and send event reminder emails for them (run from a cron job as follows for 10-day, 3-day, and 1-day checks: 0 2 * * * /virtualenvs/foo/bin/python /projects/foo/foo/manage.py check_reminders 10 3 1)

from datetime import datetime, timedelta

from django.core.management.base import BaseCommand, CommandError
from foo.firms.models import Event

class Command(BaseCommand):
    args = '<days days ...>'
    help = "Check for any Events that start in a given number of days, and execute send_reminder() on them"

    def handle(self, *args, **kwargs):
        if args:
            days = args
        else:
            days = [10]

        for day in days:
            day = int(day)
            dt = datetime.now().date() + timedelta(days=day)
            upcoming_events = Event.objects.filter(event_start_date__year=dt.year, event_start_date__month=dt.month, event_start_date__day=dt.day)

            [event.send_reminder(day) for event in upcoming_events]