Skip to content

Commit

Permalink
Fix:updating an user with empty username and name gives an error message
Browse files Browse the repository at this point in the history
  • Loading branch information
rashikaqureshi committed Jul 31, 2020
1 parent 99820ef commit 767d7de
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
16 changes: 13 additions & 3 deletions app/api/dao/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ def update_user_profile(user_id: int, data: Dict[str, str]):
return messages.USER_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

username = data.get("username", None)

if username:
user_with_same_username = UserModel.find_by_username(username)

Expand All @@ -223,8 +224,17 @@ def update_user_profile(user_id: int, data: Dict[str, str]):

user.username = username

if "name" in data and data["name"]:
user.name = data["name"]
if "name" in data:
if data["name"]:
user.name = data["name"]
else:
return messages.NAME_FIELD_IS_MISSING,HTTPStatus.OK

if "username" in data:
if data["username"]:
user.username = data["username"]
else:
return messages.USERNAME_FIELD_IS_MISSING,HTTPStatus.OK

if "bio" in data:
if data["bio"]:
Expand Down Expand Up @@ -291,7 +301,7 @@ def update_user_profile(user_id: int, data: Dict[str, str]):

if "available_to_mentor" in data:
user.available_to_mentor = data["available_to_mentor"]

user.save_to_db()

return messages.USER_SUCCESSFULLY_UPDATED, HTTPStatus.OK
Expand Down
3 changes: 2 additions & 1 deletion app/api/validations/user.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from app import messages
from http import HTTPStatus
from app.utils.validation_utils import (
is_name_valid,
is_email_valid,
Expand Down Expand Up @@ -213,7 +214,7 @@ def validate_update_profile_request_data(data):

if "available_to_mentor" in data and data["available_to_mentor"] is None:
return messages.FIELD_AVAILABLE_TO_MENTOR_IS_INVALID

return {}


Expand Down
1 change: 1 addition & 0 deletions app/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
DESCRIPTION_FIELD_IS_MISSING = {"message": "Description field is missing."}
COMMENT_FIELD_IS_MISSING = {"message": "Comment field is missing."}
USERNAME_FIELD_IS_EMPTY = {"message": f"The username field has to be longer than {USERNAME_MIN_LENGTH} characters and shorter than {USERNAME_MAX_LENGTH} characters."}
USER_AND_USERNAME_FIELD_IS_MISSING ={"message":"The name and username fields are missing."}

# Admin
USER_IS_ALREADY_AN_ADMIN = {"message": "User is already an Admin."}
Expand Down

0 comments on commit 767d7de

Please sign in to comment.