from rest_framework_jwt.views import ObtainJSONWebToken, JSONWebTokenSerializer
from django.http import JsonResponse


class CustomJSONWebTokenSerializer(JSONWebTokenSerializer):
    def validate(self, attrs):
        try:
            return super(CustomJSONWebTokenSerializer, self).validate(attrs)
        except BaseException as e:
            return False


class CustomObtainJSONWebToken(ObtainJSONWebToken):
    serializer_class = CustomJSONWebTokenSerializer

    def post(self, request, *args, **kwargs):
        try:
            return super(CustomObtainJSONWebToken, self).post(request, *args, **kwargs)
        except BaseException as e:
            return JsonResponse({'status_code': 400,
                                 'error': 'Unable to log in with provided credentials.',
                                 'message': 'Unable to log in with provided credentials.'})
