|
7 | 7 | from rest_framework.response import Response
|
8 | 8 | from rest_framework.permissions import (AllowAny,
|
9 | 9 | IsAuthenticated)
|
10 |
| -from rest_framework.decorators import detail_route |
11 |
| -from rest_framework.viewsets import GenericViewSet |
12 |
| -from rest_framework.generics import CreateAPIView |
| 10 | +from rest_framework.generics import CreateAPIView, ListAPIView, GenericAPIView |
| 11 | +from rest_framework.exceptions import NotFound |
13 | 12 | from rest_framework import status
|
14 | 13 |
|
| 14 | +from allauth.account.adapter import get_adapter |
15 | 15 | from allauth.account.views import ConfirmEmailView
|
16 | 16 | from allauth.account.utils import complete_signup
|
17 | 17 | from allauth.account import app_settings as allauth_settings
|
| 18 | +from allauth.socialaccount import signals |
| 19 | +from allauth.socialaccount.adapter import get_adapter as get_social_adapter |
| 20 | +from allauth.socialaccount.models import SocialAccount |
18 | 21 |
|
19 | 22 | from rest_auth.app_settings import (TokenSerializer,
|
20 | 23 | JWTSerializer,
|
21 | 24 | create_token)
|
22 | 25 | from rest_auth.models import TokenModel
|
23 |
| -from rest_auth.registration.serializers import (SocialLoginSerializer, |
24 |
| - VerifyEmailSerializer, |
| 26 | +from rest_auth.registration.serializers import (VerifyEmailSerializer, |
| 27 | + SocialLoginSerializer, |
| 28 | + SocialAccountSerializer, |
25 | 29 | SocialConnectSerializer)
|
26 | 30 | from rest_auth.utils import jwt_encode
|
27 | 31 | from rest_auth.views import LoginView
|
@@ -94,98 +98,89 @@ def post(self, request, *args, **kwargs):
|
94 | 98 | return Response({'detail': _('ok')}, status=status.HTTP_200_OK)
|
95 | 99 |
|
96 | 100 |
|
97 |
| -if 'allauth.socialaccount' in settings.INSTALLED_APPS: |
98 |
| - from allauth.socialaccount import signals |
99 |
| - from allauth.socialaccount.models import SocialAccount |
100 |
| - from allauth.socialaccount.adapter import get_adapter |
| 101 | +class SocialLoginView(LoginView): |
| 102 | + """ |
| 103 | + class used for social authentications |
| 104 | + example usage for facebook with access_token |
| 105 | + ------------- |
| 106 | + from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter |
101 | 107 |
|
102 |
| - from rest_auth.registration.serializers import SocialAccountSerializer |
| 108 | + class FacebookLogin(SocialLoginView): |
| 109 | + adapter_class = FacebookOAuth2Adapter |
| 110 | + ------------- |
103 | 111 |
|
| 112 | + example usage for facebook with code |
104 | 113 |
|
105 |
| - class SocialLoginView(LoginView): |
106 |
| - """ |
107 |
| - class used for social authentications |
108 |
| - example usage for facebook with access_token |
109 |
| - ------------- |
110 |
| - from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter |
| 114 | + ------------- |
| 115 | + from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter |
| 116 | + from allauth.socialaccount.providers.oauth2.client import OAuth2Client |
111 | 117 |
|
112 |
| - class FacebookLogin(SocialLoginView): |
113 |
| - adapter_class = FacebookOAuth2Adapter |
114 |
| - ------------- |
| 118 | + class FacebookLogin(SocialLoginView): |
| 119 | + adapter_class = FacebookOAuth2Adapter |
| 120 | + client_class = OAuth2Client |
| 121 | + callback_url = 'localhost:8000' |
| 122 | + ------------- |
| 123 | + """ |
| 124 | + serializer_class = SocialLoginSerializer |
115 | 125 |
|
116 |
| - example usage for facebook with code |
| 126 | + def process_login(self): |
| 127 | + get_adapter(self.request).login(self.request, self.user) |
117 | 128 |
|
118 |
| - ------------- |
119 |
| - from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter |
120 |
| - from allauth.socialaccount.providers.oauth2.client import OAuth2Client |
121 | 129 |
|
122 |
| - class FacebookLogin(SocialLoginView): |
123 |
| - adapter_class = FacebookOAuth2Adapter |
124 |
| - client_class = OAuth2Client |
125 |
| - callback_url = 'localhost:8000' |
126 |
| - ------------- |
127 |
| - """ |
| 130 | +class SocialConnectView(LoginView): |
| 131 | + """ |
| 132 | + class used for social account linking |
128 | 133 |
|
129 |
| - serializer_class = SocialLoginSerializer |
| 134 | + example usage for facebook with access_token |
| 135 | + ------------- |
| 136 | + from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter |
130 | 137 |
|
131 |
| - def process_login(self): |
132 |
| - get_adapter(self.request).login(self.request, self.user) |
| 138 | + class FacebookConnect(SocialConnectView): |
| 139 | + adapter_class = FacebookOAuth2Adapter |
| 140 | + ------------- |
| 141 | + """ |
| 142 | + serializer_class = SocialConnectSerializer |
| 143 | + permission_classes = (IsAuthenticated,) |
133 | 144 |
|
| 145 | + def process_login(self): |
| 146 | + get_adapter(self.request).login(self.request, self.user) |
134 | 147 |
|
135 |
| - class SocialConnectView(SocialLoginView): |
136 |
| - """ |
137 |
| - class used for social account linking |
138 | 148 |
|
139 |
| - example usage for facebook with access_token |
140 |
| - ------------- |
141 |
| - from allauth.socialaccount.providers.facebook.views import FacebookOAuth2Adapter |
| 149 | +class SocialAccountListView(ListAPIView): |
| 150 | + """ |
| 151 | + List SocialAccounts for the currently logged in user |
| 152 | + """ |
| 153 | + serializer_class = SocialAccountSerializer |
| 154 | + permission_classes = (IsAuthenticated,) |
142 | 155 |
|
143 |
| - class FacebookConnect(SocialConnectView): |
144 |
| - adapter_class = FacebookOAuth2Adapter |
145 |
| - ------------- |
146 |
| - """ |
| 156 | + def get_queryset(self): |
| 157 | + return SocialAccount.objects.filter(user=self.request.user) |
147 | 158 |
|
148 |
| - serializer_class = SocialConnectSerializer |
149 |
| - permission_classes = (IsAuthenticated,) |
150 | 159 |
|
| 160 | +class SocialAccountDisconnectView(GenericAPIView): |
| 161 | + """ |
| 162 | + Disconnect SocialAccount from remote service for |
| 163 | + the currently logged in user |
| 164 | + """ |
| 165 | + serializer_class = SocialConnectSerializer |
| 166 | + permission_classes = (IsAuthenticated,) |
151 | 167 |
|
152 |
| - class SocialAccountViewSet(GenericViewSet): |
153 |
| - """ |
154 |
| - allauth SocialAccount REST API read and disconnect views for logged in users |
| 168 | + def get_queryset(self): |
| 169 | + return SocialAccount.objects.filter(user=self.request.user) |
155 | 170 |
|
156 |
| - Refer to the django-allauth package implementation of the models and |
157 |
| - account handling logic for more details, this viewset emulates the allauth web UI. |
158 |
| - """ |
159 |
| - |
160 |
| - serializer_class = SocialAccountSerializer |
161 |
| - permission_classes = (IsAuthenticated,) |
162 |
| - queryset = SocialAccount.objects.none() |
163 |
| - |
164 |
| - def get_queryset(self): |
165 |
| - return SocialAccount.objects.filter(user=self.request.user) |
166 |
| - |
167 |
| - def list(self, request): |
168 |
| - """ |
169 |
| - list SocialAccounts for the currently logged in user |
170 |
| - """ |
171 |
| - |
172 |
| - return Response(self.get_serializer(self.get_queryset(), many=True).data) |
173 |
| - |
174 |
| - @detail_route(methods=['POST']) |
175 |
| - def disconnect(self, request, pk): |
176 |
| - """ |
177 |
| - disconnect SocialAccount from remote service for the currently logged in user |
178 |
| - """ |
| 171 | + def post(self, request, *args, **kwargs): |
| 172 | + accounts = self.get_queryset() |
| 173 | + account = accounts.filter(pk=kwargs['pk']).first() |
| 174 | + if not account: |
| 175 | + raise NotFound |
179 | 176 |
|
180 |
| - accounts = self.get_queryset() |
181 |
| - account = accounts.get(pk=pk) |
182 |
| - get_adapter(self.request).validate_disconnect(account, accounts) |
| 177 | + get_social_adapter(self.request).validate_disconnect(account, accounts) |
183 | 178 |
|
184 |
| - account.delete() |
185 |
| - signals.social_account_removed.send( |
186 |
| - sender=SocialAccount, |
187 |
| - request=self.request, |
188 |
| - socialaccount=account |
189 |
| - ) |
| 179 | + account.delete() |
| 180 | + signals.social_account_removed.send( |
| 181 | + sender=SocialAccount, |
| 182 | + request=self.request, |
| 183 | + socialaccount=account |
| 184 | + ) |
190 | 185 |
|
191 |
| - return Response(self.get_serializer(account).data) |
| 186 | + return Response(self.get_serializer(account).data) |
0 commit comments