diff --git a/src/success_response/success_rest_framework_simplejwt/views.py b/src/success_response/success_rest_framework_simplejwt/views.py index 0838fd6..f590bf7 100644 --- a/src/success_response/success_rest_framework_simplejwt/views.py +++ b/src/success_response/success_rest_framework_simplejwt/views.py @@ -1,29 +1,54 @@ -from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView, TokenVerifyView +from rest_framework_simplejwt.views import TokenObtainPairView, TokenRefreshView, TokenVerifyView, TokenViewBase +from rest_framework.exceptions import ValidationError +from rest_framework.request import Request from ..response import SuccessResponse -# Custom class for obtaining JWT access and refresh tokens, wrapping the response in SuccessResponse -class SuccessTokenObtainPairView(TokenObtainPairView): +class SuccessTokenViewBase(TokenViewBase): + """Base class for customizing JWT token-related views with SuccessResponse.""" + + def handle_exception(self, serializer, request_data): + """Handle exceptions during token processing and return a proper SuccessResponse.""" + try: + serializer.is_valid(raise_exception=True) + except ValidationError as e: + detail = ' '.join( + f"{field}: {' '.join(errors)}" for field, errors in e.detail.items() + ) + return SuccessResponse({'detail': detail}, success=False) + except Exception as e: + return SuccessResponse({'detail': str(e)}, success=False) + + return SuccessResponse(serializer.data) + + def post(self, request: Request, *args, **kwargs): + """Process POST requests using the appropriate serializer.""" + serializer = self.get_serializer(data=request.data) + return self.handle_exception(serializer, request.data) + + +# Custom class for obtaining JWT access and refresh tokens +class SuccessTokenObtainPairView(SuccessTokenViewBase, TokenObtainPairView): + """Override TokenObtainPairView to wrap the response in SuccessResponse.""" + def post(self, request, *args, **kwargs): - # Call the original post method from TokenObtainPairView to handle token generation response = super().post(request, *args, **kwargs) - # Return the token data in the custom SuccessResponse format return SuccessResponse(response.data) -# Custom class for refreshing JWT access tokens, wrapping the response in SuccessResponse -class SuccessTokenRefreshView(TokenRefreshView): +# Custom class for refreshing JWT access tokens +class SuccessTokenRefreshView(SuccessTokenViewBase, TokenRefreshView): + """Override TokenRefreshView to wrap the response in SuccessResponse.""" + def post(self, request, *args, **kwargs): - # Call the original post method from TokenRefreshView to handle token refresh response = super().post(request, *args, **kwargs) - # Return the refreshed token data in the custom SuccessResponse format return SuccessResponse(response.data) -# Custom class for verifying JWT tokens, wrapping the response in SuccessResponse -class SuccessTokenVerifyView(TokenVerifyView): +# Custom class for verifying JWT tokens +class SuccessTokenVerifyView(SuccessTokenViewBase, TokenVerifyView): + """Override TokenVerifyView to wrap the response in SuccessResponse.""" + def post(self, request, *args, **kwargs): - # Call the original post method from TokenVerifyView to verify the token response = super().post(request, *args, **kwargs) - # Return the verification result in the custom SuccessResponse format return SuccessResponse(response.data)