From 9ee38669ec616f0c071021397a15d42c8d4d8c69 Mon Sep 17 00:00:00 2001 From: Aakanksha Agrawal Date: Tue, 25 May 2021 14:56:59 +0530 Subject: [PATCH 1/5] enhanced code of user.py with dict.get() --- app/api/dao/user.py | 47 ++++++++++----------------------------------- 1 file changed, 10 insertions(+), 37 deletions(-) diff --git a/app/api/dao/user.py b/app/api/dao/user.py index 8c517e1be..9eef49507 100644 --- a/app/api/dao/user.py +++ b/app/api/dao/user.py @@ -247,64 +247,37 @@ def update_user_profile(user_id: int, data: Dict[str, str]): user.name = data["name"] if "bio" in data: - if data["bio"]: - user.bio = data["bio"] - else: - user.bio = None + user.bio = data.get("bio",None); 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) if "organization" in data: - if data["organization"]: - user.organization = data["organization"] - else: - user.organization = None + user.organization = data.get("organization",None) 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"] From 91d35dc1685ce0d7f2c082c75825c2610e2f940a Mon Sep 17 00:00:00 2001 From: Aakanksha Agrawal Date: Wed, 26 May 2021 01:56:39 +0530 Subject: [PATCH 2/5] reformatted user.py with black --- app/api/dao/user.py | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/app/api/dao/user.py b/app/api/dao/user.py index 9eef49507..d25323a29 100644 --- a/app/api/dao/user.py +++ b/app/api/dao/user.py @@ -247,37 +247,37 @@ def update_user_profile(user_id: int, data: Dict[str, str]): user.name = data["name"] if "bio" in data: - user.bio = data.get("bio",None); + user.bio = data.get("bio", None) if "location" in data: - user.location = data.get("location",None) + user.location = data.get("location", None) if "occupation" in data: - user.occupation = data.get("occupation",None) + user.occupation = data.get("occupation", None) if "organization" in data: - user.organization = data.get("organization",None) + user.organization = data.get("organization", None) if "slack_username" in data: - user.slack_username = data.get("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.get("social_media_links",None) + user.social_media_links = data.get("social_media_links", None) if "skills" in data: if data["skills"]: - user.skills = data.get("skills",None) + user.skills = data.get("skills", None) if "interests" in data: - user.interests = data.get("interests",None) + user.interests = data.get("interests", None) if "resume_url" in data: - user.resume_url = data.get("resume_url",None) + user.resume_url = data.get("resume_url", None) if "photo_url" in data: if data["photo_url"]: - user.photo_url = data.get("photo_url",None) + user.photo_url = data.get("photo_url", None) if "need_mentoring" in data: user.need_mentoring = data["need_mentoring"] From b97edb5793fc7399e2058f46cb0b89d8cd391090 Mon Sep 17 00:00:00 2001 From: Aakanksha Agrawal Date: Thu, 10 Jun 2021 13:43:37 +0530 Subject: [PATCH 3/5] resolved errors due to dict.get() --- app/api/dao/user.py | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/app/api/dao/user.py b/app/api/dao/user.py index d25323a29..cf5349706 100644 --- a/app/api/dao/user.py +++ b/app/api/dao/user.py @@ -44,11 +44,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) + 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: @@ -66,10 +66,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() @@ -244,7 +244,7 @@ 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"] + user.name = data.get("name", None) or None if "bio" in data: user.bio = data.get("bio", None) @@ -253,10 +253,10 @@ def update_user_profile(user_id: int, data: Dict[str, str]): user.location = data.get("location", None) if "occupation" in data: - user.occupation = data.get("occupation", None) + user.occupation = data.get("occupation", None) or None if "organization" in data: - user.organization = data.get("organization", None) + user.organization = data.get("organization", None) or None if "slack_username" in data: user.slack_username = data.get("slack_username", None) @@ -280,10 +280,10 @@ def update_user_profile(user_id: int, data: Dict[str, str]): 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() @@ -305,8 +305,8 @@ def change_password(user_id: int, data: Dict[str, str]): """ - current_password = data["current_password"] - new_password = data["new_password"] + current_password = data.get("current_password", None) + new_password = data.get("new_password", None) user = UserModel.find_by_id(user_id) if user.check_password(current_password): From e7f07fe8b21e0c44415a3b76ac4f415ccbc25b9b Mon Sep 17 00:00:00 2001 From: Aakanksha Agrawal Date: Thu, 10 Jun 2021 13:49:02 +0530 Subject: [PATCH 4/5] reformatted with black --- app/database/models/user.py | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/app/database/models/user.py b/app/database/models/user.py index 943ef0d21..075b7eb07 100644 --- a/app/database/models/user.py +++ b/app/database/models/user.py @@ -58,7 +58,7 @@ class UserModel(db.Model): available_to_mentor = db.Column(db.Boolean) def __init__(self, name, username, password, email, terms_and_conditions_checked): - """Initialises userModel class with name, username, password, email, and terms_and_conditions_checked. """ + """Initialises userModel class with name, username, password, email, and terms_and_conditions_checked.""" ## required fields self.name = name @@ -109,49 +109,49 @@ def json(self): } def __repr__(self): - """Returns the user's name and username. """ + """Returns the user's name and username.""" return f"User name {self.name} . Username is {self.username} ." @classmethod def find_by_username(cls, username: str) -> "UserModel": - """Returns the user that has the username we searched for. """ + """Returns the user that has the username we searched for.""" return cls.query.filter_by(username=username).first() @classmethod def find_by_email(cls, email: str) -> "UserModel": - """Returns the user that has the email we searched for. """ + """Returns the user that has the email we searched for.""" return cls.query.filter_by(email=email).first() @classmethod def find_by_id(cls, _id: int) -> "UserModel": - """Returns the user that has the id we searched for. """ + """Returns the user that has the id we searched for.""" return cls.query.filter_by(id=_id).first() @classmethod def get_all_admins(cls, is_admin=True): - """Returns all the admins. """ + """Returns all the admins.""" return cls.query.filter_by(is_admin=is_admin).all() @classmethod def is_empty(cls) -> bool: - """Returns a boolean if the Usermodel is empty or not. """ + """Returns a boolean if the Usermodel is empty or not.""" return cls.query.first() is None def set_password(self, password_plain_text: str) -> None: - """Sets user password when they create an account or when they are changing their password. """ + """Sets user password when they create an account or when they are changing their password.""" self.password_hash = generate_password_hash(password_plain_text) # checks if password is the same, using its hash def check_password(self, password_plain_text: str) -> bool: - """Returns a boolean if password is the same as it hash or not. """ + """Returns a boolean if password is the same as it hash or not.""" return check_password_hash(self.password_hash, password_plain_text) def save_to_db(self) -> None: - """Adds a user to the database. """ + """Adds a user to the database.""" db.session.add(self) db.session.commit() def delete_from_db(self) -> None: - """Deletes a user from the database. """ + """Deletes a user from the database.""" db.session.delete(self) db.session.commit() From 2f8713a832a46df00fbdfe1737273e93e611b260 Mon Sep 17 00:00:00 2001 From: Aakanksha Agrawal Date: Sat, 19 Jun 2021 15:26:50 +0530 Subject: [PATCH 5/5] made chamges in users.py to increase coverage --- app/api/dao/user.py | 46 +++++++++++++++------------------------------ 1 file changed, 15 insertions(+), 31 deletions(-) diff --git a/app/api/dao/user.py b/app/api/dao/user.py index cf5349706..d744ebba4 100644 --- a/app/api/dao/user.py +++ b/app/api/dao/user.py @@ -243,47 +243,31 @@ def update_user_profile(user_id: int, data: Dict[str, str]): user.username = username - if "name" in data and data["name"]: - user.name = data.get("name", None) or None + user.name = data.get("name", None) - if "bio" in data: - user.bio = data.get("bio", None) + user.bio = data.get("bio", None) - if "location" in data: - user.location = data.get("location", None) + user.location = data.get("location", None) - if "occupation" in data: - user.occupation = data.get("occupation", None) or None + user.occupation = data.get("occupation", None) or None - if "organization" in data: - user.organization = data.get("organization", None) or None + user.organization = data.get("organization", None) or None - if "slack_username" in data: - user.slack_username = data.get("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.get("social_media_links", None) + user.social_media_links = data.get("social_media_links", None) - if "skills" in data: - if data["skills"]: - user.skills = data.get("skills", None) + user.skills = data.get("skills", None) - if "interests" in data: - user.interests = data.get("interests", None) + user.interests = data.get("interests", None) - if "resume_url" in data: - user.resume_url = data.get("resume_url", None) + user.resume_url = data.get("resume_url", None) - if "photo_url" in data: - if data["photo_url"]: - user.photo_url = data.get("photo_url", None) + user.photo_url = data.get("photo_url", None) - if "need_mentoring" in data: - user.need_mentoring = data.get("need_mentoring", None) + user.need_mentoring = data.get("need_mentoring", None) - if "available_to_mentor" in data: - user.available_to_mentor = data.get("available_to_mentor", None) + user.available_to_mentor = data.get("available_to_mentor", None) user.save_to_db() @@ -305,8 +289,8 @@ def change_password(user_id: int, data: Dict[str, str]): """ - current_password = data.get("current_password", None) - new_password = data.get("new_password", None) + current_password = data["current_password"] + new_password = data["new_password"] user = UserModel.find_by_id(user_id) if user.check_password(current_password):