10
10
import rest_framework_simplejwt .tokens
11
11
12
12
import user .constants
13
- import user .models as user_models
13
+ import user .models
14
14
import user .validators
15
15
16
16
@@ -27,10 +27,10 @@ class OtherFieldSerializer(rest_framework.serializers.Serializer):
27
27
)
28
28
29
29
def validate (self , value ):
30
- country = value ['country' ]. upper ()
30
+ country = value ['country' ]
31
31
32
32
try :
33
- pycountry .countries .lookup (country )
33
+ pycountry .countries .lookup (country . upper () )
34
34
except LookupError :
35
35
raise rest_framework .serializers .ValidationError (
36
36
'Invalid ISO 3166-1 alpha-2 country code.' ,
@@ -79,7 +79,7 @@ class SignUpSerializer(rest_framework.serializers.ModelSerializer):
79
79
other = OtherFieldSerializer (required = True )
80
80
81
81
class Meta :
82
- model = user_models .User
82
+ model = user . models .User
83
83
fields = (
84
84
'name' ,
85
85
'surname' ,
@@ -91,17 +91,17 @@ class Meta:
91
91
92
92
def create (self , validated_data ):
93
93
try :
94
- user = user_models .User .objects .create_user (
94
+ user_ = user . models .User .objects .create_user (
95
95
email = validated_data ['email' ],
96
96
name = validated_data ['name' ],
97
97
surname = validated_data ['surname' ],
98
98
avatar_url = validated_data .get ('avatar_url' ),
99
99
other = validated_data ['other' ],
100
100
password = validated_data ['password' ],
101
101
)
102
- user .token_version += 1
103
- user .save ()
104
- return user
102
+ user_ .token_version += 1
103
+ user_ .save ()
104
+ return user_
105
105
except django .core .exceptions .ValidationError as e :
106
106
raise rest_framework .serializers .ValidationError (e .messages )
107
107
@@ -168,3 +168,79 @@ def get_token(cls, user):
168
168
token = super ().get_token (user )
169
169
token ['token_version' ] = user .token_version
170
170
return token
171
+
172
+
173
+ class UserProfileSerializer (rest_framework .serializers .ModelSerializer ):
174
+ name = rest_framework .serializers .CharField (
175
+ required = False ,
176
+ min_length = user .constants .NAME_MIN_LENGTH ,
177
+ max_length = user .constants .NAME_MAX_LENGTH ,
178
+ )
179
+ surname = rest_framework .serializers .CharField (
180
+ required = False ,
181
+ min_length = user .constants .SURNAME_MIN_LENGTH ,
182
+ max_length = user .constants .SURNAME_MAX_LENGTH ,
183
+ )
184
+ email = rest_framework .serializers .EmailField (
185
+ required = False ,
186
+ min_length = user .constants .EMAIL_MIN_LENGTH ,
187
+ max_length = user .constants .EMAIL_MAX_LENGTH ,
188
+ validators = [
189
+ user .validators .UniqueEmailValidator (
190
+ 'This email address is already registered.' ,
191
+ 'email_conflict' ,
192
+ ),
193
+ ],
194
+ )
195
+ password = rest_framework .serializers .CharField (
196
+ write_only = True ,
197
+ required = False ,
198
+ validators = [django .contrib .auth .password_validation .validate_password ],
199
+ max_length = user .constants .PASSWORD_MAX_LENGTH ,
200
+ min_length = user .constants .PASSWORD_MIN_LENGTH ,
201
+ style = {'input_type' : 'password' },
202
+ )
203
+ avatar_url = rest_framework .serializers .CharField (
204
+ required = False ,
205
+ max_length = user .constants .AVATAR_URL_MAX_LENGTH ,
206
+ validators = [
207
+ django .core .validators .URLValidator (schemes = ['http' , 'https' ]),
208
+ ],
209
+ )
210
+ other = OtherFieldSerializer (required = False )
211
+
212
+ class Meta :
213
+ model = user .models .User
214
+ fields = (
215
+ 'name' ,
216
+ 'surname' ,
217
+ 'email' ,
218
+ 'password' ,
219
+ 'avatar_url' ,
220
+ 'other' ,
221
+ )
222
+
223
+ def update (self , instance , validated_data ):
224
+ password = validated_data .pop ('password' , None )
225
+
226
+ if password :
227
+ # do not invalidate the token
228
+ instance .set_password (password )
229
+
230
+ other_data = validated_data .pop ('other' , None )
231
+ if other_data is not None :
232
+ instance .other = other_data
233
+
234
+ for attr , value in validated_data .items ():
235
+ setattr (instance , attr , value )
236
+
237
+ instance .save ()
238
+ return instance
239
+
240
+ def to_representation (self , instance ):
241
+ data = super ().to_representation (instance )
242
+ # If the response structure implies that a field is optional,
243
+ # the server MUST NOT return the field if it is missing.
244
+ if not instance .avatar_url :
245
+ data .pop ('avatar_url' , None )
246
+ return data
0 commit comments