Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

make phonenumber plugin really a plugin #654

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,21 @@
'Django>=3.2',
'django_otp>=0.8.0',
'qrcode>=4.0.0,<7.99',
'django-phonenumber-field<8',
'django-formtools',
],
extras_require={
'call': ['twilio>=6.0'],
'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',
Expand Down
27 changes: 17 additions & 10 deletions two_factor/migrations/0003_auto_20150817_1733.py
Original file line number Diff line number Diff line change
@@ -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:
Expand All @@ -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 = []
22 changes: 13 additions & 9 deletions two_factor/migrations/0004_auto_20160205_1827.py
Original file line number Diff line number Diff line change
@@ -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


Expand All @@ -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 = []
10 changes: 8 additions & 2 deletions two_factor/views/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import django_otp
import qrcode
import qrcode.image.svg
from django.apps import apps
from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME, login
from django.contrib.auth.decorators import login_required
Expand All @@ -35,7 +36,6 @@
from django_otp.util import random_hex

from two_factor import signals
from two_factor.plugins.phonenumber.utils import get_available_phone_methods
from two_factor.plugins.registry import registry
from two_factor.utils import totp_digits
from two_factor.views.mixins import OTPRequiredMixin
Expand Down Expand Up @@ -685,8 +685,14 @@ def get(self, request, *args, **kwargs):
return super().get(request, *args, **kwargs)

def get_context_data(self):
phone_methods = None
if (apps.is_installed('two_factor.plugins.phonenumber')):
from two_factor.plugins.phonenumber.utils import get_available_phone_methods

phone_methods = get_available_phone_methods()

return {
'phone_methods': get_available_phone_methods(),
'phone_methods': phone_methods,
}


Expand Down
17 changes: 11 additions & 6 deletions two_factor/views/profile.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from django.apps import apps
from django.conf import settings
from django.contrib.auth.decorators import login_required
from django.shortcuts import redirect, resolve_url
Expand All @@ -7,10 +8,6 @@
from django_otp import devices_for_user
from django_otp.decorators import otp_required

from two_factor.plugins.phonenumber.utils import (
backup_phones, get_available_phone_methods,
)

from ..forms import DisableForm
from ..utils import default_device
from .utils import class_view_decorator
Expand Down Expand Up @@ -41,10 +38,18 @@ def get_context_data(self, **kwargs):
'default_device': default_device(user),
'default_device_type': default_device(user).__class__.__name__,
'backup_tokens': backup_tokens,
'backup_phones': backup_phones(user),
'available_phone_methods': get_available_phone_methods(),
}

if apps.is_installed('two_factor.plugins.phonenumber'):
from two_factor.plugins.phonenumber.utils import (
backup_phones, get_available_phone_methods,
)

context.update({
'backup_phones': backup_phones(self.request.user),
'available_phone_methods': get_available_phone_methods(),
})

return context


Expand Down
Loading