# -*- coding: utf-8 -*-
# Generated by Django 1.10.2 on 2016-11-01 10:29
from __future__ import unicode_literals

import datetime
from django.db import migrations, models
import django.db.models.deletion


def forward(apps, schema_editor):
    Participant = apps.get_model('content', 'Participant')
    ParticipantAnswer = apps.get_model('content', 'ParticipantAnswer')
    Entry = apps.get_model('content', 'Entry')

    for participant in Participant.objects.all():
        entry = Entry.objects.create(participant_id=participant.id, date_completed=participant.date_created)
        ParticipantAnswer.objects.filter(participant_id=participant.id).update(entry_id=entry.id)


def backward(apps, schema_editor):
    Participant = apps.get_model('content', 'Participant')
    ParticipantAnswer = apps.get_model('content', 'ParticipantAnswer')

    for participant in Participant.objects.all():
        ParticipantAnswer.objects.filter(entry__participant_id=participant.id).update(participant_id=participant.id)


class Migration(migrations.Migration):

    dependencies = [
        ('content', '0011_participantfreetext_participantpicture'),
    ]

    operations = [
        migrations.CreateModel(
            name='Entry',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('date_saved', models.DateTimeField(default=datetime.datetime.now, verbose_name='saved on')),
                ('date_completed', models.DateTimeField(null=True, verbose_name='completed on')),
                ('participant', models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='entries', to='content.Participant')),
            ],
            options={
                'verbose_name': 'entry',
                'verbose_name_plural': 'entries',
            },
        ),
        migrations.AddField(
            model_name='participantanswer',
            name='entry',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='answers', to='content.Entry'),
        ),
        migrations.RunPython(forward, backward),
        migrations.RemoveField(
            model_name='participantanswer',
            name='participant',
        ),
    ]
