# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-06-10 11:15
from __future__ import unicode_literals

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


def move_upper_spaces_to_section_on_top(apps, schema_editor):
    Section = apps.get_model('mapdata', 'Section')
    Space = apps.get_model('mapdata', 'Space')
    Space.objects.filter(level='lower').delete()
    for section in Section.objects.all():
        if Space.objects.filter(level='upper', section=section).exists():
            section_on_top_of = section.sections_on_top.create(altitude=section.altitude+Decimal('0.01'), public=section.public,
                                                               can_search=False, can_describe=False)
            Space.objects.filter(level='upper', section=section).update(section=section_on_top_of, outside=True)



class Migration(migrations.Migration):

    dependencies = [
        ('mapdata', '0009_column'),
    ]

    operations = [
        migrations.AddField(
            model_name='section',
            name='on_top_of',
            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE,
                                    related_name='sections_on_top', to='mapdata.Section', verbose_name='on top of'),
        ),
        migrations.RunPython(move_upper_spaces_to_section_on_top),
        migrations.RemoveField(
            model_name='space',
            name='level',
        ),
    ]
