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

enhanced code of user.py with dict.get() #1116

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
71 changes: 22 additions & 49 deletions app/api/dao/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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()

Expand Down Expand Up @@ -244,73 +244,46 @@ def update_user_profile(user_id: int, data: Dict[str, str]):
user.username = username

if "name" in data and 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.

We don't need these if conditions too

user.name = data["name"]
user.name = data.get("name", None) or None

if "bio" in data:
Copy link
Member

@devkapilbansal devkapilbansal Jun 18, 2021

Choose a reason for hiding this comment

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

Same as above similar for other cases as well

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) 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

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 All @@ -332,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)
Copy link
Member

Choose a reason for hiding this comment

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

None can be removed here as it is the default value returned

Suggested change
current_password = data.get("current_password", None)
new_password = data.get("new_password", None)
current_password = data.get("current_password")
new_password = data.get("new_password")


user = UserModel.find_by_id(user_id)
if user.check_password(current_password):
Expand Down
22 changes: 11 additions & 11 deletions app/database/models/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()