# Generated by Django 2.2.24 on 2021-10-20 12:13

from django.db import migrations

from basicknowledge.models import INTERDISCIPLINARY, LANGUAGE_COMMUNICATION, SOCIETY


def create_types(apps, schema_editor):
    BasicKnowledge = apps.get_model('basicknowledge', 'BasicKnowledge')
    InstrumentType = apps.get_model('basicknowledge', 'InstrumentType')
    language_type=InstrumentType.objects.create(
        name='Sprache & Kommunikation',
        category=LANGUAGE_COMMUNICATION
    )
    society_type=InstrumentType.objects.create(
        name='Gesellschaft',
        category=SOCIETY
    )
    interdisciplinary_type=InstrumentType.objects.create(
        name='Überfachliches Instrument',
        category=INTERDISCIPLINARY
    )
    instruments = []
    for instrument in BasicKnowledge.objects.filter(type=LANGUAGE_COMMUNICATION):
        instrument.new_type=language_type
        instruments.append(instrument)
    for instrument in BasicKnowledge.objects.filter(type=SOCIETY):
        instrument.new_type=society_type
        instruments.append(instrument)
    for instrument in BasicKnowledge.objects.filter(type=INTERDISCIPLINARY):
        instrument.new_type=interdisciplinary_type
        instruments.append(instrument)

    BasicKnowledge.objects.bulk_update(instruments, ['new_type'])


def delete_types(apps, schema_editor):
    BasicKnowledge = apps.get_model('basicknowledge', 'BasicKnowledge')
    InstrumentType = apps.get_model('basicknowledge', 'InstrumentType')
    instruments = []
    for instrument in BasicKnowledge.objects.all():
        instrument.new_type = None
        instruments.append(instrument)
    BasicKnowledge.objects.bulk_update(instruments, ['new_type'])
    InstrumentType.objects.all().delete()


class Migration(migrations.Migration):

    dependencies = [
        ('basicknowledge', '0008_auto_20211020_1202'),
    ]

    operations = [
        migrations.RunPython(create_types, delete_types)
    ]
