Skip to content

Commit

Permalink
chore: add some type hints (#354)
Browse files Browse the repository at this point in the history
* chore: added typings in
* run.py and config.py
* app/database/models
* added type hints to app/api/dao
  • Loading branch information
bartekpacia authored Mar 17, 2020
1 parent fe87045 commit 877d4e2
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 49 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 @@ -8,7 +10,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
17 changes: 9 additions & 8 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 @@ -21,7 +22,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 @@ -161,7 +162,7 @@ def isValidState(rel_state):

@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 @@ -224,7 +225,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 @@ -262,7 +263,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 @@ -296,7 +297,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 @@ -331,7 +332,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 @@ -357,7 +358,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 @@ -379,7 +380,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 flask_restplus import marshal

from app import messages
Expand All @@ -26,7 +27,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 @@ -65,7 +66,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 @@ -94,7 +95,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 @@ -110,7 +111,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 @@ -126,7 +127,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 @@ -142,7 +143,7 @@ def get_user_by_username(username):
return UserModel.find_by_username(username)

@staticmethod
def list_users(user_id, search_query="", is_verified=None):
def list_users(user_id: int, search_query: str = "", is_verified = None):
""" Retrieves a list of verified users with the specified ID.
Arguments:
Expand Down Expand Up @@ -183,7 +184,7 @@ def list_users(user_id, search_query="", 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 @@ -286,7 +287,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 @@ -312,7 +313,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 @@ -340,7 +341,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 @@ -367,7 +368,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 @@ -389,7 +390,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
11 changes: 7 additions & 4 deletions app/database/models/mentorship_relation.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import date

from app.database.models.tasks_list import TasksListModel
from app.database.models.user import UserModel
from app.database.sqlalchemy_extension import db
Expand Down Expand Up @@ -99,24 +101,25 @@ def json(self):
# % (self.id, self.mentor_id, self.mentee_id)

@classmethod
def find_by_id(cls, _id):
def find_by_id(cls, _id) -> 'MentorshipRelationModel':

"""Returns the mentorship that has the passed id.
Args:
_id: The id of a mentorship.
"""
return cls.query.filter_by(id=_id).first()

@classmethod
def is_empty(cls):
def is_empty(cls) -> bool:
"""Returns True if the mentorship model is empty, and False otherwise."""
return cls.query.first() is None

def save_to_db(self):
def save_to_db(self) -> None:
"""Saves the model to the database."""
db.session.add(self)
db.session.commit()

def delete_from_db(self):
def delete_from_db(self) -> None:
"""Deletes the record of mentorship relation from the database."""
self.tasks_list.delete_from_db()
db.session.delete(self)
Expand Down
21 changes: 12 additions & 9 deletions app/database/models/tasks_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from app.database.db_types.JsonCustomType import JsonCustomType
from app.database.sqlalchemy_extension import db
from datetime import date


class TasksListModel(db.Model):
Expand All @@ -20,7 +21,7 @@ class TasksListModel(db.Model):
tasks = db.Column(JsonCustomType)
next_task_id = db.Column(db.Integer)

def __init__(self, tasks=None):
def __init__(self, tasks: 'TasksListModel' = None):
"""Initializes tasks.
Args:
Expand All @@ -40,7 +41,7 @@ def __init__(self, tasks=None):
else:
raise ValueError(TypeError)

def add_task(self, description, created_at, is_done=False, completed_at=None):
def add_task(self, description: str, created_at: date, is_done=False, completed_at=None) -> None:
"""Adds a task to the list of tasks.
Args:
Expand All @@ -60,7 +61,7 @@ def add_task(self, description, created_at, is_done=False, completed_at=None):
self.next_task_id += 1
self.tasks = self.tasks + [task]

def delete_task(self, task_id):
def delete_task(self, task_id: int) -> None:
"""Deletes a task from the list of tasks.
Args:
Expand All @@ -74,10 +75,12 @@ def delete_task(self, task_id):
self.tasks = new_list
self.save_to_db()

def update_task(self, task_id, description=None, is_done=None, completed_at=None):
def update_task(self, task_id: int, description: str = None, is_done: bool = None,
completed_at: date = None) -> None:
"""Updates a task.
Args:
task_id: Id of the task to be updated.
description: A description of the task.
created_at: Date on which the task is created.
is_done: Boolean specifying completion of the task.
Expand Down Expand Up @@ -105,7 +108,7 @@ def update_task(self, task_id, description=None, is_done=None, completed_at=None
self.tasks = new_list
self.save_to_db()

def find_task_by_id(self, task_id):
def find_task_by_id(self, task_id: int):
"""Returns the task that has the specified id.
Args:
Expand All @@ -122,7 +125,7 @@ def find_task_by_id(self, task_id):
else:
return task[0]

def is_empty(self):
def is_empty(self) -> bool:
"""Checks if the list of tasks is empty.
Returns:
Expand Down Expand Up @@ -159,7 +162,7 @@ def __repr__(self):
)

@classmethod
def find_by_id(cls, _id):
def find_by_id(cls, _id: int):
"""Finds a task with the specified id.
Returns:
Expand All @@ -168,12 +171,12 @@ def find_by_id(cls, _id):

return cls.query.filter_by(id=_id).first()

def save_to_db(self):
def save_to_db(self) -> None:
"""Adds a task to the database."""
db.session.add(self)
db.session.commit()

def delete_from_db(self):
def delete_from_db(self) -> None:
"""Deletes a task from the database."""
db.session.delete(self)
db.session.commit()
Expand Down
Loading

0 comments on commit 877d4e2

Please sign in to comment.