# -*- coding: utf-8 -*-
# Generated by Django 1.9.7 on 2017-05-01 09:04
from __future__ import unicode_literals

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


def set_branch_creator(apps, schema_editor):
    ProblemBranch = apps.get_model("problems", "ProblemBranch")
    db_alias = schema_editor.connection.alias
    for obj in ProblemBranch.objects.using(db_alias).all():
        if obj.name != "master":
            if obj.working_copy:
                obj.creator = obj.working_copy.author
            else:
                obj.creator = obj.head.author
            obj.save()


class Migration(migrations.Migration):

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        ('problems', '0084_auto_20170427_0939'),
    ]

    operations = [
        migrations.AddField(
            model_name='problembranch',
            name='creator',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, related_name='+', to=settings.AUTH_USER_MODEL, verbose_name='creator'),
        ),
        migrations.RunPython(set_branch_creator, migrations.RunPython.noop),
        migrations.AlterField(
            model_name='problem',
            name='creator',
            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='+', to=settings.AUTH_USER_MODEL, verbose_name='creator'),
        ),
    ]
