コード例 #1
0
    def test_complete_task_api(self):

        relation = MentorshipRelationModel.find_by_id(
            self.mentorship_relation_w_second_user.id
        )
        incomplete_task = relation.tasks_list.find_task_by_id(1)
        self.assertIsNotNone(incomplete_task)
        self.assertIsNone(incomplete_task.get("completed_at"))
        self.assertFalse(incomplete_task.get("is_done"))

        auth_header = get_test_request_header(self.first_user.id)
        expected_response = messages.TASK_WAS_ACHIEVED_SUCCESSFULLY
        actual_response = self.client.put(
            "/mentorship_relation/%s/task/%s/complete"
            % (self.mentorship_relation_w_second_user.id, 1),
            follow_redirects=True,
            headers=auth_header,
        )

        self.assertEqual(200, actual_response.status_code)
        self.assertDictEqual(expected_response, json.loads(actual_response.data))

        relation = MentorshipRelationModel.find_by_id(
            self.mentorship_relation_w_second_user.id
        )
        complete_task = relation.tasks_list.find_task_by_id(1)
        self.assertIsNotNone(complete_task)
        self.assertIsNotNone(complete_task.get("completed_at"))
        self.assertTrue(complete_task.get("is_done"))
コード例 #2
0
    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.

        Args:
            user_id: ID of the user that is deleting a request.
            request_id: ID of the request.

        Returns:
            message: A message corresponding to the completed action; success if mentorship relation request is deleted, failure if otherwise.
        """

        user = UserModel.find_by_id(user_id)
        request = MentorshipRelationModel.find_by_id(request_id)

        # verify if request exists
        if request is None:
            return messages.MENTORSHIP_RELATION_REQUEST_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

        # verify if request is in pending state
        if request.state != MentorshipRelationState.PENDING:
            return messages.NOT_PENDING_STATE_RELATION, HTTPStatus.BAD_REQUEST

        # verify if user created the mentorship request
        if request.action_user_id != user_id:
            return messages.CANT_DELETE_UNINVOLVED_REQUEST, HTTPStatus.BAD_REQUEST

        # All was checked
        request.delete_from_db()

        return messages.MENTORSHIP_RELATION_WAS_DELETED_SUCCESSFULLY, HTTPStatus.OK
コード例 #3
0
    def cancel_relation(user_id: int, relation_id: int):
        """Allows a given user to terminate a particular relationship.

        Args:
            user_id: ID of the user terminating the relationship.
            relation_id: ID of the relationship.

        Returns:
            message: A message corresponding to the completed action; success if mentorship relation is terminated, failure if otherwise.
        """

        user = UserModel.find_by_id(user_id)
        request = MentorshipRelationModel.find_by_id(relation_id)

        # verify if request exists
        if request is None:
            return messages.MENTORSHIP_RELATION_REQUEST_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

        # verify if request is in pending state
        if request.state != MentorshipRelationState.ACCEPTED:
            return messages.UNACCEPTED_STATE_RELATION, HTTPStatus.BAD_REQUEST

        # verify if I'm involved in this relation
        if not (request.mentee_id == user_id or request.mentor_id == user_id):
            return messages.CANT_CANCEL_UNINVOLVED_REQUEST, HTTPStatus.BAD_REQUEST

        # All was checked
        request.state = MentorshipRelationState.CANCELLED
        request.save_to_db()

        return messages.MENTORSHIP_RELATION_WAS_CANCELLED_SUCCESSFULLY, HTTPStatus.OK
コード例 #4
0
    def reject_request(user_id: int, request_id: int):
        """Rejects a mentorship request.

        Args:
            user_id: ID of the user rejecting the request.
            request_id: ID of the request to be rejected.

        Returns:
            message: A message corresponding to the completed action; success if mentorship relation request is rejected, failure if otherwise.
        """

        user = UserModel.find_by_id(user_id)
        request = MentorshipRelationModel.find_by_id(request_id)

        # verify if request exists
        if request is None:
            return messages.MENTORSHIP_RELATION_REQUEST_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

        # verify if request is in pending state
        if request.state != MentorshipRelationState.PENDING:
            return messages.NOT_PENDING_STATE_RELATION, HTTPStatus.BAD_REQUEST

        # verify if I'm the receiver of the request
        if request.action_user_id == user_id:
            return messages.USER_CANT_REJECT_REQUEST_SENT_BY_USER, HTTPStatus.BAD_REQUEST

        # verify if I'm involved in this relation
        if not (request.mentee_id == user_id or request.mentor_id == user_id):
            return messages.CANT_REJECT_UNINVOLVED_RELATION_REQUEST, HTTPStatus.BAD_REQUEST

        # All was checked
        request.state = MentorshipRelationState.REJECTED
        request.save_to_db()

        return messages.MENTORSHIP_RELATION_WAS_REJECTED_SUCCESSFULLY, HTTPStatus.OK
コード例 #5
0
    def accept_request(user_id: int, request_id: int):
        """Allows a mentorship request.

        Args:
            user_id: ID of the user accepting the request.
            request_id: ID of the request to be accepted.

        Returns:
            message: A message corresponding to the completed action; success if mentorship relation request is accepted, failure if otherwise.
        """

        user = UserModel.find_by_id(user_id)
        request = MentorshipRelationModel.find_by_id(request_id)

        # verify if request exists
        if request is None:
            return messages.MENTORSHIP_RELATION_REQUEST_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

        # verify if request is in pending state
        if request.state != MentorshipRelationState.PENDING:
            return messages.NOT_PENDING_STATE_RELATION, HTTPStatus.BAD_REQUEST

        # verify if I'm the receiver of the request
        if request.action_user_id == user_id:
            return messages.CANT_ACCEPT_MENTOR_REQ_SENT_BY_USER, HTTPStatus.BAD_REQUEST

        # verify if I'm involved in this relation
        if not (request.mentee_id == user_id or request.mentor_id == user_id):
            return messages.CANT_ACCEPT_UNINVOLVED_MENTOR_RELATION, HTTPStatus.BAD_REQUEST

        my_requests = user.mentee_relations + user.mentor_relations

        # verify if I'm on a current relation
        for my_request in my_requests:
            if my_request.state == MentorshipRelationState.ACCEPTED:
                return messages.USER_IS_INVOLVED_IN_A_MENTORSHIP_RELATION, HTTPStatus.BAD_REQUEST

        mentee = request.mentee
        mentor = request.mentor

        # 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, HTTPStatus.BAD_REQUEST
        # 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, HTTPStatus.BAD_REQUEST

        # All was checked
        request.state = MentorshipRelationState.ACCEPTED
        request.save_to_db()

        return messages.MENTORSHIP_RELATION_WAS_ACCEPTED_SUCCESSFULLY, HTTPStatus.OK
コード例 #6
0
    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
        mentorship relation.

        Args:
            user_id: The id of the user.
            mentorship_relation_id: The id of the mentorship relation.
            task_id: The id of the task.

        Returns:
            A two element list where the first element is a dictionary containing a key 'message' indicating in its value if the
            task was deleted successfully or not as a string. The last element is the HTTP response code.
        """

        user = UserModel.find_by_id(user_id)
        relation = MentorshipRelationModel.find_by_id(mentorship_relation_id)
        if relation is None:
            return messages.MENTORSHIP_RELATION_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

        task = relation.tasks_list.find_task_by_id(task_id)
        if task is None:
            return messages.TASK_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

        if not (user_id == relation.mentee_id or user_id == relation.mentor_id):
            return messages.USER_NOT_INVOLVED_IN_THIS_MENTOR_RELATION, HTTPStatus.UNAUTHORIZED

        relation.tasks_list.delete_task(task_id)

        return messages.TASK_WAS_DELETED_SUCCESSFULLY, HTTPStatus.OK
