1
0
mirror of https://github.com/peeringdb/peeringdb.git synced 2024-05-11 05:55:09 +00:00
Files
peeringdb-peeringdb/peeringdb_server/migrations/0025_E164_phonenumbers.py
Matt Griswold 77282c4b66 Prep 2.29 (#1024)
* django3, py39, lgtm, linting (#715)

* IX-F Importer: ticket status change when posting re-occuring conflict to existing resolved ticket (#920)

* fix recaptcha requirement for user creation in django-admin (#715)

* IX-F Importer: fix command output buffering #967

* Drop dot1q_support field #903

* fix test (#967)

* Add name, city, country to ixfac (GET operation) #166

* additional tests fir #166

* Allow IXP to trigger ix-f importer for their exchange #779

* add docker compose for dev

* add selinux labels for mountpoints

* fixes #1013: The process to permanently remove old soft-deleted network contacts pdb_delete_pocs raises a false ProtectedAction

* fix api test

* relock poetry

* remove django_namespace_perms from installed apps

* fix user permissios ui

* remove remaining references to django namespace perms

* linting

* copy tox.ini

* comment flake8 check until we figure out why it ignores configs from tox.ini

* black format

* poetry lock

Co-authored-by: Stefan Pratter <stefan@20c.com>
2021-08-18 08:21:22 -05:00

143 lines
4.0 KiB
Python

# Generated by Django 1.11.23 on 2019-12-12 08:46
import csv
import phonenumbers
from django.conf import settings
from django.db import migrations
def _edit_url(tag, instance):
if tag == "poc":
return f"{settings.BASE_URL}/net/{instance.network_id}/"
else:
return f"{settings.BASE_URL}/ix/{instance.id}/"
def _fix_number(tag, instance, field_name, list_fixed, list_invalid):
number = getattr(instance, field_name, None).strip()
if number:
try:
country = getattr(instance, "country", None)
if country:
country = country.code
parsed_number = phonenumbers.parse(number, country)
validated_number = phonenumbers.format_number(
parsed_number, phonenumbers.PhoneNumberFormat.E164
)
if f"{validated_number}" == f"{number}":
return
setattr(instance, field_name, validated_number)
list_fixed.append(
[
tag,
instance.id,
_edit_url(tag, instance),
instance.status,
field_name,
number,
validated_number,
country,
]
)
print("FIXED", tag, instance.id, field_name, number, validated_number)
instance.save()
except Exception as exc:
_push_invalid(tag, instance, field_name, number, list_invalid, f"{exc}")
print("INVALID", tag, instance.id, field_name, number)
def _push_invalid(tag, instance, field_name, number, list_invalid, reason):
country = getattr(instance, "country", None)
if country:
country = country.code
list_invalid.append(
[
tag,
instance.id,
_edit_url(tag, instance),
instance.status,
field_name,
number,
country,
reason.strip(),
]
)
def forwards_func(apps, schema_editor):
"""
Attempt to validate existing phone numbers to E164 format
Output any that can't be validated to a invalid_phonenumbers.csv file
Output any that were fixed to a fixed_phonenumbers.csv file
"""
InternetExchange = apps.get_model("peeringdb_server", "InternetExchange")
NetworkContact = apps.get_model("peeringdb_server", "NetworkContact")
invalid = []
fixed = []
for ix in InternetExchange.handleref.filter(status__in=["ok", "pending"]):
_fix_number("ix", ix, "tech_phone", fixed, invalid)
_fix_number("ix", ix, "policy_phone", fixed, invalid)
for poc in NetworkContact.handleref.filter(status__in=["ok", "pending"]):
_fix_number("poc", poc, "phone", fixed, invalid)
""" This was used in production as a one time process
headers_invalid = [
"type",
"id",
"status",
"field",
"phonenumber",
"country",
"reason",
]
headers_fixed = [
"type",
"id",
"status",
"field",
"phonenumber",
"fixed",
"country",
]
print(
"Invalid numbers: {} - written to invalid_phonenumbers.csv".format(len(invalid))
)
with open("invalid_phonenumbers.csv", "w+") as csvfile:
csvwriter = csv.writer(csvfile, lineterminator="\n")
csvwriter.writerow(headers_invalid)
for row in invalid:
csvwriter.writerow(row)
print("Fixed numbers: {} - written to fixed_phonenumbers.csv".format(len(fixed)))
with open("fixed_phonenumbers.csv", "w+") as csvfile:
csvwriter = csv.writer(csvfile, lineterminator="\n")
csvwriter.writerow(headers_fixed)
for row in fixed:
csvwriter.writerow(row)
"""
class Migration(migrations.Migration):
dependencies = [
("peeringdb_server", "0024_netixlan_asn"),
]
operations = [
migrations.RunPython(forwards_func, migrations.RunPython.noop),
]