-
Notifications
You must be signed in to change notification settings - Fork 448
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
base: develop
Are you sure you want to change the base?
Changes from 2 commits
4091169
ec66d78
29bf220
747d858
e706c05
66550c9
02b982e
b5e5781
6f6df83
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -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"] | ||||||
username = data["username"] | ||||||
password = data["password"] | ||||||
email = data["email"] | ||||||
terms_and_conditions_checked = data["terms_and_conditions_checked"] | ||||||
name = data.get("name", None) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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: | ||||||
|
@@ -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() | ||||||
|
||||||
|
@@ -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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
|
||||||
|
||||||
if "bio" in data: | ||||||
if data["bio"]: | ||||||
user.bio = data["bio"] | ||||||
else: | ||||||
user.bio = None | ||||||
user.bio = data.get("bio", None) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above, same for other cases as well
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||||||
|
||||||
|
There was a problem hiding this comment.
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.