| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
from allauth.socialaccount.providers.base import ProviderAccount |
| 4 |
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider |
| 5 |
from allauth.socialaccount.providers.strava.views import StravaOAuth2Adapter |
| 6 |
|
| 7 |
|
| 8 |
class StravaAccount(ProviderAccount): |
| 9 |
def get_profile_url(self): |
| 10 |
id = self.account.extra_data.get("id") |
| 11 |
if id: |
| 12 |
return f"https://www.strava.com/athletes/{id}" |
| 13 |
return None |
| 14 |
|
| 15 |
def get_avatar_url(self): |
| 16 |
avatar = self.account.extra_data.get("profile") |
| 17 |
if avatar and avatar != "avatar/athlete/large.png": |
| 18 |
return avatar |
| 19 |
return None |
| 20 |
|
| 21 |
|
| 22 |
class StravaProvider(OAuth2Provider): |
| 23 |
id = "strava" |
| 24 |
name = "Strava" |
| 25 |
account_class = StravaAccount |
| 26 |
oauth2_adapter_class = StravaOAuth2Adapter |
| 27 |
|
| 28 |
def extract_uid(self, data): |
| 29 |
return str(data["id"]) |
| 30 |
|
| 31 |
def extract_common_fields(self, data): |
| 32 |
extra_common = super().extract_common_fields(data) |
| 33 |
firstname = data.get("firstname") |
| 34 |
lastname = data.get("lastname") |
| 35 |
name = " ".join(part for part in (firstname, lastname) if part) |
| 36 |
extra_common.update( |
| 37 |
username=data.get("username"), |
| 38 |
email=data.get("email"), |
| 39 |
first_name=firstname, |
| 40 |
last_name=lastname, |
| 41 |
name=name.strip(), |
| 42 |
) |
| 43 |
return extra_common |
| 44 |
|
| 45 |
def get_default_scope(self): |
| 46 |
return ["read,activity:read"] |
| 47 |
|
| 48 |
|
| 49 |
provider_classes = [StravaProvider] |