This commit is contained in:
Iliyan Angelov
2025-09-14 23:24:25 +03:00
commit c67067a2a4
71311 changed files with 6800714 additions and 0 deletions

View File

@@ -0,0 +1,35 @@
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class SoundCloudAccount(ProviderAccount):
def get_profile_url(self):
return self.account.extra_data.get("permalink_url")
def get_avatar_url(self):
return self.account.extra_data.get("avatar_url")
def to_str(self):
dflt = super(SoundCloudAccount, self).to_str()
full_name = self.account.extra_data.get("full_name")
username = self.account.extra_data.get("username")
return full_name or username or dflt
class SoundCloudProvider(OAuth2Provider):
id = "soundcloud"
name = "SoundCloud"
account_class = SoundCloudAccount
def extract_uid(self, data):
return str(data["id"])
def extract_common_fields(self, data):
return dict(
name=data.get("full_name"),
username=data.get("username"),
email=data.get("email"),
)
provider_classes = [SoundCloudProvider]

View File

@@ -0,0 +1,42 @@
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase
from .provider import SoundCloudProvider
class SoundCloudTests(OAuth2TestsMixin, TestCase):
provider_id = SoundCloudProvider.id
def get_mocked_response(self):
return MockedResponse(
200,
"""
{
"website": null,
"myspace_name": null,
"public_favorites_count": 0,
"followings_count": 1,
"full_name": "",
"id": 22341947,
"city": null,
"track_count": 0,
"playlist_count": 0,
"discogs_name": null,
"private_tracks_count": 0,
"followers_count": 0,
"online": true,
"username": "user187631676",
"description": null,
"kind": "user",
"website_title": null,
"primary_email_confirmed": false,
"permalink_url": "http://soundcloud.com/user187631676",
"private_playlists_count": 0,
"permalink": "user187631676",
"country": null,
"uri": "https://api.soundcloud.com/users/22341947",
"avatar_url":
"https://a1.sndcdn.com/images/default_avatar_large.png?4b4189b",
"plan": "Free"
}""",
)

View File

@@ -0,0 +1,6 @@
from allauth.socialaccount.providers.oauth2.urls import default_urlpatterns
from .provider import SoundCloudProvider
urlpatterns = default_urlpatterns(SoundCloudProvider)

View File

@@ -0,0 +1,25 @@
import requests
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import SoundCloudProvider
class SoundCloudOAuth2Adapter(OAuth2Adapter):
provider_id = SoundCloudProvider.id
access_token_url = "https://api.soundcloud.com/oauth2/token"
authorize_url = "https://soundcloud.com/connect"
profile_url = "https://api.soundcloud.com/me.json"
def complete_login(self, request, app, token, **kwargs):
resp = requests.get(self.profile_url, params={"oauth_token": token.token})
extra_data = resp.json()
return self.get_provider().sociallogin_from_response(request, extra_data)
oauth2_login = OAuth2LoginView.adapter_view(SoundCloudOAuth2Adapter)
oauth2_callback = OAuth2CallbackView.adapter_view(SoundCloudOAuth2Adapter)