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

import c3nav.mapdata.fields
from django.db import migrations, models
import django.db.models.deletion


def convert_public_into_access_restriction(apps, schema_editor):
    AccessRestriction = apps.get_model('mapdata', 'AccessRestriction')
    access_restriction = AccessRestriction.objects.create(titles={
        'en': 'non-public',
        'de': 'nichtöffentlich',
    })

    for model_name in ('Level', 'Space', 'Area', 'POI', 'LocationGroup'):
        model = apps.get_model('mapdata', model_name)
        model.objects.filter(public=False).update(access_restriction=access_restriction)

    Source = apps.get_model('mapdata', 'Source')
    Source.objects.update(access_restriction=access_restriction)


class Migration(migrations.Migration):

    dependencies = [
        ('mapdata', '0026_remove_specificlocation_color'),
    ]

    operations = [
        migrations.CreateModel(
            name='AccessRestriction',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('titles', c3nav.mapdata.fields.JSONField(default={})),
            ],
            options={
                'verbose_name': 'Access Restriction',
                'verbose_name_plural': 'Access Restrictions',
                'default_related_name': 'accessrestrictions',
            },
        ),
        migrations.AddField(
            model_name='area',
            name='access_restriction',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE,
                                    related_name='areas', to='mapdata.AccessRestriction',
                                    verbose_name='Access Restriction'),
        ),
        migrations.AddField(
            model_name='level',
            name='access_restriction',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE,
                                    related_name='levels', to='mapdata.AccessRestriction',
                                    verbose_name='Access Restriction'),
        ),
        migrations.AddField(
            model_name='locationgroup',
            name='access_restriction',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE,
                                    related_name='locationgroups', to='mapdata.AccessRestriction',
                                    verbose_name='Access Restriction'),
        ),
        migrations.AddField(
            model_name='poi',
            name='access_restriction',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE,
                                    related_name='pois', to='mapdata.AccessRestriction',
                                    verbose_name='Access Restriction'),
        ),
        migrations.AddField(
            model_name='source',
            name='access_restriction',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE,
                                    related_name='sources', to='mapdata.AccessRestriction',
                                    verbose_name='Access Restriction'),
        ),
        migrations.AddField(
            model_name='space',
            name='access_restriction',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE,
                                    related_name='spaces', to='mapdata.AccessRestriction',
                                    verbose_name='Access Restriction'),
        ),
        migrations.RunPython(convert_public_into_access_restriction),
        migrations.RemoveField(
            model_name='area',
            name='public',
        ),
        migrations.RemoveField(
            model_name='level',
            name='public',
        ),
        migrations.RemoveField(
            model_name='locationgroup',
            name='public',
        ),
        migrations.RemoveField(
            model_name='poi',
            name='public',
        ),
        migrations.RemoveField(
            model_name='space',
            name='public',
        ),
    ]
