Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved code of user.py with dict.get() and suggestions #1124

Open
wants to merge 9 commits into
base: develop
Choose a base branch
from
84 changes: 20 additions & 64 deletions app/api/dao/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,11 @@ def create_user(data: Dict[str, str]):
A tuple with two elements. The first element is a dictionary containing a key 'message' containing a string which indicates whether or not the user was created successfully. The second is the HTTP response code.
"""

name = data["name"]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also please use a different branch for each pr. You've used develop currently, this may cause issues in the future.

username = data["username"]
password = data["password"]
email = data["email"]
terms_and_conditions_checked = data["terms_and_conditions_checked"]
name = data.get("name", None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None is default right? I don't think you need this here

username = data.get("username", None)
password = data.get("password", None)
email = data.get("email", None)
terms_and_conditions_checked = data.get("terms_and_conditions_checked", None)

existing_user = UserModel.find_by_username(data["username"])
if existing_user:
Expand All @@ -61,10 +61,10 @@ def create_user(data: Dict[str, str]):

user = UserModel(name, username, password, email, terms_and_conditions_checked)
if "need_mentoring" in data:
user.need_mentoring = data["need_mentoring"]
user.need_mentoring = data.get("need_mentoring", None)

if "available_to_mentor" in data:
user.available_to_mentor = data["available_to_mentor"]
user.available_to_mentor = data.get("available_to_mentor", None)

user.save_to_db()

Expand Down Expand Up @@ -235,77 +235,33 @@ def update_user_profile(user_id: int, data: Dict[str, str]):
messages.USER_USES_A_USERNAME_THAT_ALREADY_EXISTS,
HTTPStatus.BAD_REQUEST,
)

user.username = username

if "name" in data and data["name"]:
user.name = data["name"]
user.name = data.get("name", None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will raise a key error if name entered is not found that's why none is required here

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use data["name"], then KeyError will be raised, not with data.get

Suggested change
user.name = data.get("name", None)
user.name = data.get("name")


if "bio" in data:
if data["bio"]:
user.bio = data["bio"]
else:
user.bio = None
user.bio = data.get("bio", None)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above


if "location" in data:
if data["location"]:
user.location = data["location"]
else:
user.location = None
user.location = data.get("location", None)

if "occupation" in data:
if data["occupation"]:
user.occupation = data["occupation"]
else:
user.occupation = None
user.occupation = data.get("occupation", None) or None

if "organization" in data:
if data["organization"]:
user.organization = data["organization"]
else:
user.organization = None
user.organization = data.get("organization", None) or None
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above, same for other cases as well

Suggested change
user.organization = data.get("organization", None) or None
user.organization = data.get("organization")

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I have told earlier, on removing none in organization and occupation will fail test cases.


if "slack_username" in data:
if data["slack_username"]:
user.slack_username = data["slack_username"]
else:
user.slack_username = None
user.slack_username = data.get("slack_username", None)

if "social_media_links" in data:
if data["social_media_links"]:
user.social_media_links = data["social_media_links"]
else:
user.social_media_links = None
user.social_media_links = data.get("social_media_links", None)

if "skills" in data:
if data["skills"]:
user.skills = data["skills"]
else:
user.skills = None
user.skills = data.get("skills", None)

if "interests" in data:
if data["interests"]:
user.interests = data["interests"]
else:
user.interests = None
user.interests = data.get("interests", None)

if "resume_url" in data:
if data["resume_url"]:
user.resume_url = data["resume_url"]
else:
user.resume_url = None
user.resume_url = data.get("resume_url", None)

if "photo_url" in data:
if data["photo_url"]:
user.photo_url = data["photo_url"]
else:
user.photo_url = None
user.photo_url = data.get("photo_url", None)

if "need_mentoring" in data:
user.need_mentoring = data["need_mentoring"]
user.need_mentoring = data.get("need_mentoring", None)

if "available_to_mentor" in data:
user.available_to_mentor = data["available_to_mentor"]
user.available_to_mentor = data.get("available_to_mentor", None)

user.save_to_db()

Expand Down