Skip to content

Commit

Permalink
style: format strings using f-strings in app/api/resources/user.py (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
KushalBeniwal authored Apr 29, 2021
1 parent d9dcb53 commit d42f1f6
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 76 deletions.
104 changes: 39 additions & 65 deletions app/api/resources/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,9 @@
@users_ns.route("users")
@users_ns.response(
HTTPStatus.UNAUTHORIZED.value,
"%s\n%s\n%s"
% (
messages.TOKEN_HAS_EXPIRED,
messages.TOKEN_IS_INVALID,
messages.AUTHORISATION_TOKEN_IS_MISSING,
),
f"{messages.TOKEN_HAS_EXPIRED}\n"
f"{messages.TOKEN_IS_INVALID}\n"
f"{messages.AUTHORISATION_TOKEN_IS_MISSING}",
)
# TODO: @users_ns.response(404, 'User does not exist.')
class UserList(Resource):
Expand Down Expand Up @@ -95,14 +92,11 @@ class OtherUser(Resource):
)
@users_ns.response(
HTTPStatus.UNAUTHORIZED.value,
"%s\n%s\n%s"
% (
messages.TOKEN_HAS_EXPIRED,
messages.TOKEN_IS_INVALID,
messages.AUTHORISATION_TOKEN_IS_MISSING,
),
f"{messages.TOKEN_HAS_EXPIRED}\n"
f"{messages.TOKEN_IS_INVALID}\n"
f"{messages.AUTHORISATION_TOKEN_IS_MISSING}",
)
@users_ns.response(HTTPStatus.NOT_FOUND.value, "%s" % messages.USER_DOES_NOT_EXIST)
@users_ns.response(HTTPStatus.NOT_FOUND.value, f"{messages.USER_DOES_NOT_EXIST}")
def get(cls, user_id):
"""
Returns a user.
Expand All @@ -120,14 +114,11 @@ def get(cls, user_id):
@users_ns.route("user")
@users_ns.response(
HTTPStatus.UNAUTHORIZED.value,
"%s\n%s\n%s"
% (
messages.TOKEN_HAS_EXPIRED,
messages.TOKEN_IS_INVALID,
messages.AUTHORISATION_TOKEN_IS_MISSING,
),
f"{messages.TOKEN_HAS_EXPIRED}\n"
f"{messages.TOKEN_IS_INVALID}\n"
f"{messages.AUTHORISATION_TOKEN_IS_MISSING}",
)
@users_ns.response(HTTPStatus.NOT_FOUND.value, "%s" % messages.USER_DOES_NOT_EXIST)
@users_ns.response(HTTPStatus.NOT_FOUND.value, f"{messages.USER_DOES_NOT_EXIST}")
class MyUserProfile(Resource):
@classmethod
@jwt_required
Expand All @@ -150,7 +141,7 @@ def get(cls):
@jwt_required
@users_ns.doc("update_user_profile")
@users_ns.expect(auth_header_parser, update_user_request_body_model)
@users_ns.response(HTTPStatus.OK.value, "%s" % messages.USER_SUCCESSFULLY_UPDATED)
@users_ns.response(HTTPStatus.OK.value, f"{messages.USER_SUCCESSFULLY_UPDATED}")
@users_ns.response(
HTTPStatus.BAD_REQUEST.value,
f"{messages.NO_DATA_FOR_UPDATING_PROFILE_WAS_SENT}\n"
Expand Down Expand Up @@ -186,7 +177,7 @@ def put(cls):
@jwt_required
@users_ns.doc("delete_user")
@users_ns.expect(auth_header_parser, validate=True)
@users_ns.response(HTTPStatus.OK.value, "%s" % messages.USER_SUCCESSFULLY_DELETED)
@users_ns.response(HTTPStatus.OK.value, f"{messages.USER_SUCCESSFULLY_DELETED}")
def delete(cls):
"""
Deletes user.
Expand All @@ -200,20 +191,18 @@ def delete(cls):


@users_ns.response(
HTTPStatus.CREATED.value, "%s" % messages.PASSWORD_SUCCESSFULLY_UPDATED
HTTPStatus.CREATED.value, f"{messages.PASSWORD_SUCCESSFULLY_UPDATED}"
)
@users_ns.response(
HTTPStatus.BAD_REQUEST.value,
f"{messages.USER_ENTERED_INCORRECT_PASSWORD}\n{messages.USER_INPUTS_SPACE_IN_PASSWORD}",
f"{messages.USER_ENTERED_INCORRECT_PASSWORD}\n"
f"{messages.USER_INPUTS_SPACE_IN_PASSWORD}",
)
@users_ns.response(
HTTPStatus.UNAUTHORIZED.value,
"%s\n%s\n%s"
% (
messages.TOKEN_HAS_EXPIRED,
messages.TOKEN_IS_INVALID,
messages.AUTHORISATION_TOKEN_IS_MISSING,
),
f"{messages.TOKEN_HAS_EXPIRED}\n"
f"{messages.TOKEN_IS_INVALID}\n"
f"{messages.AUTHORISATION_TOKEN_IS_MISSING}",
)
@users_ns.route("user/change_password")
class ChangeUserPassword(Resource):
Expand Down Expand Up @@ -241,12 +230,9 @@ def put(cls):

