Skip to content

Commit ace8e52

Browse files
refactor(user auth): Improve SignUpView and SignInView implementation
- Refactored SignUpView and SignInView to better leverage DRF generic views and SimpleJWT patterns. - Simplified view logic by removing manual error handling and post overrides where applicable, relying on serializers and base view implementations. - Removed redundant imports. - Removed BaseCustomResponseMixin.
1 parent 55241c6 commit ace8e52

File tree

3 files changed

+12
-51
lines changed

3 files changed

+12
-51
lines changed

promo_code/core/views.py

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,9 @@
22
import django.views
33
import rest_framework.permissions
44
import rest_framework.response
5-
import rest_framework.status
65
import rest_framework.views
76

87

9-
class BaseCustomResponseMixin:
10-
error_response = {'status': 'error', 'message': 'Error in request data.'}
11-
12-
def handle_validation_error(self):
13-
return rest_framework.response.Response(
14-
self.error_response,
15-
status=rest_framework.status.HTTP_400_BAD_REQUEST,
16-
)
17-
18-
198
class PingView(django.views.View):
209
def get(self, request, *args, **kwargs):
2110
return django.http.HttpResponse('PROOOOOOOOOOOOOOOOOD', status=200)

promo_code/user/urls.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@
99
urlpatterns = [
1010
django.urls.path(
1111
'auth/sign-up',
12-
user.views.SignUpView.as_view(),
12+
user.views.UserSignUpView.as_view(),
1313
name='sign-up',
1414
),
1515
django.urls.path(
1616
'auth/sign-in',
17-
rest_framework_simplejwt.views.TokenObtainPairView.as_view(),
17+
user.views.UserSignInView.as_view(),
1818
name='sign-in',
1919
),
2020
django.urls.path(

promo_code/user/views.py

Lines changed: 10 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,67 +1,39 @@
1-
import rest_framework.exceptions
21
import rest_framework.generics
32
import rest_framework.response
4-
import rest_framework.serializers
53
import rest_framework.status
6-
import rest_framework_simplejwt.exceptions
74
import rest_framework_simplejwt.tokens
85
import rest_framework_simplejwt.views
96

10-
import core.views
117
import user.serializers
128

139

14-
class SignUpView(
15-
core.views.BaseCustomResponseMixin,
10+
class UserSignUpView(
1611
rest_framework.generics.CreateAPIView,
1712
):
1813
serializer_class = user.serializers.SignUpSerializer
1914

2015
def create(self, request, *args, **kwargs):
2116
serializer = self.get_serializer(data=request.data)
22-
23-
try:
24-
serializer.is_valid(raise_exception=True)
25-
except rest_framework.exceptions.ValidationError:
26-
return self.handle_validation_error()
17+
serializer.is_valid(raise_exception=True)
2718

2819
user = serializer.save()
29-
3020
refresh = rest_framework_simplejwt.tokens.RefreshToken.for_user(user)
3121
refresh['token_version'] = user.token_version
22+
3223
access_token = refresh.access_token
3324

25+
response_data = {
26+
'access': str(access_token),
27+
'refresh': str(refresh),
28+
}
29+
3430
return rest_framework.response.Response(
35-
{'access': str(access_token), 'refresh': str(refresh)},
31+
response_data,
3632
status=rest_framework.status.HTTP_200_OK,
3733
)
3834

3935

40-
class SignInView(
41-
core.views.BaseCustomResponseMixin,
36+
class UserSignInView(
4237
rest_framework_simplejwt.views.TokenObtainPairView,
4338
):
4439
serializer_class = user.serializers.SignInSerializer
45-
46-
def post(self, request, *args, **kwargs):
47-
try:
48-
serializer = self.get_serializer(data=request.data)
49-
serializer.is_valid(raise_exception=True)
50-
except (
51-
rest_framework.serializers.ValidationError,
52-
rest_framework_simplejwt.exceptions.TokenError,
53-
) as e:
54-
if isinstance(e, rest_framework.serializers.ValidationError):
55-
return self.handle_validation_error()
56-
57-
raise rest_framework_simplejwt.exceptions.InvalidToken(str(e))
58-
59-
response_data = {
60-
'access': serializer.validated_data['access'],
61-
'refresh': serializer.validated_data['refresh'],
62-
}
63-
64-
return rest_framework.response.Response(
65-
response_data,
66-
status=rest_framework.status.HTTP_200_OK,
67-
)

0 commit comments

Comments
 (0)