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,33 @@
from allauth.socialaccount.providers.base import ProviderAccount
from allauth.socialaccount.providers.oauth2.provider import OAuth2Provider
class FoursquareAccount(ProviderAccount):
def get_profile_url(self):
return "https://foursquare.com/user/" + self.account.extra_data.get("id")
def get_avatar_url(self):
return self.account.extra_data.get("photo")
def to_str(self):
dflt = super(FoursquareAccount, self).to_str()
return self.account.extra_data.get("name", dflt)
class FoursquareProvider(OAuth2Provider):
id = "foursquare"
name = "Foursquare"
account_class = FoursquareAccount
def extract_uid(self, data):
return str(data["id"])
def extract_common_fields(self, data):
return dict(
first_name=data.get("firstname"),
last_name=data.get("lastname"),
email=data.get("contact").get("email"),
)
provider_classes = [FoursquareProvider]

View File

@@ -0,0 +1,68 @@
from allauth.socialaccount.tests import OAuth2TestsMixin
from allauth.tests import MockedResponse, TestCase
from .provider import FoursquareProvider
class FoursquareTests(OAuth2TestsMixin, TestCase):
provider_id = FoursquareProvider.id
def get_mocked_response(self):
return MockedResponse(
200,
"""
{"notifications": [{"item": {"unreadCount": 0}, "type": "notificationTray"}],
"meta": {"code": 200},
"response": {
"user": {
"photo": {
"prefix": "https://irs0.4sqi.net/img/user/", "suffix": "/blank_boy.png"},
"pings": false,
"homeCity": "Athens, ESYE31",
"id": "76077726",
"badges": {"count": 0, "items": []},
"referralId": "u-76077726",
"friends":
{
"count": 0,
"groups": [{"count": 0, "items": [], "type": "friends",
"name": "Mutual friends"}, {"count": 0, "items": [], "type": "others",
"name": "Other friends"}]
},
"createdAt": 1389624445,
"tips": {"count": 0},
"type": "user",
"bio": "",
"relationship": "self",
"lists":
{
"count": 1,
"groups": [{"count": 1, "items": [{"description": "",
"collaborative": false, "url": "/user/76077726/list/todos", "editable": false,
"listItems": {"count": 0}, "id": "76077726/todos", "followers": {"count": 0},
"user": {"gender": "male",
"firstName": "\u03a1\u03c9\u03bc\u03b1\u03bd\u03cc\u03c2",
"relationship": "self", "photo": {"prefix": "https://irs0.4sqi.net/img/user/",
"suffix": "/blank_boy.png"},
"lastName": "\u03a4\u03c3\u03bf\u03c5\u03c1\u03bf\u03c0\u03bb\u03ae\u03c2",
"id": "76077726"}, "public": false,
"canonicalUrl": "https://foursquare.com/user/76077726/list/todos",
"name": "My to-do list"}], "type": "created"}, {"count": 0, "items": [],
"type": "followed"}]
},
"photos": {"count": 0, "items": []},
"checkinPings": "off",
"scores": {"max": 0, "checkinsCount": 0, "goal": 50, "recent": 0},
"checkins": {"count": 0, "items": []},
"firstName": "\u03a1\u03c9\u03bc\u03b1\u03bd\u03cc\u03c2",
"gender": "male",
"contact": {"email": "romdimtsouroplis@example.com"},
"lastName": "\u03a4\u03c3\u03bf\u03c5\u03c1\u03bf\u03c0\u03bb\u03ae\u03c2",
"following": {"count": 0, "groups": [{"count": 0, "items": [],
"type": "following", "name": "Mutual following"}, {"count": 0, "items": [],
"type": "others", "name": "Other following"}]},
"requests": {"count": 0}, "mayorships": {"count": 0, "items": []}}
}
}
""",
)

View File

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

View File

@@ -0,0 +1,33 @@
import requests
from allauth.socialaccount.providers.oauth2.views import (
OAuth2Adapter,
OAuth2CallbackView,
OAuth2LoginView,
)
from .provider import FoursquareProvider
class FoursquareOAuth2Adapter(OAuth2Adapter):
provider_id = FoursquareProvider.id
access_token_url = "https://foursquare.com/oauth2/access_token"
# Issue ?? -- this one authenticates over and over again...
# authorize_url = 'https://foursquare.com/oauth2/authorize'
authorize_url = "https://foursquare.com/oauth2/authenticate"
profile_url = "https://api.foursquare.com/v2/users/self"
def complete_login(self, request, app, token, **kwargs):
# Foursquare needs a version number for their API requests as
# documented here
# https://developer.foursquare.com/overview/versioning
resp = requests.get(
self.profile_url,
params={"oauth_token": token.token, "v": "20140116"},
)
extra_data = resp.json()["response"]["user"]
return self.get_provider().sociallogin_from_response(request, extra_data)
oauth2_login = OAuth2LoginView.adapter_view(FoursquareOAuth2Adapter)
oauth2_callback = OAuth2CallbackView.adapter_view(FoursquareOAuth2Adapter)