From 9439576107d97959f1ebf11cafce39045fde5a7f Mon Sep 17 00:00:00 2001 From: David D Lowe Date: Tue, 18 Jun 2024 17:26:26 +0100 Subject: [PATCH] Allow subclassing InvitationsAdapter Previously, the code would check to see if ACCOUNT_ADAPTER equalled "invitations.models.InvitationsAdapter". But this would cause it to reject subclasses of InvitationsAdapter. Instead, it is enough to check to see if ACCOUNT_ADAPTER setting exists, as this setting is meant for django-allauth. Modify the tests to check for this. --- invitations/adapters.py | 5 +---- invitations/models.py | 37 ++++++++++++++++++------------------- test_allauth_adapters.py | 10 ++++++++++ test_allauth_settings.py | 2 +- 4 files changed, 30 insertions(+), 24 deletions(-) create mode 100644 test_allauth_adapters.py diff --git a/invitations/adapters.py b/invitations/adapters.py index 81cbcc2..53fdd39 100644 --- a/invitations/adapters.py +++ b/invitations/adapters.py @@ -112,10 +112,7 @@ def add_message( def get_invitations_adapter(): # Compatibility with legacy allauth only version. - LEGACY_ALLAUTH = ( - hasattr(settings, "ACCOUNT_ADAPTER") - and settings.ACCOUNT_ADAPTER == "invitations.models.InvitationsAdapter" - ) + LEGACY_ALLAUTH = hasattr(settings, "ACCOUNT_ADAPTER") if LEGACY_ALLAUTH: # defer to allauth from allauth.account.adapter import get_adapter diff --git a/invitations/models.py b/invitations/models.py index 8a8dd88..e3b271b 100644 --- a/invitations/models.py +++ b/invitations/models.py @@ -75,22 +75,21 @@ def __str__(self): # here for backwards compatibility, historic allauth adapter if hasattr(settings, "ACCOUNT_ADAPTER"): - if settings.ACCOUNT_ADAPTER == "invitations.models.InvitationsAdapter": - from allauth.account.adapter import DefaultAccountAdapter - from allauth.account.signals import user_signed_up - - class InvitationsAdapter(DefaultAccountAdapter): - def is_open_for_signup(self, request): - if hasattr(request, "session") and request.session.get( - "account_verified_email", - ): - return True - elif app_settings.INVITATION_ONLY is True: - # Site is ONLY open for invites - return False - else: - # Site is open to signup - return True - - def get_user_signed_up_signal(self): - return user_signed_up + from allauth.account.adapter import DefaultAccountAdapter + from allauth.account.signals import user_signed_up + + class InvitationsAdapter(DefaultAccountAdapter): + def is_open_for_signup(self, request): + if hasattr(request, "session") and request.session.get( + "account_verified_email", + ): + return True + elif app_settings.INVITATION_ONLY is True: + # Site is ONLY open for invites + return False + else: + # Site is open to signup + return True + + def get_user_signed_up_signal(self): + return user_signed_up diff --git a/test_allauth_adapters.py b/test_allauth_adapters.py new file mode 100644 index 0000000..c103ffb --- /dev/null +++ b/test_allauth_adapters.py @@ -0,0 +1,10 @@ +from invitations.models import InvitationsAdapter + + +class TestInvitationsAdapter(InvitationsAdapter): + """Class used to test that subclasses to InvitationsAdapter work as + expected + + """ + + pass diff --git a/test_allauth_settings.py b/test_allauth_settings.py index a0c263d..efb6426 100644 --- a/test_allauth_settings.py +++ b/test_allauth_settings.py @@ -19,7 +19,7 @@ "allauth.account.auth_backends.AuthenticationBackend", ) -ACCOUNT_ADAPTER = "invitations.models.InvitationsAdapter" +ACCOUNT_ADAPTER = "test_allauth_adapters.TestInvitationsAdapter" MIDDLEWARE.append("allauth.account.middleware.AccountMiddleware") # noqa: F405