# -*- coding: utf-8 -*-
# Generated by Django 1.10.7 on 2017-06-08 13:01
from __future__ import unicode_literals

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


def assign_hole_to_space(apps, schema_editor):
    Hole = apps.get_model('mapdata', 'Hole')
    Space = apps.get_model('mapdata', 'Space')
    for hole in Hole.objects.filter():
        space = max(hole.section.spaces.filter(level='normal'),
                    key=lambda s: s.geometry.intersection(hole.geometry).area)
        hole.space = space
        hole.save()


class Migration(migrations.Migration):

    dependencies = [
        ('mapdata', '0006_remove_section_name'),
    ]

    operations = [
        migrations.AddField(
            model_name='hole',
            name='space',
            field=models.ForeignKey(default=0, on_delete=django.db.models.deletion.CASCADE, related_name='holes', to='mapdata.Space', verbose_name='space'),
            preserve_default=False,
        ),
        migrations.RunPython(assign_hole_to_space),
    ]
