mirror of
https://github.com/fergalmoran/dss.api.git
synced 2025-12-29 12:51:10 +00:00
Python 3 upgrade finished
This commit is contained in:
@@ -36,11 +36,11 @@ class SocialLoginHandler(APIView):
|
||||
if auth_token and backend:
|
||||
try:
|
||||
user = auth_by_token(request, backend)
|
||||
except Exception, e:
|
||||
except Exception as e:
|
||||
logger.exception(e)
|
||||
return Response({
|
||||
'status': 'Bad request',
|
||||
'message': e.message
|
||||
'message': e
|
||||
}, status=status.HTTP_400_BAD_REQUEST)
|
||||
|
||||
if user:
|
||||
|
||||
120
api/old_auth.py
120
api/old_auth.py
@@ -1,120 +0,0 @@
|
||||
from requests import HTTPError
|
||||
from rest_framework import parsers
|
||||
from rest_framework.authentication import get_authorization_header
|
||||
from rest_framework.authtoken.models import Token
|
||||
from rest_framework.authtoken.serializers import AuthTokenSerializer
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.status import *
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework import status
|
||||
from rest_framework import renderers
|
||||
from social.apps.django_app.utils import psa
|
||||
|
||||
|
||||
class LoginException(Exception):
|
||||
pass
|
||||
|
||||
|
||||
@psa()
|
||||
def register_by_access_token(request, backend):
|
||||
auth = get_authorization_header(request).split()
|
||||
if not auth or auth[0].lower() != b'social':
|
||||
raise LoginException("Unable to register_by_access_token: No token header provided")
|
||||
|
||||
access_token = auth[1]
|
||||
return request.backend.do_auth(access_token)
|
||||
"""
|
||||
class RefreshTokenView(APIView):
|
||||
serializer_class = AuthTokenSerializer
|
||||
model = Token
|
||||
|
||||
def post(self, request):
|
||||
# Here we call PSA to authenticate like we would if we used PSA on server side.
|
||||
try:
|
||||
backend = request.META.get('HTTP_AUTH_BACKEND')
|
||||
if backend is None:
|
||||
# Work around django test client oddness
|
||||
return Response("No Auth-Backend header specified", HTTP_400_BAD_REQUEST)
|
||||
|
||||
user = refresh_access_token(request, backend)
|
||||
|
||||
# If user is active we get or create the REST token and send it back with user data
|
||||
if user and user.is_active:
|
||||
token, created = Token.objects.get_or_create(user=user)
|
||||
return Response({
|
||||
'slug': user.userprofile.slug,
|
||||
'token': token.key
|
||||
})
|
||||
except LoginException, ex:
|
||||
return Response(ex.message, HTTP_400_BAD_REQUEST)
|
||||
except HTTPError, ex:
|
||||
if ex.response.status_code == 400:
|
||||
return Response(ex.message, HTTP_401_UNAUTHORIZED)
|
||||
return Response(ex.message, HTTP_400_BAD_REQUEST)
|
||||
"""
|
||||
|
||||
class ObtainAuthToken(APIView):
|
||||
serializer_class = AuthTokenSerializer
|
||||
model = Token
|
||||
|
||||
def post(self, request):
|
||||
# Here we call PSA to authenticate like we would if we used PSA on server side.
|
||||
try:
|
||||
backend = request.META.get('HTTP_AUTH_BACKEND')
|
||||
if backend is None:
|
||||
# Work around django test client oddness
|
||||
return Response("No Auth-Backend header specified", HTTP_400_BAD_REQUEST)
|
||||
|
||||
user = register_by_access_token(request, backend)
|
||||
|
||||
# If user is active we get or create the REST token and send it back with user data
|
||||
if user and user.is_active:
|
||||
token, created = Token.objects.get_or_create(user=user)
|
||||
return Response({
|
||||
'slug': user.userprofile.slug,
|
||||
'token': token.key
|
||||
})
|
||||
except LoginException, ex:
|
||||
return Response(ex.message, HTTP_400_BAD_REQUEST)
|
||||
except HTTPError, ex:
|
||||
if ex.response.status_code == 400:
|
||||
return Response(ex.message, HTTP_401_UNAUTHORIZED)
|
||||
return Response(ex.message, HTTP_400_BAD_REQUEST)
|
||||
|
||||
|
||||
class ObtainUser(APIView):
|
||||
throttle_classes = ()
|
||||
permission_classes = ()
|
||||
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
|
||||
renderer_classes = (renderers.JSONRenderer,)
|
||||
serializer_class = AuthTokenSerializer
|
||||
model = Token
|
||||
|
||||
def get(self, request):
|
||||
if request.META.get('HTTP_AUTHORIZATION'):
|
||||
|
||||
auth = request.META.get('HTTP_AUTHORIZATION').split()
|
||||
|
||||
if not auth or auth[0].lower() != b'token' or len(auth) != 2:
|
||||
msg = 'Invalid token header. No credentials provided.'
|
||||
return Response(msg, status=status.HTTP_401_UNAUTHORIZED)
|
||||
|
||||
token = Token.objects.get(key=auth[1])
|
||||
if token and token.user.is_active:
|
||||
return Response({'id': token.user_id, 'name': token.user.username, 'firstname': token.user.first_name,
|
||||
'userRole': 'user', 'token': token.key})
|
||||
else:
|
||||
return Response(status=status.HTTP_401_UNAUTHORIZED)
|
||||
|
||||
|
||||
class ObtainLogout(APIView):
|
||||
throttle_classes = ()
|
||||
permission_classes = ()
|
||||
parser_classes = (parsers.FormParser, parsers.MultiPartParser, parsers.JSONParser,)
|
||||
renderer_classes = (renderers.JSONRenderer,)
|
||||
serializer_class = AuthTokenSerializer
|
||||
model = Token
|
||||
|
||||
# Logout le user
|
||||
def get(self, request):
|
||||
return Response({'User': ''})
|
||||
@@ -208,7 +208,7 @@ class MixSerializer(serializers.ModelSerializer):
|
||||
validated_data.pop('genres', None)
|
||||
|
||||
return super(MixSerializer, self).update(instance, validated_data)
|
||||
except MixUpdateException, ex:
|
||||
except MixUpdateException as ex:
|
||||
raise ex
|
||||
|
||||
def is_valid(self, raise_exception=False):
|
||||
@@ -314,8 +314,8 @@ class UserProfileSerializer(serializers.ModelSerializer):
|
||||
def get_roles(self, obj):
|
||||
try:
|
||||
return obj.get_roles()
|
||||
except Exception, ex:
|
||||
print "Error getting roles: " + ex.message
|
||||
except Exception as ex:
|
||||
print("Error getting roles: " + ex)
|
||||
return []
|
||||
|
||||
def get_isme(self, obj):
|
||||
|
||||
@@ -3,4 +3,4 @@ from spa.models.mix import Mix
|
||||
|
||||
class UploadTest(unittest.TestCase):
|
||||
def run(self, result=None):
|
||||
print "Argle Bargle"
|
||||
print("Argle Bargle")
|
||||
@@ -29,15 +29,15 @@ class DebugView(APIView):
|
||||
authentication_classes = (JSONWebTokenAuthentication, )
|
||||
|
||||
def get(self, request):
|
||||
print self.request.session
|
||||
print(self.request.session)
|
||||
return Response({'status': 'ok', 'session': self.request.session.session_key},
|
||||
status=status.HTTP_200_OK)
|
||||
|
||||
def post(self, request, format=None):
|
||||
try:
|
||||
activity.post_activity('user:message', request.user.userprofile.get_session_id(), 'Hello Sailor')
|
||||
except Exception, ex:
|
||||
print ex.message
|
||||
except Exception as ex:
|
||||
print(ex)
|
||||
|
||||
return Response({
|
||||
'status': request.user.first_name,
|
||||
|
||||
10
api/views.py
10
api/views.py
@@ -61,7 +61,7 @@ class CommentViewSet(viewsets.ModelViewSet):
|
||||
)
|
||||
except Mix.DoesNotExist:
|
||||
pass
|
||||
except Exception, ex:
|
||||
except Exception as ex:
|
||||
pass
|
||||
|
||||
|
||||
@@ -134,7 +134,7 @@ class AttachedImageUploadView(views.APIView):
|
||||
return Response(HTTP_202_ACCEPTED)
|
||||
except ObjectDoesNotExist:
|
||||
return Response(status=HTTP_404_NOT_FOUND)
|
||||
except Exception, ex:
|
||||
except Exception as ex:
|
||||
logger.exception(ex)
|
||||
|
||||
return Response(status=HTTP_401_UNAUTHORIZED)
|
||||
@@ -196,7 +196,7 @@ class PartialMixUploadView(views.APIView):
|
||||
).delay()
|
||||
logger.debug("Waveform task started")
|
||||
|
||||
except Exception, ex:
|
||||
except Exception as ex:
|
||||
logger.exception(ex)
|
||||
response = \
|
||||
'Unable to connect to rabbitmq, there may be a delay in getting your mix online'
|
||||
@@ -207,8 +207,8 @@ class PartialMixUploadView(views.APIView):
|
||||
'uid': uid
|
||||
}
|
||||
return Response(file_dict, HTTP_202_ACCEPTED)
|
||||
except Exception, ex:
|
||||
logger.exception(ex.message)
|
||||
except Exception as ex:
|
||||
logger.exception(ex)
|
||||
raise
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user