# -*- coding: utf-8 -*-
# Generated by Django 1.10.3 on 2017-01-20 08:26
from __future__ import unicode_literals

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


class Migration(migrations.Migration):

    def set_last_activated_on_active_appservers(apps, schema_editor):
        """
        Ensure active AppServers have a value for `last_activated`
        """
        # We can't import the OpenEdXInstance model directly as it may be a newer
        # version than this migration expects. We use the historical version.
        OpenEdXInstance = apps.get_model("instance", "OpenEdXInstance")
        now = timezone.now()
        for instance in OpenEdXInstance.objects.all():
            if instance.active_appserver and instance.active_appserver.last_activated is None:
                instance.active_appserver.last_activated = now
                instance.active_appserver.save()

    dependencies = [
        ('instance', '0070_merge_20170120_0718'),
    ]

    operations = [
        migrations.RunPython(set_last_activated_on_active_appservers),
    ]
