Skip to content

Commit

Permalink
style: format code using black tool (anitab-org#478)
Browse files Browse the repository at this point in the history
  • Loading branch information
isabelcosta authored Mar 4, 2020
1 parent 1aab015 commit 80114ac
Show file tree
Hide file tree
Showing 87 changed files with 4,322 additions and 2,920 deletions.
16 changes: 9 additions & 7 deletions app/api/api_extension.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
from flask_restplus import Api

api = Api(
title='Mentorship System API',
version='1.0',
description='API documentation for the backend of Mentorship System'
title="Mentorship System API",
version="1.0",
description="API documentation for the backend of Mentorship System"
# doc='/docs/'
)

# Adding namespaces
from app.api.resources.user import users_ns as user_namespace

api.add_namespace(user_namespace, path='/')
api.add_namespace(user_namespace, path="/")

from app.api.resources.admin import admin_ns as admin_namespace

api.add_namespace(admin_namespace, path='/')
api.add_namespace(admin_namespace, path="/")

from app.api.resources.mentorship_relation import mentorship_relation_ns as mentorship_namespace
from app.api.resources.mentorship_relation import (
mentorship_relation_ns as mentorship_namespace,
)

api.add_namespace(mentorship_namespace, path='/')
api.add_namespace(mentorship_namespace, path="/")
11 changes: 5 additions & 6 deletions app/api/dao/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from app.database.models.user import UserModel
from app.utils.decorator_utils import email_verification_required


class AdminDAO:
"""Data Access Object for Admin functionalities."""

Expand All @@ -19,19 +20,18 @@ def assign_new_user(user_id, data):
Returns:
message: A message corresponding to the completed action.
"""
new_admin_user_id = data['user_id']
new_admin_user_id = data["user_id"]

if user_id == new_admin_user_id:
return messages.USER_CANNOT_BE_ASSIGNED_ADMIN_BY_USER, 403

admin_user = UserModel.find_by_id(user_id)

if admin_user:
if admin_user:
if not admin_user.is_admin:
return messages.USER_ASSIGN_NOT_ADMIN, 403
else:
return messages.USER_NOT_FOUND, 404


new_admin_user = UserModel.find_by_id(new_admin_user_id)

Expand Down Expand Up @@ -61,7 +61,7 @@ def revoke_admin_user(user_id, data):
Returns:
message: A message corresponding to the completed action.
"""
admin_user_id = data['user_id']
admin_user_id = data["user_id"]

if user_id == admin_user_id:
return messages.USER_CANNOT_REVOKE_ADMIN_STATUS, 403
Expand All @@ -70,7 +70,7 @@ def revoke_admin_user(user_id, data):

admin_user = UserModel.find_by_id(user_id)

if admin_user:
if admin_user:
if not admin_user.is_admin:
return messages.USER_REVOKE_NOT_ADMIN, 403
else:
Expand Down Expand Up @@ -103,4 +103,3 @@ def list_admins(user_id):
list_of_users = [user.json() for user in users_list if user.is_admin]

return list_of_users

73 changes: 44 additions & 29 deletions app/api/dao/mentorship_relation.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from app.utils.decorator_utils import email_verification_required
from app.utils.enum_utils import MentorshipRelationState


class MentorshipRelationDAO:
"""Data Access Object for mentorship relation functionalities.
Expand All @@ -33,10 +34,10 @@ def create_mentorship_relation(self, user_id, data):
message: A message corresponding to the completed action; success if mentorship relationship is established, failure if otherwise.
"""
action_user_id = user_id
mentor_id = data['mentor_id']
mentee_id = data['mentee_id']
end_date_timestamp = data['end_date']
notes = data['notes']
mentor_id = data["mentor_id"]
mentee_id = data["mentee_id"]
end_date_timestamp = data["end_date"]
notes = data["notes"]

# user_id has to match either mentee_id or mentor_id
is_valid_user_ids = action_user_id == mentor_id or action_user_id == mentee_id
Expand Down Expand Up @@ -83,15 +84,18 @@ def create_mentorship_relation(self, user_id, data):
if not mentee_user.need_mentoring:
return messages.MENTEE_NOT_AVAIL_TO_BE_MENTORED, 400


# TODO add tests for this portion

all_mentor_relations = mentor_user.mentor_relations + mentor_user.mentee_relations
all_mentor_relations = (
mentor_user.mentor_relations + mentor_user.mentee_relations
)
for relation in all_mentor_relations:
if relation.state == MentorshipRelationState.ACCEPTED:
return messages.MENTOR_ALREADY_IN_A_RELATION, 400

all_mentee_relations = mentee_user.mentor_relations + mentee_user.mentee_relations
all_mentee_relations = (
mentee_user.mentor_relations + mentee_user.mentee_relations
)
for relation in all_mentee_relations:
if relation.state == MentorshipRelationState.ACCEPTED:
return messages.MENTEE_ALREADY_IN_A_RELATION, 400
Expand All @@ -101,14 +105,16 @@ def create_mentorship_relation(self, user_id, data):
tasks_list = TasksListModel()
tasks_list.save_to_db()

mentorship_relation = MentorshipRelationModel(action_user_id=action_user_id,
mentor_user=mentor_user,
mentee_user=mentee_user,
creation_date=datetime.now().timestamp(),
end_date=end_date_timestamp,
state=MentorshipRelationState.PENDING,
notes=notes,
tasks_list=tasks_list)
mentorship_relation = MentorshipRelationModel(
action_user_id=action_user_id,
mentor_user=mentor_user,
mentee_user=mentee_user,
creation_date=datetime.now().timestamp(),
end_date=end_date_timestamp,
state=MentorshipRelationState.PENDING,
notes=notes,
tasks_list=tasks_list,
)

mentorship_relation.save_to_db()

Expand All @@ -128,7 +134,8 @@ def list_mentorship_relations(user_id=None, state=None):
message: A message corresponding to the completed action; success if all relationships of a given user are listed, failure if otherwise.
"""
# To check if the entered 'state' is valid.
valid_states = ['PENDING','ACCEPTED','REJECTED','CANCELLED','COMPLETED']
valid_states = ["PENDING", "ACCEPTED", "REJECTED", "CANCELLED", "COMPLETED"]

def isValidState(rel_state):
if rel_state in valid_states:
return True
Expand All @@ -140,13 +147,15 @@ def isValidState(rel_state):
# Filtering the list of relations on the basis of 'state'.
if state:
if isValidState(state):
all_relations=list(filter(lambda rel: (rel.state.name == state), all_relations))
all_relations = list(
filter(lambda rel: (rel.state.name == state), all_relations)
)
else:
return [], 400

# add extra field for api response
for relation in all_relations:
setattr(relation, 'sent_by_me', relation.action_user_id == user_id)
setattr(relation, "sent_by_me", relation.action_user_id == user_id)

return all_relations, 200

Expand Down Expand Up @@ -191,18 +200,18 @@ def accept_request(user_id, request_id):

mentee = request.mentee
mentor = request.mentor
#If I am mentor : Check if the mentee isn't in any other relation already

# If I am mentor : Check if the mentee isn't in any other relation already
if user_id == mentor.id:
mentee_requests = mentee.mentee_relations + mentee.mentor_relations

for mentee_request in mentee_requests:
if mentee_request.state == MentorshipRelationState.ACCEPTED:
return messages.MENTEE_ALREADY_IN_A_RELATION, 400
#If I am mentee : Check if the mentor isn't in any other relation already
# If I am mentee : Check if the mentor isn't in any other relation already
else:
mentor_requests = mentor.mentee_relations + mentor.mentor_relations

for mentor_request in mentor_requests:
if mentor_request.state == MentorshipRelationState.ACCEPTED:
return messages.MENTOR_ALREADY_IN_A_RELATION, 400
Expand Down Expand Up @@ -334,12 +343,15 @@ def list_past_mentorship_relations(user_id):

user = UserModel.find_by_id(user_id)
now_timestamp = datetime.now().timestamp()
past_relations = list(filter(
lambda relation: relation.end_date < now_timestamp,
user.mentor_relations + user.mentee_relations))
past_relations = list(
filter(
lambda relation: relation.end_date < now_timestamp,
user.mentor_relations + user.mentee_relations,
)
)

for relation in past_relations:
setattr(relation, 'sent_by_me', relation.action_user_id == user_id)
setattr(relation, "sent_by_me", relation.action_user_id == user_id)

return past_relations, 200

Expand All @@ -360,7 +372,7 @@ def list_current_mentorship_relation(user_id):

for relation in all_relations:
if relation.state == MentorshipRelationState.ACCEPTED:
setattr(relation, 'sent_by_me', relation.action_user_id == user_id)
setattr(relation, "sent_by_me", relation.action_user_id == user_id)
return relation

return messages.NOT_IN_MENTORED_RELATION_CURRENTLY, 200
Expand All @@ -383,8 +395,11 @@ def list_pending_mentorship_relations(user_id):
all_relations = user.mentor_relations + user.mentee_relations

for relation in all_relations:
if relation.state == MentorshipRelationState.PENDING and relation.end_date > now_timestamp:
setattr(relation, 'sent_by_me', relation.action_user_id == user_id)
if (
relation.state == MentorshipRelationState.PENDING
and relation.end_date > now_timestamp
):
setattr(relation, "sent_by_me", relation.action_user_id == user_id)
pending_requests += [relation]

return pending_requests, 200
7 changes: 4 additions & 3 deletions app/api/dao/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def create_task(user_id, mentorship_relation_id, data):
response code.
"""

description = data['description']
description = data["description"]

user = UserModel.find_by_id(user_id)
relation = MentorshipRelationModel.find_by_id(_id=mentorship_relation_id)
Expand Down Expand Up @@ -137,10 +137,11 @@ def complete_task(user_id, mentorship_relation_id, task_id):
if task is None:
return messages.TASK_DOES_NOT_EXIST, 404

if task.get('is_done'):
if task.get("is_done"):
return messages.TASK_WAS_ALREADY_ACHIEVED, 400
else:
relation.tasks_list.update_task(
task_id=task_id, is_done=True, completed_at=datetime.now().timestamp())
task_id=task_id, is_done=True, completed_at=datetime.now().timestamp()
)

return messages.TASK_WAS_ACHIEVED_SUCCESSFULLY, 200
Loading

0 comments on commit 80114ac

Please sign in to comment.