# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2019-02-18 18:36
from __future__ import unicode_literals

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


class Migration(migrations.Migration):

    dependencies = [("topologie", "0070_auto_20190218_1743")]

    def transfer_room(apps, schema_editor):
        db_alias = schema_editor.connection.alias
        room_obj = apps.get_model("topologie", "Room")
        building_obj = apps.get_model("topologie", "Building")
        dorm_obj = apps.get_model("topologie", "Dormitory")
        dorm = dorm_obj.objects.using(db_alias).first()
        for room in room_obj.objects.using(db_alias).all():
            building, created = building_obj.objects.using(db_alias).get_or_create(
                name=room.name[0].upper(), dormitory=dorm
            )
            room.building = building
            room.name = room.name[1:]
            room.save()

    def untransfer_room(apps, schema_editor):
        pass

    operations = [
        migrations.AlterField(
            model_name="room", name="name", field=models.CharField(max_length=255)
        ),
        migrations.AddField(
            model_name="room",
            name="building",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                to="topologie.Building",
            ),
        ),
        migrations.AlterUniqueTogether(
            name="room", unique_together=set([("name", "building")])
        ),
        migrations.AlterField(
            model_name="building",
            name="dormitory",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.PROTECT, to="topologie.Dormitory"
            ),
        ),
        migrations.RunPython(transfer_room, untransfer_room),
        migrations.AlterField(
            model_name="room",
            name="building",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.PROTECT, to="topologie.Building"
            ),
        ),
    ]
