|
| 1 | +import business.models |
| 2 | +import business.serializers |
| 3 | +import rest_framework.exceptions |
| 4 | +import rest_framework.generics |
| 5 | +import rest_framework.response |
| 6 | +import rest_framework.serializers |
| 7 | +import rest_framework.status |
| 8 | +import rest_framework_simplejwt.exceptions |
| 9 | +import rest_framework_simplejwt.tokens |
| 10 | +import rest_framework_simplejwt.views |
| 11 | + |
| 12 | +import core.views |
| 13 | + |
| 14 | + |
| 15 | +class CompanySignUpView( |
| 16 | + core.views.BaseCustomResponseMixin, |
| 17 | + rest_framework.generics.CreateAPIView, |
| 18 | +): |
| 19 | + def post(self, request): |
| 20 | + try: |
| 21 | + serializer = business.serializers.CompanySignUpSerializer( |
| 22 | + data=request.data, |
| 23 | + ) |
| 24 | + serializer.is_valid(raise_exception=True) |
| 25 | + except ( |
| 26 | + rest_framework.serializers.ValidationError, |
| 27 | + rest_framework_simplejwt.exceptions.TokenError, |
| 28 | + ) as e: |
| 29 | + if isinstance(e, rest_framework.serializers.ValidationError): |
| 30 | + return self.handle_validation_error() |
| 31 | + |
| 32 | + raise rest_framework_simplejwt.exceptions.InvalidToken(str(e)) |
| 33 | + |
| 34 | + company = serializer.save() |
| 35 | + |
| 36 | + refresh = rest_framework_simplejwt.tokens.RefreshToken() |
| 37 | + refresh['user_type'] = 'company' |
| 38 | + refresh['company_id'] = company.id |
| 39 | + refresh['token_version'] = company.token_version |
| 40 | + |
| 41 | + access_token = refresh.access_token |
| 42 | + access_token['user_type'] = 'company' |
| 43 | + access_token['company_id'] = company.id |
| 44 | + refresh['token_version'] = company.token_version |
| 45 | + |
| 46 | + response_data = { |
| 47 | + 'access': str(access_token), |
| 48 | + 'refresh': str(refresh), |
| 49 | + } |
| 50 | + |
| 51 | + return rest_framework.response.Response( |
| 52 | + response_data, |
| 53 | + status=rest_framework.status.HTTP_200_OK, |
| 54 | + ) |
| 55 | + |
| 56 | + |
| 57 | +class CompanySignInView( |
| 58 | + core.views.BaseCustomResponseMixin, |
| 59 | + rest_framework_simplejwt.views.TokenObtainPairView, |
| 60 | +): |
| 61 | + def post(self, request): |
| 62 | + try: |
| 63 | + serializer = business.serializers.CompanySignInSerializer( |
| 64 | + data=request.data, |
| 65 | + ) |
| 66 | + serializer.is_valid(raise_exception=True) |
| 67 | + except ( |
| 68 | + rest_framework.serializers.ValidationError, |
| 69 | + rest_framework_simplejwt.exceptions.TokenError, |
| 70 | + ) as e: |
| 71 | + if isinstance(e, rest_framework.serializers.ValidationError): |
| 72 | + return self.handle_validation_error() |
| 73 | + |
| 74 | + raise rest_framework_simplejwt.exceptions.InvalidToken(str(e)) |
| 75 | + |
| 76 | + company = business.models.Company.objects.get( |
| 77 | + email=serializer.validated_data['email'], |
| 78 | + ) |
| 79 | + company.token_version += 1 |
| 80 | + company.save() |
| 81 | + |
| 82 | + refresh = rest_framework_simplejwt.tokens.RefreshToken() |
| 83 | + refresh['user_type'] = 'company' |
| 84 | + refresh['company_id'] = company.id |
| 85 | + refresh['token_version'] = company.token_version |
| 86 | + |
| 87 | + access_token = refresh.access_token |
| 88 | + access_token['user_type'] = 'company' |
| 89 | + access_token['company_id'] = company.id |
| 90 | + |
| 91 | + response_data = { |
| 92 | + 'access': str(access_token), |
| 93 | + 'refresh': str(refresh), |
| 94 | + } |
| 95 | + |
| 96 | + return rest_framework.response.Response( |
| 97 | + response_data, |
| 98 | + status=rest_framework.status.HTTP_200_OK, |
| 99 | + ) |
0 commit comments