# Generated by Django 2.2.4 on 2020-09-21 17:30

from django.db import migrations
import phonenumbers


def format_phone_numbers(apps, schema_editor):
    Flat = apps.get_model('property', 'Flat')
    for flat in Flat.objects.all():
        parsed_phone_number = phonenumbers.parse(flat.owners_phonenumber, 'RU')
        if phonenumbers.is_valid_number(parsed_phone_number):
            flat.owner_pure_phone = phonenumbers.format_number(
                parsed_phone_number,
                phonenumbers.PhoneNumberFormat.E164
            )
            flat.save()
        else:
            flat.owner_pure_phone = 'Невалидный номер, исправьте!'
            flat.save()


class Migration(migrations.Migration):

    dependencies = [
        ('property', '0007_flat_owner_pure_phone'),
    ]

    operations = [
        migrations.RunPython(format_phone_numbers)
    ]
