-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviews.py
More file actions
130 lines (107 loc) · 4.16 KB
/
views.py
File metadata and controls
130 lines (107 loc) · 4.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import rest_framework.exceptions
import rest_framework.generics
import rest_framework.permissions
import rest_framework.response
import rest_framework.serializers
import rest_framework.status
import rest_framework_simplejwt.exceptions
import rest_framework_simplejwt.tokens
import rest_framework_simplejwt.views
import business.models
import business.permissions
import business.serializers
import core.views
class CompanySignUpView(
core.views.BaseCustomResponseMixin,
rest_framework.generics.CreateAPIView,
):
def post(self, request):
try:
serializer = business.serializers.CompanySignUpSerializer(
data=request.data,
)
serializer.is_valid(raise_exception=True)
except (
rest_framework.serializers.ValidationError,
rest_framework_simplejwt.exceptions.TokenError,
) as e:
if isinstance(e, rest_framework.serializers.ValidationError):
return self.handle_validation_error()
raise rest_framework_simplejwt.exceptions.InvalidToken(str(e))
company = serializer.save()
refresh = rest_framework_simplejwt.tokens.RefreshToken()
refresh['user_type'] = 'company'
refresh['company_id'] = str(company.id)
refresh['token_version'] = company.token_version
access_token = refresh.access_token
access_token['user_type'] = 'company'
access_token['company_id'] = str(company.id)
refresh['token_version'] = company.token_version
response_data = {
'access': str(access_token),
'refresh': str(refresh),
}
return rest_framework.response.Response(
response_data,
status=rest_framework.status.HTTP_200_OK,
)
class CompanySignInView(
core.views.BaseCustomResponseMixin,
rest_framework_simplejwt.views.TokenObtainPairView,
):
def post(self, request):
try:
serializer = business.serializers.CompanySignInSerializer(
data=request.data,
)
serializer.is_valid(raise_exception=True)
except (
rest_framework.serializers.ValidationError,
rest_framework_simplejwt.exceptions.TokenError,
) as e:
if isinstance(e, rest_framework.serializers.ValidationError):
return self.handle_validation_error()
raise rest_framework_simplejwt.exceptions.InvalidToken(str(e))
company = business.models.Company.objects.get(
email=serializer.validated_data['email'],
)
company.token_version += 1
company.save()
refresh = rest_framework_simplejwt.tokens.RefreshToken()
refresh['user_type'] = 'company'
refresh['company_id'] = str(company.id)
refresh['token_version'] = company.token_version
access_token = refresh.access_token
access_token['user_type'] = 'company'
access_token['company_id'] = str(company.id)
response_data = {
'access': str(access_token),
'refresh': str(refresh),
}
return rest_framework.response.Response(
response_data,
status=rest_framework.status.HTTP_200_OK,
)
class CompanyTokenRefreshView(rest_framework_simplejwt.views.TokenRefreshView):
serializer_class = business.serializers.CompanyTokenRefreshSerializer
class PromoCreateView(rest_framework.views.APIView):
permission_classes = [
rest_framework.permissions.IsAuthenticated,
business.permissions.IsCompanyUser,
]
serializer_class = business.serializers.PromoCreateSerializer
def post(self, request, *args, **kwargs):
serializer = self.serializer_class(
data=request.data,
context={'request': request},
)
if serializer.is_valid():
instance = serializer.save()
return rest_framework.response.Response(
{'id': str(instance.id)},
status=rest_framework.status.HTTP_201_CREATED,
)
return rest_framework.response.Response(
serializer.errors,
status=rest_framework.status.HTTP_400_BAD_REQUEST,
)