-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserializers.py
More file actions
132 lines (114 loc) · 4.19 KB
/
serializers.py
File metadata and controls
132 lines (114 loc) · 4.19 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
131
132
import business.models as business_models
import business.validators
import django.contrib.auth.password_validation
import django.core.exceptions
import django.core.validators
import rest_framework.exceptions
import rest_framework.serializers
import rest_framework.status
import rest_framework_simplejwt.exceptions
import rest_framework_simplejwt.serializers
import rest_framework_simplejwt.tokens
import rest_framework_simplejwt.views
class CompanySignUpSerializer(rest_framework.serializers.ModelSerializer):
password = rest_framework.serializers.CharField(
write_only=True,
required=True,
validators=[django.contrib.auth.password_validation.validate_password],
min_length=8,
max_length=60,
style={'input_type': 'password'},
)
name = rest_framework.serializers.CharField(
required=True,
min_length=5,
max_length=50,
)
email = rest_framework.serializers.EmailField(
required=True,
min_length=8,
max_length=120,
validators=[
business.validators.UniqueEmailValidator(
'This email address is already registered.',
'email_conflict',
),
],
)
class Meta:
model = business_models.Company
fields = (
'name',
'email',
'password',
)
def create(self, validated_data):
try:
company = business_models.Company.objects.create_company(
email=validated_data['email'],
name=validated_data['name'],
password=validated_data['password'],
)
company.token_version += 1
company.save()
return company
except django.core.exceptions.ValidationError as e:
raise rest_framework.serializers.ValidationError(e.messages)
class CompanySignInSerializer(
rest_framework.serializers.Serializer,
):
email = rest_framework.serializers.EmailField(required=True)
password = rest_framework.serializers.CharField(
required=True,
write_only=True,
style={'input_type': 'password'},
)
def validate(self, attrs):
email = attrs.get('email')
password = attrs.get('password')
if not email or not password:
raise rest_framework.exceptions.ValidationError(
{'detail': 'Both email and password are required'},
code='required',
)
try:
company = business_models.Company.objects.get(email=email)
except business_models.Company.DoesNotExist:
raise rest_framework.serializers.ValidationError(
'Invalid credentials',
)
if not company.is_active or not company.check_password(password):
raise rest_framework.exceptions.AuthenticationFailed(
{'detail': 'Invalid credentials or inactive account'},
code='authentication_failed',
)
return attrs
class CompanyTokenRefreshSerializer(
rest_framework_simplejwt.serializers.TokenRefreshSerializer,
):
def validate(self, attrs):
refresh = rest_framework_simplejwt.tokens.RefreshToken(
attrs['refresh'],
)
user_type = refresh.payload.get('user_type', 'user')
if user_type != 'company':
raise rest_framework_simplejwt.exceptions.InvalidToken(
'This refresh endpoint is for company tokens only',
)
company_id = refresh.payload.get('company_id')
if not company_id:
raise rest_framework_simplejwt.exceptions.InvalidToken(
'Company ID missing in token',
)
try:
company = business_models.Company.objects.get(id=company_id)
except business_models.Company.DoesNotExist:
raise rest_framework_simplejwt.exceptions.InvalidToken(
'Company not found',
)
token_version = refresh.payload.get('token_version', 0)
if company.token_version != token_version:
raise rest_framework_simplejwt.exceptions.InvalidToken(
'Token is blacklisted',
)
return super().validate(attrs)