# Generated by Django 2.2.4 on 2021-08-11 12:25

from django.db import migrations
import phonenumbers
from django.db import transaction


def update_phone_numbers(apps, schema_editor):
    Flat = apps.get_model('property', 'Flat')

    for flat in Flat.objects.all():
        parsed_num = phonenumbers.parse(flat.owners_phonenumber, 'RU')
        if phonenumbers.is_valid_number(parsed_num):
            flat.owner_pure_phone = parsed_num
            flat.save()

def update_phone_numbers_reverse(apps, schema_editor):
    Flat = apps.get_model('property', 'Flat')
    for flat in Flat.objects.all():
        flat.owner_pure_phone = None
        flat.save()


class Migration(migrations.Migration):

    dependencies = [
        ('property', '0009_auto_20210811_1611'),
    ]

    operations = [
        migrations.RunPython(update_phone_numbers, reverse_code=update_phone_numbers_reverse),
    ]
