Skip to content

Commit

Permalink
added type hints to app/api/dao
Browse files Browse the repository at this point in the history
  • Loading branch information
bartekpacia committed Jan 17, 2020
1 parent 015a220 commit 801749e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 27 deletions.
6 changes: 4 additions & 2 deletions app/api/dao/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Dict

from app import messages
from app.database.models.user import UserModel
from app.utils.decorator_utils import email_verification_required
Expand All @@ -7,7 +9,7 @@ class AdminDAO:

@staticmethod
@email_verification_required
def assign_new_user(user_id, data):
def assign_new_user(user_id: int, data: Dict[str, str]):
"""Creates a new admin.
Creates a new admin if the assigned user exists and is assigned by another user. Otherwise returns a message.
Expand Down Expand Up @@ -49,7 +51,7 @@ def assign_new_user(user_id, data):

@staticmethod
@email_verification_required
def revoke_admin_user(user_id, data):
def revoke_admin_user(user_id: int, data: Dict[str, str]):
"""Revokes the admin status of an user.
Revokes the admin status of an user if the user exists, is an admin and another user requests for this action. Otherwise returns a message.
Expand Down
19 changes: 10 additions & 9 deletions app/api/dao/mentorship_relation.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime, timedelta
from typing import Dict

from app import messages
from app.database.models.mentorship_relation import MentorshipRelationModel
Expand All @@ -20,7 +21,7 @@ class MentorshipRelationDAO:
MAXIMUM_MENTORSHIP_DURATION = timedelta(weeks=24) # 6 months = approximately 6*4
MINIMUM_MENTORSHIP_DURATION = timedelta(weeks=4)

