Skip to content

Commit

Permalink
fix(providers): Avoid loading Django user models before apps are ready
Browse files Browse the repository at this point in the history
To register a custom Provider, users subclass
`allauth.socialaccount.providers.base.ProviderAccount`. Their subclass
is then registered using
`allauth.socialaccount.providers.registry.register(MyProvider)`.

The registration must happen before the `allauth.socialaccount` app
models are ready, as the `SocialApp.provider` choices come from the
registry. Otherwise, a Django migration is created because at the time
of the import of `allauth.socialaccount.models`, the registry was empty,
but when the models are ready (and Django can check migrations) the
registry contains the custom provider.

Below is an example stack trace to illustrate the issue:
```
  File "/home/freitafr/dev/itou/itou/allauth_adapters/peamu/provider.py", line 1, in <module>
    from allauth.socialaccount.providers.base import ProviderAccount
  File "/home/freitafr/dev/django-allauth/allauth/socialaccount/providers/base/__init__.py", line 2, in <module>
    from .provider import Provider, ProviderAccount, ProviderException  # noqa
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/freitafr/dev/django-allauth/allauth/socialaccount/providers/base/provider.py", line 3, in <module>
    from allauth.account.models import EmailAddress
  File "/home/freitafr/dev/django-allauth/allauth/account/models.py", line 12, in <module>
    from .adapter import get_adapter
  File "/home/freitafr/dev/django-allauth/allauth/account/adapter.py", line 16, in <module>
    from django.contrib.auth.models import AbstractUser
  File "/home/freitafr/dev/itou/.venv/lib/python3.11/site-packages/django/contrib/auth/models.py", line 3, in <module>
    from django.contrib.auth.base_user import AbstractBaseUser, BaseUserManager
  File "/home/freitafr/dev/itou/.venv/lib/python3.11/site-packages/django/contrib/auth/base_user.py", line 57, in <module>
    class AbstractBaseUser(models.Model):
  File "/home/freitafr/dev/itou/.venv/lib/python3.11/site-packages/django/db/models/base.py", line 129, in __new__
    app_config = apps.get_containing_app_config(module)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/freitafr/dev/itou/.venv/lib/python3.11/site-packages/django/apps/registry.py", line 260, in get_containing_app_config
    self.check_apps_ready()
  File "/home/freitafr/dev/itou/.venv/lib/python3.11/site-packages/django/apps/registry.py", line 138, in check_apps_ready
    raise AppRegistryNotReady("Apps aren't loaded yet.")
django.core.exceptions.AppRegistryNotReady: Apps aren't loaded yet.
```
  • Loading branch information
francoisfreitag committed Aug 24, 2023
1 parent ef12bd6 commit cb05d5d
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions allauth/socialaccount/providers/base/provider.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from django.core.exceptions import ImproperlyConfigured

from allauth.account.models import EmailAddress
from allauth.socialaccount import app_settings
from allauth.socialaccount.adapter import get_adapter


class ProviderException(Exception):
Expand Down Expand Up @@ -59,6 +57,7 @@ def sociallogin_from_response(self, request, response):
:return: A populated instance of the `SocialLogin` model (unsaved).
"""
# NOTE: Avoid loading models at top due to registry boot...
from allauth.socialaccount.adapter import get_adapter
from allauth.socialaccount.models import SocialAccount, SocialLogin

adapter = get_adapter(request)
Expand Down Expand Up @@ -123,6 +122,9 @@ def extract_common_fields(self, data):
return {}

def cleanup_email_addresses(self, email, addresses, email_verified=False):
# Avoid loading models before adapters have been registered.
from allauth.account.models import EmailAddress

# Move user.email over to EmailAddress
if email and email.lower() not in [a.email.lower() for a in addresses]:
addresses.append(
Expand Down

0 comments on commit cb05d5d

Please sign in to comment.