Commit 1163ad4c authored by Virgílio Nascimento Santos's avatar Virgílio Nascimento Santos
Browse files

social media: add facebook and twitter connect

parent da0c4f65
from django.views.generic.base import TemplateView
from rest_auth.registration.views import SocialAccountListView, \
SocialAccountDisconnectView
from rest_framework.routers import SimpleRouter
from django.conf.urls import url
......@@ -15,6 +17,16 @@ urlpatterns = [
name='api-key'
),
url(r'^close/$', TemplateView.as_view(template_name='users/close.html')),
url(
r'^socialaccounts/$',
SocialAccountListView.as_view(),
name='social_account_list'
),
url(
r'^socialaccounts/(?P<pk>\d+)/disconnect/$',
SocialAccountDisconnectView.as_view(),
name='social_account_disconnect'
)
]
urlpatterns.extend(router.urls)
from urllib.parse import parse_qs
import requests
from allauth.socialaccount.models import SocialApp
from django.urls import reverse
from requests_oauthlib import OAuth1
from rest_framework.viewsets import ModelViewSet
from rest_framework.decorators import detail_route, parser_classes
from rest_framework.decorators import detail_route, parser_classes, action
from rest_framework.response import Response
from rest_framework.parsers import FormParser, MultiPartParser
from rest_framework.authtoken.models import Token
from rest_framework import status
from rest_framework import status, generics, reverse
from rest_framework import permissions
from django.http import Http404, JsonResponse
from django.utils.translation import ugettext_lazy as _
from .models import User
from .permissions import IsCurrentUserOrAdmin
from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter
from allauth.socialaccount.providers.twitter.views import TwitterOAuthAdapter
from rest_auth.views import LoginView
from rest_auth.registration.views import SocialLoginView
from rest_auth.social_serializers import TwitterLoginSerializer
from rest_auth.registration.views import SocialLoginView, SocialConnectView
from rest_auth.social_serializers import TwitterLoginSerializer, \
TwitterConnectSerializer
from .serializers import UserSerializer, FixSocialLoginSerializer
......@@ -66,6 +74,61 @@ class TwitterLogin(LoginView):
adapter_class = TwitterOAuthAdapter
class FacebookConnect(SocialConnectView):
adapter_class = FacebookOAuth2Adapter
class TwitterConnect(SocialConnectView):
serializer_class = TwitterConnectSerializer
adapter_class = TwitterOAuthAdapter
class TwitterRequestToken(generics.GenericAPIView):
def post(self, request):
if not SocialApp.objects.filter(provider='twitter').exists():
return Response({'message': _('No Twitter provider found')},
status=status.HTTP_404_NOT_FOUND)
twitter_app = SocialApp.objects.get(provider='twitter')
CONSUMER_KEY = twitter_app.client_id
CONSUMER_SECRET = twitter_app.secret
callback = request\
.build_absolute_uri(reverse('twitter_connect_callback'))
auth = OAuth1(CONSUMER_KEY, CONSUMER_SECRET, callback_uri=callback)
r = requests.post('https://api.twitter.com/oauth/request_token',
auth=auth)
if r.status_code == 200:
data = parse_qs(r.content.decode('utf-8'))
else:
return Response({'message': r.content},
status.HTTP_400_BAD_REQUEST)
return Response(data, status=status.HTTP_200_OK)
class TwitterAccessToken(generics.GenericAPIView):
def post(self, request):
if not SocialApp.objects.filter(provider='twitter').exists():
return Response({'error': ('No Twitter provider found')},
status=status.HTTP_404_NOT_FOUND)
twitter_app = SocialApp.objects.get(provider='twitter')
CONSUMER_KEY = twitter_app.client_id
CONSUMER_SECRET = twitter_app.secret
auth = OAuth1(CONSUMER_KEY, CONSUMER_SECRET,
request.data['oauth_token'],
verifier=request.data['oauth_verifier'])
r = requests\
.post('https://api.twitter.com/oauth/access_token', auth=auth)
if r.status_code == 200:
data = parse_qs(r.content.decode('ascii'))
else:
return Response({'message': r.content},
status.HTTP_400_BAD_REQUEST)
return Response(data, status=status.HTTP_200_OK)
def get_api_key(request):
if request.user.id is None:
raise Http404
......
......@@ -3,10 +3,11 @@ from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.views import defaults as default_views
from django.views.generic import TemplateView
from rest_framework.documentation import include_docs_urls
from base_django_project.users.views import FacebookLogin, TwitterLogin
from base_django_project.users.views import FacebookLogin, TwitterLogin, \
FacebookConnect, TwitterRequestToken, TwitterAccessToken, TwitterConnect
urlpatterns = [
# Django Admin, use {% url 'admin:index' %}
......@@ -17,11 +18,25 @@ urlpatterns = [
# User management
url(r'^api/profile/', include('base_django_project.users.urls', namespace='users')),
url(r'^api/auth/facebook/$', FacebookLogin.as_view(), name='fb_login'),
url(r'^api/auth/twitter/$', TwitterLogin.as_view(), name='tw_login'),
url(r'^rest-auth/facebook/$', FacebookLogin.as_view(), name='fb_login'),
url(r'^rest-auth/twitter/$', TwitterLogin.as_view(), name='tw_login'),
url(r'^rest-auth/facebook/connect/$', FacebookConnect.as_view(), name='fb_connect'),
url(r'^rest-auth/twitter/request_token/$', TwitterRequestToken.as_view(), name='twitter_request_token'),
url(r'^rest-auth/twitter/access_token/$', TwitterAccessToken.as_view(), name='twitter_access_token'),
url(r'^rest-auth/twitter/connect/$', TwitterConnect.as_view(), name='twitter_connect'),
url(r'^rest-auth/', include('rest_auth.urls')),
url(r'^rest-auth/registration/', include('rest_auth.registration.urls')),
url(r'^accounts/', include('allauth.urls')),
## Dummy urls only to create reverse
url(r'^recuperar-senha/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<token>[0-9A-Za-z]{1,13}-[0-9A-Za-z]{1,20})/$', TemplateView.as_view(), name='password_reset_confirm'),
url(r'^verificar-email/(?P<key>[-:\w]+)/$', TemplateView.as_view(), name='account_confirm_email'),
url(r'^verificar-email/$', TemplateView.as_view(), name='account_email_verification_sent'),
url(r'^perfil/editar/twitter/connect/callback$', TemplateView.as_view(), name='twitter_connect_callback'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
if settings.DEBUG:
......
......@@ -39,7 +39,7 @@ django-redis==4.8.0
redis>=2.10.5
django-filter==1.0.2
djangorestframework==3.7.1
djangorestframework==3.8.2
django-rest-auth[with_social]==0.9.3
django-cors-headers==1.2.0
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment