| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 4 |
from allauth.socialaccount.providers.foursquare.views import FoursquareOAuth2Adapter |
| 5 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 6 |
|
| 7 |
|
| 8 |
class FoursquareAccount(ProviderAccount): |
| 9 |
def get_profile_url(self): |
| 10 |
return f"https://foursquare.com/user/{self.account.extra_data.get('id')}" |
| 11 |
|
| 12 |
def get_avatar_url(self): |
| 13 |
return self.account.extra_data.get("photo") |
| 14 |
|
| 15 |
def to_str(self): |
| 16 |
dflt = super().to_str() |
| 17 |
return self.account.extra_data.get("contact", {}).get("email", dflt) |
| 18 |
|
| 19 |
|
| 20 |
class FoursquareProvider(OAuth2Provider): |
| 21 |
id = "foursquare" |
| 22 |
name = "Foursquare" |
| 23 |
account_class = FoursquareAccount |
| 24 |
oauth2_adapter_class = FoursquareOAuth2Adapter |
| 25 |
|
| 26 |
def extract_uid(self, data): |
| 27 |
return str(data["id"]) |
| 28 |
|
| 29 |
def extract_common_fields(self, data): |
| 30 |
return dict( |
| 31 |
first_name=data.get("firstname"), |
| 32 |
last_name=data.get("lastname"), |
| 33 |
email=data.get("contact").get("email"), |
| 34 |
) |
| 35 |
|
| 36 |
|
| 37 |
provider_classes = [FoursquareProvider] |