# Generated by Django 3.2.7 on 2021-09-21 15:50

import django.core.validators
import django_inet.models
from django.db import migrations, models
from django_peeringdb import const

from peeringdb_server.util import disable_auto_now_and_save


class Migration(migrations.Migration):
    dependencies = [
        ("peeringdb_server", "0075_add_region_continent_field"),
    ]

    def update_region_continent_ok(apps, schema_editor):
        print("Updating fac region_continent")
        Facility = apps.get_model("peeringdb_server", "Facility")

        objects = Facility.handleref.all()

        for ob in objects:
            if ob.status != "ok":
                continue

            country = ob.country.code
            continent = None

            for ctr in const.REGION_MAPPING:
                if ctr["code"] == country:
                    continent = ctr["continent"]
                    break

            ob.region_continent = continent
            disable_auto_now_and_save(ob)

    operations = [
        migrations.RunPython(update_region_continent_ok),
    ]
