| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from django.http import HttpRequest |
| 4 |
from django.urls import reverse |
| 5 |
|
| 6 |
from allauth.account.models import EmailAddress |
| 7 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 8 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 9 |
from allauth.socialaccount.providers.untappd.views import UntappdOAuth2Adapter |
| 10 |
|
| 11 |
|
| 12 |
class UntappdAccount(ProviderAccount): |
| 13 |
def get_profile_url(self): |
| 14 |
return self.account.extra_data.get("untappd_url") |
| 15 |
|
| 16 |
def get_avatar_url(self): |
| 17 |
return self.account.extra_data.get("user_avatar") |
| 18 |
|
| 19 |
def get_user_data(self): |
| 20 |
return self.account.extra_data.get("response", {}).get("user", {}) |
| 21 |
|
| 22 |
|
| 23 |
class UntappdProvider(OAuth2Provider): |
| 24 |
id = "untappd" |
| 25 |
name = "Untappd" |
| 26 |
account_class = UntappdAccount |
| 27 |
oauth2_adapter_class = UntappdOAuth2Adapter |
| 28 |
|
| 29 |
def get_auth_params_from_request(self, request: HttpRequest, action): |
| 30 |
params = super().get_auth_params_from_request(request, action) |
| 31 |
# Untappd uses redirect_url instead of redirect_uri |
| 32 |
params["redirect_url"] = request.build_absolute_uri( |
| 33 |
reverse(f"{self.id}_callback") |
| 34 |
) |
| 35 |
return params |
| 36 |
|
| 37 |
def extract_uid(self, data): |
| 38 |
return str(data["response"]["user"]["uid"]) |
| 39 |
|
| 40 |
def extract_common_fields(self, data): |
| 41 |
user = data["response"]["user"] |
| 42 |
return dict( |
| 43 |
username=user["user_name"], |
| 44 |
name=f"{user['first_name']} {user['last_name']}", |
| 45 |
) |
| 46 |
|
| 47 |
def extract_email_addresses(self, data): |
| 48 |
ret = [ |
| 49 |
EmailAddress( |
| 50 |
email=data["response"]["user"]["settings"]["email_address"], |
| 51 |
verified=True, |
| 52 |
primary=True, |
| 53 |
) |
| 54 |
] |
| 55 |
return ret |
| 56 |
|
| 57 |
|
| 58 |
provider_classes = [UntappdProvider] |