| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from allauth.account.models import EmailAddress |
| 4 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 5 |
from allauth.socialaccount.providers.notion.views import NotionOAuth2Adapter |
| 6 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 7 |
|
| 8 |
|
| 9 |
class NotionAccount(ProviderAccount): |
| 10 |
def get_user(self): |
| 11 |
return self.account.extra_data["owner"]["user"] |
| 12 |
|
| 13 |
def get_name(self): |
| 14 |
return self.get_user()["name"] |
| 15 |
|
| 16 |
def get_avatar_url(self): |
| 17 |
return self.get_user()["avatar_url"] |
| 18 |
|
| 19 |
def get_workspace_name(self): |
| 20 |
return self.account.extra_data["workspace_name"] |
| 21 |
|
| 22 |
def get_workspace_icon(self): |
| 23 |
return self.account.extra_data["workspace_icon"] |
| 24 |
|
| 25 |
def to_str(self): |
| 26 |
name = self.get_name() |
| 27 |
workspace = self.get_workspace_name() |
| 28 |
return f"{name} ({workspace})" |
| 29 |
|
| 30 |
|
| 31 |
class NotionProvider(OAuth2Provider): |
| 32 |
id = "notion" |
| 33 |
name = "Notion" |
| 34 |
account_class = NotionAccount |
| 35 |
oauth2_adapter_class = NotionOAuth2Adapter |
| 36 |
|
| 37 |
def extract_uid(self, data): |
| 38 |
""" |
| 39 |
The unique identifier for Notion is a combination of the User ID |
| 40 |
and the Workspace ID they have authorized the application with. |
| 41 |
""" |
| 42 |
user_id = data["owner"]["user"]["id"] |
| 43 |
workspace_id = data["workspace_id"] |
| 44 |
return f"user-{user_id}_workspace-{workspace_id}" |
| 45 |
|
| 46 |
def extract_common_fields(self, data): |
| 47 |
user = data["owner"]["user"] |
| 48 |
user["email"] = user["person"]["email"] |
| 49 |
return user |
| 50 |
|
| 51 |
def extract_email_addresses(self, data): |
| 52 |
user = data["owner"]["user"] |
| 53 |
email = user["person"]["email"] |
| 54 |
return [EmailAddress(email=email, verified=False, primary=True)] |
| 55 |
|
| 56 |
|
| 57 |
provider_classes = [NotionProvider] |