aboutsummaryrefslogtreecommitdiffstats
path: root/pyblackbird_cc/users/adapters.py
diff options
context:
space:
mode:
authorMatthew Lemon <y@yulqen.org>2024-05-13 17:26:25 +0100
committerMatthew Lemon <y@yulqen.org>2024-05-13 17:26:25 +0100
commitefbbd480ddc62e695123d31c31d233b0df5155bd (patch)
treebc2fb465edd5050d83c97f280b1aac8e023fe3e5 /pyblackbird_cc/users/adapters.py
After first pre-commit processing
Diffstat (limited to 'pyblackbird_cc/users/adapters.py')
-rw-r--r--pyblackbird_cc/users/adapters.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/pyblackbird_cc/users/adapters.py b/pyblackbird_cc/users/adapters.py
new file mode 100644
index 0000000..b13f3ca
--- /dev/null
+++ b/pyblackbird_cc/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 pyblackbird_cc.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