diff --git a/app/api/dao/task.py b/app/api/dao/task.py index 6bf92055c..cec551b7c 100644 --- a/app/api/dao/task.py +++ b/app/api/dao/task.py @@ -30,6 +30,10 @@ def create_task(user_id: int, mentorship_relation_id: int, data: Dict[str, str]) """ description = data["description"] + if 'requires_approval' in data.keys(): + requires_approval = data['requires_approval'] + else: + requires_approval = False user = UserModel.find_by_id(user_id) relation = MentorshipRelationModel.find_by_id(_id=mentorship_relation_id) @@ -46,7 +50,8 @@ def create_task(user_id: int, mentorship_relation_id: int, data: Dict[str, str]) ) now_timestamp = datetime.utcnow().timestamp() - relation.tasks_list.add_task(description=description, created_at=now_timestamp) + relation.tasks_list.add_task(description=description, created_at=now_timestamp, + requires_approval=requires_approval) relation.tasks_list.save_to_db() return messages.TASK_WAS_CREATED_SUCCESSFULLY, HTTPStatus.CREATED @@ -153,6 +158,9 @@ def complete_task(user_id: int, mentorship_relation_id: int, task_id: int): if task is None: return messages.TASK_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND + if user_id == relation.mentee_id and task.get('requires_approval'): + return messages.TASK_REQUIRES_APPROVAL, HTTPStatus.UNAUTHORIZED + if task.get("is_done"): return messages.TASK_WAS_ALREADY_ACHIEVED, HTTPStatus.CONFLICT else: diff --git a/app/api/models/task.py b/app/api/models/task.py index d5f7b931a..fa6a10241 100644 --- a/app/api/models/task.py +++ b/app/api/models/task.py @@ -11,7 +11,8 @@ def add_models_to_namespace(api_namespace): { "description": fields.String( required=True, description="Mentorship relation task description" - ) + ), + 'requires_approval': fields.Boolean(required=False, description='If true, only mentor can mark task as completed.') }, ) @@ -28,5 +29,6 @@ def add_models_to_namespace(api_namespace): "completed_at": fields.Float( required=False, description="Task completion date in UNIX timestamp format" ), + 'requires_approval': fields.Boolean(required=False, description='If true, only mentor can mark task as completed.') }, ) diff --git a/app/database/models/tasks_list.py b/app/database/models/tasks_list.py index b69269d54..e56933ae7 100644 --- a/app/database/models/tasks_list.py +++ b/app/database/models/tasks_list.py @@ -42,7 +42,7 @@ def __init__(self, tasks: "TasksListModel" = None): raise ValueError(TypeError) def add_task( - self, description: str, created_at: date, is_done=False, completed_at=None + self, description: str, created_at: date, is_done=False, completed_at=None,requires_approval=False ) -> None: """Adds a task to the list of tasks. @@ -51,6 +51,7 @@ def add_task( created_at: Date on which the task is created. is_done: Boolean specifying completion of the task. completed_at: Date on which task is completed. + requires_approval: Boolean specifying whether mentee can mark task as completed on his own. """ task = { @@ -59,6 +60,7 @@ def add_task( TasksFields.IS_DONE.value: is_done, TasksFields.CREATED_AT.value: created_at, TasksFields.COMPLETED_AT.value: completed_at, + TasksFields.REQUIRES_APPROVAL.value: requires_approval } self.next_task_id += 1 self.tasks = self.tasks + [task] @@ -195,6 +197,7 @@ class TasksFields(Enum): IS_DONE: Boolean specifying the completion of the task. COMPLETED_AT: The date on which the task is completed. CREATED_AT: The date on which the task was created. + REQUIRES_APPROVAL: Boolean specifying whether mentee can mark task as completed on his own. """ ID = "id" @@ -202,6 +205,7 @@ class TasksFields(Enum): IS_DONE = "is_done" COMPLETED_AT = "completed_at" CREATED_AT = "created_at" + REQUIRES_APPROVAL = 'requires_approval' def values(self): """Returns a list containing a task.""" diff --git a/app/messages.py b/app/messages.py index 79c7cd365..b8ee3df79 100644 --- a/app/messages.py +++ b/app/messages.py @@ -37,6 +37,7 @@ TASK_COMMENT_WITH_GIVEN_TASK_ID_DOES_NOT_EXIST = { "message": "Task comment with given task id does not exist." } +TASK_REQUIRES_APPROVAL = {"message": "Task requires mentor's approval."} # Missing fields MENTOR_ID_FIELD_IS_MISSING = {"message": "Mentor ID field is missing."}