0

As I'm working, my ./views.py is becoming really competitive and I'm wondering if there is a way to refactor it to be DRY. I think in order to do so, I would need the ability to use specify multiple serializers per view.

data would need to be less ambigious so that it actually describes the data it is receiving so that it can be passed into the correct serializer, which would mean it would need to know the API route the received data came from. Not sure how to do that except with a one class per route, how I currently have it setup.

Then there would need to be a way to specify multiple serializers in a view to send the respective data to. Not seeing that this is possible.

# ./urls.py

from .views import (
    SecurityQuestionsAPIView,
    UserSigninTokenAPIView,
    UsernameRecoveryAPIView,
    ValidateKeyAPIView
)

urlpatterns = [
    url(r'^signin/', UserSigninTokenAPIView.as_view(), name='signin'),
    url(r'^verify/', verify_jwt_token),
    url(r'^refresh/', refresh_jwt_token),
    url(r'^username_recovery/', UsernameRecoveryAPIView.as_view(), name='username_recovery'),
    url(r'^validate_key/', ValidateKeyAPIView.as_view(), name='validate_key'),
    url(r'^security_questions/', SecurityQuestionsAPIView.as_view(), name='security_questions'),
]

# ./views.py
from .serializers import (
    SecurityQuestionsSerializer,
    UserSigninTokenSerializer, 
    UsernameRecoverySerializer,
    ValidateKeySerializer
)

# Used for logging into the web application
class UserSigninTokenAPIView(APIView):
    permission_classes = [AllowAny]
    serializer_class = UserSigninTokenSerializer

    def post(self, request, *args, **kwargs):
        data = request.data
        serializer = UserSigninTokenSerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            new_data = serializer.data
            return Response(new_data, status=HTTP_200_OK)
        return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)

class UsernameRecoveryAPIView(APIView):
    permission_classes = [AllowAny]
    serializer_class = UsernameRecoverySerializer

    def post(self, request, *args, **kwargs):
        data = request.data
        serializer = UsernameRecoverySerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            new_data = serializer.data
            return Response(new_data, status=HTTP_200_OK)
        return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)

class ValidateKeyAPIView(APIView):
    permission_classes = [AllowAny]
    serializer_class = ValidateKeySerializer

    def post(self, request, *args, **kwargs):
        data = request.data
        serializer = ValidateKeySerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            new_data = serializer.data
            return Response(new_data, status=HTTP_200_OK)
        return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)

class SecurityQuestionsAPIView(APIView):
    permission_classes = [AllowAny]
    serializer_class = SecurityQuestionsSerializer

    def post(self, request, *args, **kwargs):
        data = request.data
        serializer = SecurityQuestionsSerializer(data=data)
        if serializer.is_valid(raise_exception=True):
            new_data = serializer.data
            return Response(new_data, status=HTTP_200_OK)
        return Response(serializer.errors, status=HTTP_400_BAD_REQUEST)

1 Answer 1

1

You are likely looking for the CreateApiView

class UserSigninTokenAPIView(CreateApiView):
    permission_classes = [AllowAny]
    serializer_class = UserSigninTokenSerializer


class UsernameRecoveryAPIView(CreateApiView):
    permission_classes = [AllowAny]
    serializer_class = UsernameRecoverySerializer


class ValidateKeyAPIView(CreateApiView):
    permission_classes = [AllowAny]
    serializer_class = ValidateKeySerializer


class SecurityQuestionsAPIView(CreateApiView):
    permission_classes = [AllowAny]
    serializer_class = SecurityQuestionsSerializer
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.