コード例 #7
0
    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.

        Args:
            user_id: The id of the user.
            mentorship_relation_id: The id of the mentorship relation.

        Returns:
            A list containing all the tasks one user has in a mentorship relation. otherwise, it returns a two element list where the first element is
            a dictionary containing a key 'message' indicating in its value if there were any problem finding user's tasks in the specified
            mentorship relation as a string. The last element is the HTTP response code
        """

        user = UserModel.find_by_id(user_id)
        relation = MentorshipRelationModel.find_by_id(mentorship_relation_id)
        if relation is None:
            return messages.MENTORSHIP_RELATION_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

        if not (user_id == relation.mentee_id or user_id == relation.mentor_id):
            return messages.USER_NOT_INVOLVED_IN_THIS_MENTOR_RELATION, HTTPStatus.UNAUTHORIZED

        all_tasks = relation.tasks_list.tasks

        return all_tasks
コード例 #8
0
    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.

        Args:
            user_id: The id of the user.
            mentorship_relation_id: The id of the mentorship relation.
            data: A list containing the description of the task.

        Returns:
            A two element list where the first element is a dictionary containing a key 'message' indicating
            in its value if the task creation was successful or not as a string. The last element is the HTTP
            response code.
        """

        description = data["description"]

        user = UserModel.find_by_id(user_id)
        relation = MentorshipRelationModel.find_by_id(_id=mentorship_relation_id)
        if relation is None:
            return messages.MENTORSHIP_RELATION_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

        if relation.state != MentorshipRelationState.ACCEPTED:
            return messages.UNACCEPTED_STATE_RELATION, HTTPStatus.FORBIDDEN

        if (relation.mentor_id != user_id) and (relation.mentee_id != user_id):
            return messages.USER_NOT_INVOLVED_IN_THIS_MENTOR_RELATION, 403

        now_timestamp = datetime.now().timestamp()
        relation.tasks_list.add_task(description=description, created_at=now_timestamp)
        relation.tasks_list.save_to_db()

        return messages.TASK_WAS_CREATED_SUCCESSFULLY, HTTPStatus.CREATED
