aboutsummaryrefslogtreecommitdiffstats
path: root/alphabetlearning/users/adapters.py
diff options
context:
space:
mode:
Diffstat (limited to 'alphabetlearning/users/adapters.py')
-rw-r--r--alphabetlearning/users/adapters.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/alphabetlearning/users/adapters.py b/alphabetlearning/users/adapters.py
new file mode 100644
index 0000000..29396c4
--- /dev/null
+++ b/alphabetlearning/users/adapters.py
@@ -0,0 +1,48 @@
+from __future__ import annotations
+
+import typing
+
+from allauth.account.adapter import DefaultAccountAdapter
+from allauth.socialaccount.adapter import DefaultSocialAccountAdapter
+from django.conf import settings
+
+if typing.TYPE_CHECKING:
+ from allauth.socialaccount.models import SocialLogin
+ from django.http import HttpRequest
+
+ from alphabetlearning.users.models import User
+
+
+class AccountAdapter(DefaultAccountAdapter):
+ def is_open_for_signup(self, request: HttpRequest) -> bool:
+ return getattr(settings, "ACCOUNT_ALLOW_REGISTRATION", True)
+
+
+class SocialAccountAdapter(DefaultSocialAccountAdapter):
+ def is_open_for_signup(
+ self,
+ request: HttpRequest,
+ sociallogin: SocialLogin,
+ ) -> bool:
+ return getattr(settings, "ACCOUNT_ALLOW_REGISTRATION", True)
+
+ def populate_user(
+ self,
+ request: HttpRequest,
+ sociallogin: SocialLogin,
+ data: dict[str, typing.Any],
+ ) -> User:
+ """
+ Populates user information from social provider info.
+
+ See: https://docs.allauth.org/en/latest/socialaccount/advanced.html#creating-and-populating-user-instances
+ """
+ user = super().populate_user(request, sociallogin, data)
+ if not user.name:
+ if name := data.get("name"):
+ user.name = name
+ elif first_name := data.get("first_name"):
+ user.name = first_name
+ if last_name := data.get("last_name"):
+ user.name += f" {last_name}"
+ return user