# Generated by Django 3.0.5 on 2020-04-27 17:55

import json
from django.db import migrations


def insert_data(apps, schema_editor):
    # We can't import the models directly as they may be a newer
    # version than this migration expects. We use the historical version.
    Question = apps.get_model('quiz_app', 'Question')
    Answer = apps.get_model('quiz_app', 'Answer')
    CorrectAnswer = apps.get_model('quiz_app', 'CorrectAnswer')
    with open('data.json') as f:
        gre_data = json.load(f)
        for data in gre_data:
            q = Question.objects.create(question_text=data['question'])
            for answer in data['answers'].values():
                Answer.objects.create(question=q, answer_text=answer)
            CorrectAnswer.objects.create(question=q,
                                         answer=Answer.objects.get(question=q,
                                                                   answer_text=data['answers'][data['correct_answer']]))


class Migration(migrations.Migration):

    dependencies = [
        ('quiz_app', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(insert_data),
    ]
