From 23137db9550322027c9eda50b51b3211c5c9ed2f Mon Sep 17 00:00:00 2001 From: Javier Paniagua Date: Fri, 8 Sep 2023 21:42:16 +0200 Subject: [PATCH] make phonenumbers and django-phonenumber-field optional --- setup.py | 11 +++++--- .../migrations/0003_auto_20150817_1733.py | 27 ++++++++++++------- .../migrations/0004_auto_20160205_1827.py | 22 ++++++++------- two_factor/plugins/phonenumber/utils.py | 5 +++- 4 files changed, 42 insertions(+), 23 deletions(-) diff --git a/setup.py b/setup.py index 0c6b66581..639f6906d 100644 --- a/setup.py +++ b/setup.py @@ -15,7 +15,6 @@ 'Django>=3.2', 'django_otp>=0.8.0', 'qrcode>=4.0.0,<7.99', - 'django-phonenumber-field<8', 'django-formtools', ], extras_require={ @@ -23,8 +22,14 @@ 'sms': ['twilio>=6.0'], 'webauthn': ['webauthn>=2.0,<2.99'], 'yubikey': ['django-otp-yubikey'], - 'phonenumbers': ['phonenumbers>=7.0.9,<8.99'], - 'phonenumberslite': ['phonenumberslite>=7.0.9,<8.99'], + 'phonenumbers': [ + 'phonenumbers>=7.0.9,<8.99', + 'django-phonenumber-field<8', + ], + 'phonenumberslite': [ + 'phonenumberslite>=7.0.9,<8.99', + 'django-phonenumber-field<8', + ], # used internally for local development & CI 'tests': [ 'coverage', diff --git a/two_factor/migrations/0003_auto_20150817_1733.py b/two_factor/migrations/0003_auto_20150817_1733.py index da25c0c3d..9bb9b6dac 100644 --- a/two_factor/migrations/0003_auto_20150817_1733.py +++ b/two_factor/migrations/0003_auto_20150817_1733.py @@ -1,13 +1,14 @@ import logging -import phonenumbers +from django.apps import apps from django.db import migrations -from phonenumber_field.modelfields import PhoneNumberField logger = logging.getLogger(__name__) def migrate_phone_numbers(apps, schema_editor): + import phonenumbers + PhoneDevice = apps.get_model("two_factor", "PhoneDevice") for device in PhoneDevice.objects.all(): try: @@ -29,11 +30,17 @@ class Migration(migrations.Migration): ('two_factor', '0002_auto_20150110_0810'), ] - operations = [ - migrations.RunPython(migrate_phone_numbers, reverse_code=migrations.RunPython.noop), - migrations.AlterField( - model_name='phonedevice', - name='number', - field=PhoneNumberField(max_length=16, verbose_name='number'), - ), - ] + if apps.is_installed('two_factor.plugins.phonenumber'): + from phonenumber_field.modelfields import PhoneNumberField + + operations = [ + migrations.RunPython(migrate_phone_numbers, reverse_code=migrations.RunPython.noop), + migrations.AlterField( + model_name='phonedevice', + name='number', + field=PhoneNumberField(max_length=16, verbose_name='number'), + ), + ] + + else: + operations = [] diff --git a/two_factor/migrations/0004_auto_20160205_1827.py b/two_factor/migrations/0004_auto_20160205_1827.py index 80c439e43..2d603429f 100644 --- a/two_factor/migrations/0004_auto_20160205_1827.py +++ b/two_factor/migrations/0004_auto_20160205_1827.py @@ -1,6 +1,5 @@ # Generated by Django 1.9.2 on 2016-02-05 17:27 - -import phonenumber_field.modelfields +from django.apps import apps from django.db import migrations @@ -10,10 +9,15 @@ class Migration(migrations.Migration): ('two_factor', '0003_auto_20150817_1733'), ] - operations = [ - migrations.AlterField( - model_name='phonedevice', - name='number', - field=phonenumber_field.modelfields.PhoneNumberField(max_length=128), - ), - ] + if apps.is_installed('two_factor.plugins.phonenumber'): + import phonenumber_field.modelfields + + operations = [ + migrations.AlterField( + model_name='phonedevice', + name='number', + field=phonenumber_field.modelfields.PhoneNumberField(max_length=128), + ), + ] + else: + operations = [] diff --git a/two_factor/plugins/phonenumber/utils.py b/two_factor/plugins/phonenumber/utils.py index 9a8ea164e..348375723 100644 --- a/two_factor/plugins/phonenumber/utils.py +++ b/two_factor/plugins/phonenumber/utils.py @@ -1,6 +1,5 @@ import re -import phonenumbers from django.conf import settings from django.utils.translation import gettext_lazy as _ @@ -39,6 +38,8 @@ def mask_phone_number(number): :param number: str or phonenumber object :return: str """ + import phonenumbers + if isinstance(number, phonenumbers.PhoneNumber): number = format_phone_number(number) return phone_mask.sub('*', number) @@ -50,6 +51,8 @@ def format_phone_number(number): :param number: str or phonenumber object :return: str """ + import phonenumbers + if not isinstance(number, phonenumbers.PhoneNumber): number = phonenumbers.parse(number) return phonenumbers.format_number(number, phonenumbers.PhoneNumberFormat.INTERNATIONAL)