Skip to content

Commit fc7874e

Browse files
authored
Merge pull request #50 from abbastoof/feature/019-user-profile-management
turned the serializer.validationerror to a list of errors in views.py…
2 parents 69c2f38 + d4f3e77 commit fc7874e

File tree

3 files changed

+18
-19
lines changed

3 files changed

+18
-19
lines changed

Backend/user_service/user_service/user_app/serializers.py

+3-9
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def create(self, validate_data) -> User:
6868
try:
6969
validate_password(validate_data["password"])
7070
except ValidationError as err:
71-
raise serializers.ValidationError({"password": err.messages}) from err
71+
raise serializers.ValidationError(detail=err.messages) from err
7272
password = validate_data.pop("password", None)
7373
instance = self.Meta.model(**validate_data)
7474
if password is not None:
@@ -97,20 +97,14 @@ def update(self, instance, validate_data) -> User:
9797
for attr, value in validate_data.items():
9898
if attr == "password" and value is not None:
9999
if instance.check_password(value):
100-
raise serializers.ValidationError(
101-
{
102-
"password": "New password must be different from the current password."
103-
}
104-
)
100+
raise serializers.ValidationError(detail="New password must be different from the current password.")
105101

106102
# Validate the new password using CustomPasswordValidator
107103
try:
108104
validator = CustomPasswordValidator()
109105
validator.validate(value, user=instance)
110106
except ValidationError as err:
111-
raise serializers.ValidationError(
112-
{"password": err.messages}
113-
) from err
107+
raise serializers.ValidationError(detail=err.messages) from err
114108
instance.set_password(value)
115109
else:
116110
setattr(instance, attr, value)

Backend/user_service/user_service/user_app/validators.py

+6-9
Original file line numberDiff line numberDiff line change
@@ -14,38 +14,35 @@ def validate(self, password, user=None):
1414
errors = []
1515

1616
if len(password) < self.minlength:
17-
errors.append(
17+
raise ValidationError(
1818
_("This password must contain at least {minlength} characters.").format(minlength=self.minlength)
1919
)
2020

2121
if sum(1 for c in password if c.islower()) < self.minlower:
22-
errors.append(
22+
raise ValidationError(
2323
_("This password must contain at least {minlower} lowercase letter(s).").format(minlower=self.minlower)
2424
)
2525

2626
if sum(1 for c in password if c.isupper()) < self.minupper:
27-
errors.append(
27+
raise ValidationError(
2828
_("This password must contain at least {minupper} uppercase letter(s).").format(minupper=self.minupper)
2929
)
3030

3131
if sum(1 for c in password if c.isdigit()) < self.mindigit:
32-
errors.append(
32+
raise ValidationError(
3333
_("This password must contain at least {mindigit} digit(s).").format(mindigit=self.mindigit)
3434
)
3535

3636
if sum(1 for c in password if c in self.special_characters) < self.minspecial:
37-
errors.append(
37+
raise ValidationError(
3838
_("This password must contain at least {minspecial} special character(s) from {specials}.").format(minspecial=self.minspecial, specials=self.special_characters)
3939
)
4040

4141
if user and user.check_password(password):
42-
errors.append(
42+
raise ValidationError(
4343
_("The new password cannot be the same as the old password.")
4444
)
4545

46-
if errors:
47-
raise ValidationError(errors)
48-
4946
def get_help_text(self):
5047
"""
5148
Get the help text for the password validator.

Backend/user_service/user_service/user_app/views.py

+9-1
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,11 @@ def update_user(self, request, pk=None) -> Response:
131131
serializer.save()
132132
# if User updated Username should send message to all microservices to update the username related to this user using Kafka
133133
return Response(serializer.data, status=status.HTTP_202_ACCEPTED)
134+
except ValidationError as err:
135+
item_lists = []
136+
for item in err.detail:
137+
item_lists.append(item)
138+
return Response({'error': item_lists}, status=status.HTTP_400_BAD_REQUEST)
134139
except Exception as err:
135140
return Response({"error": str(err)}, status=status.HTTP_400_BAD_REQUEST)
136141

@@ -194,7 +199,10 @@ def create_user(self, request) -> Response:
194199
data["email"] = ["A user with that email already exists."]
195200
return Response({"error":data}, status=status.HTTP_400_BAD_REQUEST)
196201
except ValidationError as err:
197-
return Response({'error': err}, status=status.HTTP_400_BAD_REQUEST)
202+
item_lists = []
203+
for item in err.detail:
204+
item_lists.append(item)
205+
return Response({'error': item_lists}, status=status.HTTP_400_BAD_REQUEST)
198206

199207
class FriendsViewSet(viewsets.ViewSet):
200208
authentication_classes = [JWTAuthentication]

0 commit comments

Comments
 (0)