Пример #1
0
async def update_comment(res: Response, comment_ID: int,
                         comment_data: CommentBaseModel) -> Dict[str, Any]:
    """Update comment attributes according to CommentBaseModel class\n

    Args:\n
        comment_ID (int): comment to update\n
        comment_data (CommentBaseModel): new comment data\n

    Returns:\n
        Dict[str, Any]: contains comment new data or error\n
    """
    response = {"success": True, "comment": {}}

    new_owner = await Person.get_or_none(id=comment_data.user)
    if new_owner is None:
        res.status_code = status.HTTP_404_NOT_FOUND
        response["success"] = False
        response["detail"] = "Comment owner doesn't exist."
        return response

    comment_found = await Comment.get_or_none(id=comment_ID)
    if comment_found is None:
        res.status_code = status.HTTP_404_NOT_FOUND
        response["success"] = False
        response["detail"] = f"Comment with ID {comment_ID} doesn't exist."
        return response

    comment_data.user = new_owner

    comment_updated = comment_found.update_from_dict(comment_data.__dict__)
    await comment_updated.save()
    response["detail"] = "Comment successfully updated"
    response["comment"] = API_functools.tortoise_to_dict(comment_updated)
    return jsonable_encoder(response)
Пример #2
0
async def create_comment(res: Response, comment: dict) -> Dict[str, Any]:
    """Create new comment\n

    Args:\n
        comment (dict): Comment to create\n

    Returns:\n
        Dict[str, Any]: Comment created\n
    """
    data = {
        "success": True,
        "comment": {},
        "detail": "Comment successfully created",
    }
    comment_owner = API_functools.get_or_default(
        await Person.filter(pk=comment.get("user", 0)), 0, None)

    if comment_owner is None:
        res.status_code = status.HTTP_404_NOT_FOUND
        data["success"] = False
        data["detail"] = "Comment owner doesn't exist"
        return data
    comment["user"] = comment_owner
    data["comment"] = API_functools.tortoise_to_dict(await
                                                     Comment.create(**comment))
    return jsonable_encoder(data)
Пример #3
0
async def fix_comment(res: Response, comment_ID: int,
                      comment_data: PartialComment) -> Dict[str, Any]:
    """Fix some comment attributes according to PartialComment class\n

    Args:\n
        comment_ID (int): user ID\n
        comment_data (PartialComment): new data\n

    Returns:\n
        Dict[str, Any]: contains updated Comment data or error\n
    """
    response = {"success": True, "comment": {}}

    comment_found = await Comment.get_or_none(id=comment_ID)
    if comment_found is None:
        res.status_code = status.HTTP_404_NOT_FOUND
        response["success"] = False
        response["detail"] = f"Comment with ID {comment_ID} doesn't exist."
        return response

    comment_updated = comment_found.update_from_dict(comment_data.__dict__)
    await comment_updated.save()
    response["detail"] = "Comment successfully patched"
    response["comment"] = API_functools.tortoise_to_dict(comment_updated)
    return jsonable_encoder(response)
Пример #4
0
 async def test_tortoise_to_dict(self):
     actual = API_functools.tortoise_to_dict(
         await Person(**INIT_DATA.get("person", [])[0]))
     actual["date_of_birth"] = jsonable_encoder(actual["date_of_birth"])
     assert actual == {
         "avatar": avatar,
         "company": "Edgetag",
         "country_of_birth": "Egypt",
         "date_of_birth": "1978-04-15",
         "email": "*****@*****.**",
         "first_name": "Shalom",
         "gender": "Male",
         "id": None,
         "is_admin": True,
         "job": "Compensation Analyst",
         "last_name": "Handes",
     }
Пример #5
0
async def delete_comment(res: Response, comment_ID: int) -> Dict[str, Any]:
    """Delete a comment\n

    Args:\n
        comment_ID (int): comment to delete\n

    Returns:\n
        Dict[str, Any]: contains deleted comment data or error\n
    """
    response = {"success": False, "comment": {}}

    comment_found = await Comment.get_or_none(id=comment_ID)
    if comment_found is None:
        res.status_code = status.HTTP_404_NOT_FOUND
        response["detail"] = f"Comment with ID {comment_ID} doesn't exist"
        return response

    await comment_found.delete()

    response["success"] = True
    response["comment"] = API_functools.tortoise_to_dict(comment_found)
    response["detail"] = f"Comment {comment_ID} deleted successfully тнР"
    return jsonable_encoder(response)