# -*- coding: utf-8 -*-
# Generated by Django 1.11.2 on 2017-07-13 21:42
from __future__ import unicode_literals

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


def door_access_restriction(apps, schema_editor):
    Level = apps.get_model('mapdata', 'Level')
    for level in Level.objects.all():
        spaces = level.spaces.all()
        for door in level.doors.all():
            public = 0
            for space in spaces:
                if space.access_restriction_id is None and space.geometry.intersects(door.geometry):
                    public += 1
                    if public == 2:
                        break
            else:
                door.access_restriction_id = 1
                door.save()


class Migration(migrations.Migration):

    dependencies = [
        ('mapdata', '0027_access_restriction'),
    ]

    operations = [
        migrations.AddField(
            model_name='door',
            name='access_restriction',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='doors', to='mapdata.AccessRestriction', verbose_name='Access Restriction'),
        ),
        migrations.RunPython(door_access_restriction),
    ]
