Skip to content

Commit

Permalink
make phonenumbers and django-phonenumber-field optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jpaniagualaconich committed Sep 10, 2023
1 parent d6d8b63 commit 320e6d5
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 23 deletions.
5 changes: 2 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,15 @@
'Django>=3.2',
'django_otp>=0.8.0',
'qrcode>=4.0.0,<7.99',
'django-phonenumber-field>=1.1.0,<7',
'django-formtools',
],
extras_require={
'call': ['twilio>=6.0'],
'sms': ['twilio>=6.0'],
'webauthn': ['webauthn>=1.6.0,<1.99', 'pydantic>=1.9.0,<1.99'],
'yubikey': ['django-otp-yubikey'],
'phonenumbers': ['phonenumbers>=7.0.9,<8.99'],
'phonenumberslite': ['phonenumberslite>=7.0.9,<8.99'],
'phonenumbers': ['django-phonenumber-field>=1.1.0,<7', 'phonenumbers>=7.0.9,<8.99'],
'phonenumberslite': ['django-phonenumber-field>=1.1.0,<7', 'phonenumberslite>=7.0.9,<8.99'],
},
include_package_data=True,
classifiers=[
Expand Down
28 changes: 18 additions & 10 deletions two_factor/migrations/0003_auto_20150817_1733.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import logging

import phonenumbers
from django.conf import settings
from django.db import migrations
from phonenumber_field.modelfields import PhoneNumberField

if 'two_factor.plugins.phonenumber' in settings.INSTALLED_APPS:
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:
Expand All @@ -29,11 +33,15 @@ 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 'two_factor.plugins.phonenumber' in settings.INSTALLED_APPS:
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 = []
23 changes: 14 additions & 9 deletions two_factor/migrations/0004_auto_20160205_1827.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
# Generated by Django 1.9.2 on 2016-02-05 17:27

import phonenumber_field.modelfields
from django.conf import settings
from django.db import migrations

if 'two_factor.plugins.phonenumber' in settings.INSTALLED_APPS:
import phonenumber_field.modelfields


class Migration(migrations.Migration):

dependencies = [
('two_factor', '0003_auto_20150817_1733'),
]

operations = [
migrations.AlterField(
model_name='phonedevice',
name='number',
field=phonenumber_field.modelfields.PhoneNumberField(max_length=128),
),
]
if 'two_factor.plugins.phonenumber' in settings.INSTALLED_APPS:
operations = [
migrations.AlterField(
model_name='phonedevice',
name='number',
field=phonenumber_field.modelfields.PhoneNumberField(max_length=128),
),
]
else:
operations = []
5 changes: 4 additions & 1 deletion two_factor/plugins/phonenumber/utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import re

import phonenumbers
from django.conf import settings
from django.utils.translation import gettext_lazy as _

Expand Down Expand Up @@ -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)
Expand All @@ -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)

0 comments on commit 320e6d5

Please sign in to comment.