<filename>exams/migrations/0008_populate_exam_run.py
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-04-24 19:10
from __future__ import unicode_literals
from datetime import datetime

from django.db import migrations
import pytz


PILOT_SERIES_CODE = 'PILOT'
PILOT_SCHEDULE_START_DATE = datetime(2017, 3, 6, tzinfo=pytz.UTC)
PILOT_SCHEDULE_END_DATE = datetime(2017, 3, 30, tzinfo=pytz.UTC)
PILOT_ELIGIBILITY_START_DATE = datetime(2017, 3, 18, tzinfo=pytz.UTC)
PILOT_ELIGIBILITY_END_DATE = datetime(2017, 3, 31, tzinfo=pytz.UTC)


def populate_pilot_runs(apps, schema_editor):
    """Generate pilot ExamRuns"""
    ExamRun = apps.get_model('exams', 'ExamRun')
    ExamAuthorization = apps.get_model('exams', 'ExamAuthorization')
    runs_by_course_id = {}

    for exam_auth in ExamAuthorization.objects.filter(exam_run__isnull=True).iterator():
        if exam_auth.course_id not in runs_by_course_id:
            runs_by_course_id[exam_auth.course_id] = ExamRun.objects.create(
                course=exam_auth.course,
                exam_series_code=PILOT_SERIES_CODE,
                date_first_schedulable=PILOT_SCHEDULE_START_DATE,
                date_last_schedulable=PILOT_SCHEDULE_END_DATE,
                date_first_eligible=PILOT_ELIGIBILITY_START_DATE,
                date_last_eligible=PILOT_ELIGIBILITY_END_DATE,
            )
        exam_auth.exam_run = runs_by_course_id[exam_auth.course_id]
        exam_auth.save(update_fields=('exam_run',))

class Migration(migrations.Migration):

    dependencies = [
        ('exams', '0007_add_exam_run'),
    ]

    operations = [
        migrations.RunPython(populate_pilot_runs, migrations.RunPython.noop)
    ]

<filename>exams/migrations/0008_populate_exam_run.py
# -*- coding: utf-8 -*-
# Generated by Django 1.10.5 on 2017-04-24 19:10
from __future__ import unicode_literals
from datetime import datetime

from django.db import migrations
import pytz


PILOT_SERIES_CODE = 'PILOT'
PILOT_SCHEDULE_START_DATE = datetime(2017, 3, 6, tzinfo=pytz.UTC)
PILOT_SCHEDULE_END_DATE = datetime(2017, 3, 30, tzinfo=pytz.UTC)
PILOT_ELIGIBILITY_START_DATE = datetime(2017, 3, 18, tzinfo=pytz.UTC)
PILOT_ELIGIBILITY_END_DATE = datetime(2017, 3, 31, tzinfo=pytz.UTC)


def populate_pilot_runs(apps, schema_editor):
    """Generate pilot ExamRuns"""
    ExamRun = apps.get_model('exams', 'ExamRun')
    ExamAuthorization = apps.get_model('exams', 'ExamAuthorization')
    runs_by_course_id = {}

    for exam_auth in ExamAuthorization.objects.filter(exam_run__isnull=True).iterator():
        if exam_auth.course_id not in runs_by_course_id:
            runs_by_course_id[exam_auth.course_id] = ExamRun.objects.create(
                course=exam_auth.course,
                exam_series_code=PILOT_SERIES_CODE,
                date_first_schedulable=PILOT_SCHEDULE_START_DATE,
                date_last_schedulable=PILOT_SCHEDULE_END_DATE,
                date_first_eligible=PILOT_ELIGIBILITY_START_DATE,
                date_last_eligible=PILOT_ELIGIBILITY_END_DATE,
            )
        exam_auth.exam_run = runs_by_course_id[exam_auth.course_id]
        exam_auth.save(update_fields=('exam_run',))

class Migration(migrations.Migration):

    dependencies = [
        ('exams', '0007_add_exam_run'),
    ]

    operations = [
        migrations.RunPython(populate_pilot_runs, migrations.RunPython.noop)
    ]

# -*- coding: utf-8 -*- # Generated by Django 1.10.5 on 2017-04-24 19:10 Generate pilot ExamRuns