@users_ns.response(
HTTPStatus.UNAUTHORIZED.value,
"%s\n%s\n%s"
% (
messages.TOKEN_HAS_EXPIRED,
messages.TOKEN_IS_INVALID,
messages.AUTHORISATION_TOKEN_IS_MISSING,
),
f"{messages.TOKEN_HAS_EXPIRED}\n"
f"{messages.TOKEN_IS_INVALID}\n"
f"{messages.AUTHORISATION_TOKEN_IS_MISSING}",
)
@users_ns.route("users/verified")
class VerifiedUser(Resource):
Expand Down Expand Up @@ -303,7 +289,7 @@ class UserRegister(Resource):
@classmethod
@users_ns.doc("create_user")
@users_ns.response(
HTTPStatus.CREATED.value, "%s" % messages.USER_WAS_CREATED_SUCCESSFULLY
HTTPStatus.CREATED.value, f"{messages.USER_WAS_CREATED_SUCCESSFULLY}"
)
@users_ns.response(
HTTPStatus.BAD_REQUEST.value,
Expand All @@ -323,11 +309,8 @@ class UserRegister(Resource):
)
@users_ns.response(
HTTPStatus.CONFLICT,
"%s\n%s"
% (
messages.USER_USES_A_USERNAME_THAT_ALREADY_EXISTS,
messages.USER_USES_AN_EMAIL_ID_THAT_ALREADY_EXISTS,
),
f"{messages.USER_USES_A_USERNAME_THAT_ALREADY_EXISTS}\n"
f"{messages.USER_USES_AN_EMAIL_ID_THAT_ALREADY_EXISTS}",
)
@users_ns.expect(register_user_api_model, validate=True)
def post(cls):
Expand Down Expand Up @@ -358,14 +341,11 @@ def post(cls):
@users_ns.route("user/confirm_email/<string:token>")
@users_ns.response(
HTTPStatus.CREATED.value,
"%s\n%s"
% (
messages.USER_SUCCESSFULLY_CREATED,
messages.ACCOUNT_ALREADY_CONFIRMED_AND_THANKS,
),
f"{messages.USER_SUCCESSFULLY_CREATED}\n"
f"{messages.ACCOUNT_ALREADY_CONFIRMED_AND_THANKS}",
)
@users_ns.response(
HTTPStatus.CONFLICT.value, "%s" % messages.EMAIL_EXPIRED_OR_TOKEN_IS_INVALID
HTTPStatus.CONFLICT.value, f"{messages.EMAIL_EXPIRED_OR_TOKEN_IS_INVALID}"
)
@users_ns.param("token", "Token sent to the user's email")
class UserEmailConfirmation(Resource):
Expand Down Expand Up @@ -433,12 +413,9 @@ class RefreshUser(Resource):
)
@users_ns.response(
HTTPStatus.UNAUTHORIZED.value,
"%s\n%s\n%s"
% (
messages.TOKEN_HAS_EXPIRED,
messages.TOKEN_IS_INVALID,
messages.AUTHORISATION_TOKEN_IS_MISSING,
),
f"{messages.TOKEN_HAS_EXPIRED}\n"
f"{messages.TOKEN_IS_INVALID}\n"
f"{messages.AUTHORISATION_TOKEN_IS_MISSING}",
)
@users_ns.expect(refresh_auth_header_parser)
def post(cls):
Expand All @@ -465,15 +442,15 @@ class LoginUser(Resource):
)
@users_ns.response(
HTTPStatus.BAD_REQUEST.value,
"%s\n%s"
% (messages.USERNAME_FIELD_IS_MISSING, messages.PASSWORD_FIELD_IS_MISSING),
f"{messages.USERNAME_FIELD_IS_MISSING}\n"
f"{messages.PASSWORD_FIELD_IS_MISSING}",
)
@users_ns.response(
HTTPStatus.FORBIDDEN.value,
"%s" % messages.USER_HAS_NOT_VERIFIED_EMAIL_BEFORE_LOGIN,
f"{messages.USER_HAS_NOT_VERIFIED_EMAIL_BEFORE_LOGIN}",
)
@users_ns.response(
HTTPStatus.UNAUTHORIZED.value, "%s" % messages.WRONG_USERNAME_OR_PASSWORD
HTTPStatus.UNAUTHORIZED.value, f"{messages.WRONG_USERNAME_OR_PASSWORD}"
)
@users_ns.expect(login_request_body_model)
def post(cls):
Expand Down Expand Up @@ -527,14 +504,11 @@ def post(cls):
)
@users_ns.response(
HTTPStatus.UNAUTHORIZED.value,
"%s\n%s\n%s"
% (
messages.TOKEN_HAS_EXPIRED,
messages.TOKEN_IS_INVALID,
messages.AUTHORISATION_TOKEN_IS_MISSING,
),
f"{messages.TOKEN_HAS_EXPIRED}\n"
f"{messages.TOKEN_IS_INVALID}\n"
f"{messages.AUTHORISATION_TOKEN_IS_MISSING}",
)
@users_ns.response(HTTPStatus.NOT_FOUND.value, "%s" % messages.USER_NOT_FOUND)
@users_ns.response(HTTPStatus.NOT_FOUND.value, f"{messages.USER_NOT_FOUND}")
class UserHomeStatistics(Resource):
@classmethod
@jwt_required
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()

0 comments on commit d42f1f6

Please sign in to comment.