コード例 #9
0
def validate_data_for_task_comment(user_id, task_id, relation_id):
    relation = MentorshipRelationModel.find_by_id(relation_id)
    if relation is None:
        return messages.MENTORSHIP_RELATION_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

    if user_id != relation.mentor_id and user_id != relation.mentee_id:
        return messages.USER_NOT_INVOLVED_IN_THIS_MENTOR_RELATION, HTTPStatus.UNAUTHORIZED

    if relation.state != MentorshipRelationState.ACCEPTED:
        return messages.UNACCEPTED_STATE_RELATION, HTTPStatus.BAD_REQUEST

    task = relation.tasks_list.find_task_by_id(task_id)
    if task is None:
        return messages.TASK_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

    return {}
コード例 #10
0
ファイル: task.py プロジェクト: vipulss/learningweb-python
    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
        mentorship relation to finished task status.

        Args:
            user_id: The id of the user.
            mentorship_relation_id: The id of the mentorship relation.
            task_id: The id of the task.

        Returns:
            A two element list where the first element is a dictionary containing a key 'message' indicating in its value
            if the task was set to complete successfully or not as a string. The last element is the HTTP response code.
        """

        user = UserModel.find_by_id(user_id)
        relation = MentorshipRelationModel.find_by_id(mentorship_relation_id)
        if relation is None:
            return messages.MENTORSHIP_RELATION_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

        if not (user_id == relation.mentee_id
                or user_id == relation.mentor_id):
            return messages.USER_NOT_INVOLVED_IN_THIS_MENTOR_RELATION, HTTPStatus.UNAUTHORIZED

        task = relation.tasks_list.find_task_by_id(task_id)
        if task is None:
            return messages.TASK_DOES_NOT_EXIST, HTTPStatus.NOT_FOUND

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

        return messages.TASK_WAS_ACHIEVED_SUCCESSFULLY, HTTPStatus.OK
コード例 #11
0
def send_email_mentorship_relation_accepted(request_id):
    """
    Sends a notification email to the sender of the mentorship relation request,
    stating that his request has been accepted.

    Args:
        request_id: Request id of the mentorship request.
    """

    from app.database.models.user import UserModel
    from app.database.models.mentorship_relation import MentorshipRelationModel

    # Getting the request from id.
    request = MentorshipRelationModel.find_by_id(request_id)

    # Getting the sender and receiver of the mentorship request from their ids.
    if request.action_user_id == request.mentor_id:
        request_sender = UserModel.find_by_id(request.mentor_id)
        request_receiver = UserModel.find_by_id(request.mentee_id)
        role = "mentee"
    else:
        request_sender = UserModel.find_by_id(request.mentee_id)
        request_receiver = UserModel.find_by_id(request.mentor_id)
        role = "mentor"

    end_date = request.end_date
    date = datetime.datetime.fromtimestamp(end_date).strftime("%d-%m-%Y")

    subject = "Mentorship relation accepted!"
    html = render_template(
        "mentorship_relation_accepted.html",
        request_sender=request_sender.name,
        request_receiver=request_receiver.name,
        role=role,
        end_date=date,
    )
    send_email(request_sender.email, subject, html)
コード例 #12
0
 def test_find_mentorship_relation_by_id(self):
     query_mentorship_relation = MentorshipRelationModel.query.first()
     find_by_id_result = MentorshipRelationModel.find_by_id(
         query_mentorship_relation.id)
     self.assertEqual(query_mentorship_relation, find_by_id_result)