def create_mentorship_relation(self, user_id, data):
def create_mentorship_relation(self, user_id: int, data: Dict[str, str]):
"""Creates a relationship between two users.
Establishes the mentor-mentee relationship.
Expand Down Expand Up @@ -113,7 +114,7 @@ def create_mentorship_relation(self, user_id, data):

@staticmethod
@email_verification_required
def list_mentorship_relations(user_id=None, accepted=None, pending=None, completed=None, cancelled=None, rejected=None):
def list_mentorship_relations(user_id: int=None, accepted: bool=None, pending: bool=None, completed: bool=None, cancelled: bool=None, rejected: bool=None):
"""Lists all relationships of a given user.
Lists all relationships of a given user. Support for filtering not yet implemented.
Expand Down Expand Up @@ -146,7 +147,7 @@ def list_mentorship_relations(user_id=None, accepted=None, pending=None, complet

@staticmethod
@email_verification_required
def accept_request(user_id, request_id):
def accept_request(user_id: int, request_id: int):
"""Allows a mentorship request.
Args:
Expand Down Expand Up @@ -191,7 +192,7 @@ def accept_request(user_id, request_id):

@staticmethod
@email_verification_required
def reject_request(user_id, request_id):
def reject_request(user_id: int, request_id: int):
"""Rejects a mentorship request.
Args:
Expand Down Expand Up @@ -229,7 +230,7 @@ def reject_request(user_id, request_id):

@staticmethod
@email_verification_required
def cancel_relation(user_id, relation_id):
def cancel_relation(user_id: int, relation_id: int):
"""Allows a given user to terminate a particular relationship.
Args:
Expand Down Expand Up @@ -263,7 +264,7 @@ def cancel_relation(user_id, relation_id):

@staticmethod
@email_verification_required
def delete_request(user_id, request_id):
def delete_request(user_id: int, request_id: int):
"""Deletes a mentorship request.
Deletes a mentorship request if the current user was the one who created it and the request is in the pending state.
Expand Down Expand Up @@ -298,7 +299,7 @@ def delete_request(user_id, request_id):

@staticmethod
@email_verification_required
def list_past_mentorship_relations(user_id):
def list_past_mentorship_relations(user_id: int):
"""Lists past mentorship relation details.
Args:
Expand All @@ -322,7 +323,7 @@ def list_past_mentorship_relations(user_id):

@staticmethod
@email_verification_required
def list_current_mentorship_relation(user_id):
def list_current_mentorship_relation(user_id: int):
"""Lists current mentorship relation details.
Args:
Expand All @@ -344,7 +345,7 @@ def list_current_mentorship_relation(user_id):

@staticmethod
@email_verification_required
def list_pending_mentorship_relations(user_id):
def list_pending_mentorship_relations(user_id: int):
"""Lists mentorship requests in pending state.
Args:
Expand Down
9 changes: 5 additions & 4 deletions app/api/dao/task.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from datetime import datetime
from typing import Dict

from app import messages
from app.database.models.mentorship_relation import MentorshipRelationModel
Expand All @@ -12,7 +13,7 @@ class TaskDAO:

@staticmethod
@email_verification_required
def create_task(user_id, mentorship_relation_id, data):
def create_task(user_id: int, mentorship_relation_id: int, data: Dict[str, str]):
"""Creates a new task.
Creates a new task in a mentorship relation if the specified user is already involved in it.
Expand Down Expand Up @@ -46,7 +47,7 @@ def create_task(user_id, mentorship_relation_id, data):

@staticmethod
@email_verification_required
def list_tasks(user_id, mentorship_relation_id):
def list_tasks(user_id: int, mentorship_relation_id: int):
"""Retrieves all tasks of a user in a mentorship relation.
Lists all tasks from a mentorship relation for the specified user if the user is involved in a current mentorship relation.
Expand Down Expand Up @@ -75,7 +76,7 @@ def list_tasks(user_id, mentorship_relation_id):

@staticmethod
@email_verification_required
def delete_task(user_id, mentorship_relation_id, task_id):
def delete_task(user_id: int, mentorship_relation_id: int, task_id: int):
"""Deletes a specified task from a mentorship relation.
Deletes a task that belongs to a user who is involved in the specified
Expand Down Expand Up @@ -109,7 +110,7 @@ def delete_task(user_id, mentorship_relation_id, task_id):

@staticmethod
@email_verification_required
def complete_task(user_id, mentorship_relation_id, task_id):
def complete_task(user_id: int, mentorship_relation_id: int, task_id: int):
"""Marks a task as completed.
Updates the task that belongs to a user who is involved in the specified
Expand Down
25 changes: 13 additions & 12 deletions app/api/dao/user.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from datetime import datetime
from operator import itemgetter
from typing import Dict

from app import messages
from app.api.email_utils import confirm_token
Expand All @@ -17,7 +18,7 @@ class UserDAO:
MIN_NUMBER_OF_ADMINS = 1

@staticmethod
def create_user(data):
def create_user(data: Dict[str, str]):
"""Creates a new user.
Creates a new user with provided data.
Expand Down Expand Up @@ -56,7 +57,7 @@ def create_user(data):

@staticmethod
@email_verification_required
def delete_user(user_id):
def delete_user(user_id: int):
""" Deletes a user.
Deletes the specified user and removes them from the directory, with checks to make sure that the user exists and is not the only administrator.
Expand Down Expand Up @@ -85,7 +86,7 @@ def delete_user(user_id):

@staticmethod
@email_verification_required
def get_user(user_id):
def get_user(user_id: int):
""" Retrieves a user's profile information using a specified ID.
Provides the user profile of the user whose ID matches the one specified.
Expand All @@ -101,7 +102,7 @@ def get_user(user_id):
return UserModel.find_by_id(user_id)

@staticmethod
def get_user_by_email(email):
def get_user_by_email(email: str):
""" Retrieves a user's profile information using a specified email.
Provides the user profile of the user whose email matches the one specified.
Expand All @@ -117,7 +118,7 @@ def get_user_by_email(email):
return UserModel.find_by_email(email)

@staticmethod
def get_user_by_username(username):
def get_user_by_username(username: str):
""" Retrieves a user's profile information using a specified username.
Provides the user profile of the user whose username matches the one specified.
Expand All @@ -133,7 +134,7 @@ def get_user_by_username(username):
return UserModel.find_by_username(username)

@staticmethod
def list_users(user_id, is_verified=None):
def list_users(user_id: int, is_verified: str = None):
""" Retrieves a list of verified users with the specified ID.
Arguments:
Expand All @@ -158,7 +159,7 @@ def list_users(user_id, is_verified=None):

@staticmethod
@email_verification_required
def update_user_profile(user_id, data):
def update_user_profile(user_id: int, data: Dict[str, str]):
""" Updates the profile of a specified user with new data.
Replaces old data items with new ones in the provided data list, with a check for overlap between users in username and a check that a user with the specified ID exists
Expand Down Expand Up @@ -261,7 +262,7 @@ def update_user_profile(user_id, data):

@staticmethod
@email_verification_required
def change_password(user_id, data):
def change_password(user_id: int, data: Dict[str, str]):
""" Changes the user's password.
Finds the user with the given ID, checks their current password, and then updates to the new one.
Expand All @@ -287,7 +288,7 @@ def change_password(user_id, data):
return messages.USER_ENTERED_INCORRECT_PASSWORD, 400

@staticmethod
def confirm_registration(token):
def confirm_registration(token: str):
""" Determines whether a user's email registration has been confirmed.
Determines whether a user's email registration was invalid, previously confirmed, or just confirmed.
Expand Down Expand Up @@ -315,7 +316,7 @@ def confirm_registration(token):
return messages.ACCOUNT_ALREADY_CONFIRMED_AND_THANKS, 200

@staticmethod
def authenticate(username_or_email, password):
def authenticate(username_or_email: str, password: str):
""" User login process.
The user can login with two options:
Expand All @@ -342,7 +343,7 @@ def authenticate(username_or_email, password):

@staticmethod
@email_verification_required
def get_achievements(user_id):
def get_achievements(user_id: int):
"""Shows a subset of the user's achievements
Gets all the completed tasks of the user and
Expand All @@ -364,7 +365,7 @@ def get_achievements(user_id):
return achievements

@staticmethod
def get_user_statistics(user_id):
def get_user_statistics(user_id: int):
"""Shows some basic user statistics
Gets the following statistics of the user:
Expand Down

0 comments on commit 801749e

Please sign in to comment.