-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
executable file
·52 lines (35 loc) · 1.74 KB
/
views.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
import logging
from django.contrib.auth.models import User
from rest_framework import viewsets, permissions, status, generics
from rest_framework.response import Response
from social_django.utils import load_backend, load_strategy
from shotclock import models, serializers
logger = logging.Logger(__name__)
SOCIAL_AUTH_PROVIDER = 'spotify'
class DefaultViewSet(viewsets.ModelViewSet):
permission_classes = [permissions.IsAuthenticated, ]
class UserViewSet(DefaultViewSet):
queryset = User.objects.all()
serializer_class = serializers.UserSerializer
class MusicProfileViewSet(DefaultViewSet):
queryset = models.MusicProfile.objects.all()
serializer_class = serializers.MusicProfileSerializer
class PowerHourViewSet(DefaultViewSet):
queryset = models.PowerHour.objects.all()
serializer_class = serializers.PowerHourSerializer
class SocialAuth(generics.CreateAPIView):
def create(self, request, *args, **kwargs):
redirect = request.path
try:
access_token = request.data['access_token']
except KeyError:
return Response({'detail': "'access_token' is required."}, status=status.HTTP_400_BAD_REQUEST)
strategy = load_strategy(request)
backend = load_backend(strategy, SOCIAL_AUTH_PROVIDER, redirect)
request.social_auth_backend = backend
try:
user = backend.do_auth(access_token, expires=None, *args, **kwargs)
user_serializer = serializers.UserSerializer(user, context={'request': request})
return Response(user_serializer.data, status=status.HTTP_200_OK)
except Exception as e:
return Response({'detail': e.args[0]}, status=status.HTTP_400_BAD_REQUEST)