# Generated by Django 1.10.5 on 2017-01-07 17:29
from __future__ import unicode_literals

import datetime

from django.db import migrations
from django.utils import timezone


def add_timestamps_to_event(event):
    """ This will:

        add a thank-you-email-sent timestamp to all events in the past
        add a submit-information-email-sent timestamp to all events in the past
    """
    today = datetime.date.today()
    if event.date < today:
        if (
            not all((event.date.year, event.date.month, event.date.day)) and
            event.date.month == today.month and
            event.date.year == today.year
        ):
            # for events that don't have a firm date, if year and month are the same as today then we can't know
            # for sure that this event is already over.
            return

        event.thank_you_email_sent = timezone.now()
        event.submit_information_email_sent = timezone.now()
        event.save(update_fields=['thank_you_email_sent', 'submit_information_email_sent'])


def set_default_email_flags(apps, schema_editor):
    Event = apps.get_model("core", "Event")
    for event in Event.objects.all():
        add_timestamps_to_event(event)


class Migration(migrations.Migration):

    dependencies = [
        ('core', '0029_auto_20170107_1539'),
    ]

    operations = [
        migrations.RunPython(set_default_email_flags),
    ]

Add a data migration to make sure we don’t send a lot of emails to old events
Add a data migration to make sure we don’t send a lot of emails to old events
