-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix validation error handling and improve response format
- Loading branch information
1 parent
1203073
commit dec1a83
Showing
1 changed file
with
38 additions
and
13 deletions.
There are no files selected for viewing
51 changes: 38 additions & 13 deletions
51
src/success_response/success_rest_framework_simplejwt/views.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) |