# -*- coding: utf-8 -*-
# Generated by Django 1.9.6 on 2016-06-21 16:28
from __future__ import unicode_literals

import datetime
from django.db import migrations


def print_date_to_daterange(apps, schema_editor):
    """Convert the legacy pring_run_date fields to new range fields"""
    Package = apps.get_model("budget", "Package")
    for package in Package.objects.filter(print_run_date_old__isnull=False):
        lower_bound = package.print_run_date_old
        upper_bound = lower_bound + datetime.timedelta(days=1)
        package.print_run_date = (lower_bound, upper_bound)
        package.save()


def default_dateranges(apps, schema_editor):
    """Walk back the date range migration to the field defaults"""
    Package = apps.get_model("budget", "Package")
    for package in Package.objects.all():
        package.print_run_date = None
        package.save()


class Migration(migrations.Migration):

    dependencies = [
        ('budget', '0010_package_print_run_date'),
    ]

    operations = [
        migrations.RunPython(
            print_date_to_daterange,
            default_dateranges
        )
    ]
