# -*- coding: utf-8 -*-
# Generated by Django 1.11.17 on 2020-12-29 09:51
from __future__ import unicode_literals

from bluebottle.clients import properties

from bluebottle.utils.utils import get_languages
from django.db import migrations


def migrate_to_office_regions(apps, schema_editor):
    Location = apps.get_model('geo', 'Location')
    OfficeSubRegion = apps.get_model('offices', 'OfficeSubRegion')
    OfficeRegion = apps.get_model('offices', 'OfficeRegion')
    CountryTranslation = apps.get_model('geo', 'CountryTranslation')
    CountryRegionTranslation = apps.get_model('geo', 'RegionTranslation')
    lang = properties.LANGUAGE_CODE
    for location in Location.objects.all():
        if location.country_id:
            country_name = CountryTranslation.objects.get(
                master_id=location.country.id,
                language_code=lang)
            subregion, _created = OfficeSubRegion.objects.get_or_create(name=country_name.name)
            location.subregion = subregion
            location.save()

            if location.group:
                region, _created = OfficeRegion.objects.get_or_create(name=location.group.name)
            else:
                region_name = CountryRegionTranslation.objects.get(
                    master_id=location.country.subregion.region.id,
                    language_code=lang)
                region, _created = OfficeRegion.objects.get_or_create(name=region_name.name)
            if not subregion.region:
                subregion.region = region
                subregion.save()


class Migration(migrations.Migration):

    dependencies = [
        ('geo', '0018_auto_20201229_1038'),
    ]

    operations = [
        migrations.RunPython(
            migrate_to_office_regions,
            migrations.RunPython.noop
        )
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.11.17 on 2020-12-29 09:51
from __future__ import unicode_literals

from bluebottle.clients import properties

from bluebottle.utils.utils import get_languages
from django.db import migrations


def migrate_to_office_regions(apps, schema_editor):
    Location = apps.get_model('geo', 'Location')
    OfficeSubRegion = apps.get_model('offices', 'OfficeSubRegion')
    OfficeRegion = apps.get_model('offices', 'OfficeRegion')
    CountryTranslation = apps.get_model('geo', 'CountryTranslation')
    CountryRegionTranslation = apps.get_model('geo', 'RegionTranslation')
    lang = properties.LANGUAGE_CODE
    for location in Location.objects.all():
        if location.country_id:
            country_name = CountryTranslation.objects.filter(
                master_id=location.country.id,
                language_code=lang).first()
            if not country_name:
                country_name = CountryTranslation.objects.filter(
                    master_id=location.country.id).first()
            if not country_name:
                continue
            subregion, _created = OfficeSubRegion.objects.get_or_create(name=country_name.name)
            location.subregion = subregion
            location.save()

            if location.group:
                region, _created = OfficeRegion.objects.get_or_create(name=location.group.name)
            else:
                region_name = CountryRegionTranslation.objects.get(
                    master_id=location.country.subregion.region.id,
                    language_code=lang)
                region, _created = OfficeRegion.objects.get_or_create(name=region_name.name)
            if not subregion.region:
                subregion.region = region
                subregion.save()


class Migration(migrations.Migration):

    dependencies = [
        ('geo', '0018_auto_20201229_1038'),
    ]

    operations = [
        migrations.RunPython(
            migrate_to_office_regions,
            migrations.RunPython.noop
        )
    ]

Fix migration