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

Avoid loading Django user models before apps are ready #3386

Merged
merged 1 commit into from
Aug 24, 2023

Conversation

francoisfreitag
Copy link
Contributor

@francoisfreitag francoisfreitag commented Aug 24, 2023

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.

Submitting Pull Requests

General

  • Make sure you use semantic commit messages.
    Examples: "fix(google): Fixed foobar bug", "feat(accounts): Added foobar feature".
  • All Python code must formatted using Black, and clean from pep8 and isort issues.
  • JavaScript code should adhere to StandardJS.
  • If your changes are significant, please update ChangeLog.rst.
  • If your change is substantial, feel free to add yourself to AUTHORS.

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.
```
francoisfreitag added a commit to gip-inclusion/les-emplois that referenced this pull request Aug 24, 2023
Waiting on pennersr/django-allauth#3386

Remove hack to register adapters, as well as migrations that diverged
from upstream.

The registration of providers must happen before the allauth models are
loaded, as `SocialApp.provider.choices` calls on the providers registry.
Otherwise, a migration is detected as our choices include PEAMU once the
apps are ready.

To make sure allauth_adapters registers before the allauth apps, the
separation of `INSTALLED_APPS` into DJANGO, THIRD_PARTY and ITOU was
broken.
@coveralls
Copy link

coveralls commented Aug 24, 2023

Coverage Status

coverage: 91.324% (+0.007%) from 91.317% when pulling cb05d5d on francoisfreitag:provider into ef12bd6 on pennersr:main.

@pennersr pennersr merged commit 6a9b248 into pennersr:main Aug 24, 2023
24 checks passed
@francoisfreitag francoisfreitag deleted the provider branch September 4, 2023 08:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants