Exemple #1
0
async def feed_articles(
    current_user: schemas.UserDB = Depends(deps.get_current_user()),
    limit: int = 20,
    offset: int = 0,
) -> schemas.MultipleArticlesInResponse:
    article_dbs = await crud_article.feed(limit=limit,
                                          offset=offset,
                                          follow_by=current_user.id)
    articles = []
    for article_db in article_dbs:
        profile = await crud_profile.get_profile_by_user_id(
            article_db.author_id, requested_user=current_user)
        tags = await crud_article.get_article_tags(article_db.id)
        favorited = await crud_article.is_article_favorited_by_user(
            article_db.id, current_user.id)
        favorites_count = await crud_article.count_article_favorites(
            article_db.id)
        article_for_reponse = schemas.ArticleForResponse(
            slug=article_db.slug,
            title=article_db.title,
            description=article_db.description,
            body=article_db.body,
            createdAt=article_db.created_at,
            updatedAt=article_db.updated_at,
            author=profile,  # type: ignore
            tagList=tags,
            favorited=favorited,
            favoritesCount=favorites_count,
        )
        articles.append(article_for_reponse)

    return schemas.MultipleArticlesInResponse(articles=articles,
                                              articlesCount=len(articles))
Exemple #2
0
async def delete_comment_for_article(
        slug: str,
        comment_id: int,
        current_user: schemas.UserDB = Depends(deps.get_current_user()),
) -> None:

    article_db = await crud_article.get_article_by_sluq(slug=slug)
    if article_db is None:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=SLUG_NOT_FOUND,
        )
    comment_db = await crud_comment.get(comment_id)
    if comment_db is None:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="comment with this id not found",
        )
    if comment_db.article_id != article_db.id:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="This comment is not belong to this article",
        )
    if comment_db.author_id != current_user.id:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="You can not delete a comment is not belong to yourself",
        )
    await crud_comment.delete(comment_id)
Exemple #3
0
async def get_article(
    slug: str,
    current_user: schemas.UserDB = Depends(
        deps.get_current_user(required=False)),
) -> schemas.ArticleInResponse:
    return await get_article_response_by_slug(slug=slug,
                                              current_user=current_user)
Exemple #4
0
async def create_article_comment(
    slug: str,
    comment_in: schemas.CommentInCreate = Body(...,
                                               embed=True,
                                               alias="comment"),
    current_user: schemas.UserDB = Depends(deps.get_current_user()),
) -> schemas.CommentInResponse:
    article_db = await crud_article.get_article_by_sluq(slug=slug)
    if article_db is None:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=SLUG_NOT_FOUND,
        )
    comment_id = await crud_comment.create(payload=comment_in,
                                           article_id=article_db.id,
                                           author_id=current_user.id)
    comment_db = await crud_comment.get(comment_id)  # type: ignore
    profile = await crud_profile.get_profile_by_user_id(
        comment_db.author_id,
        requested_user=current_user  # type: ignore
    )
    return schemas.CommentInResponse(comment=schemas.CommentForResponse(
        id=comment_db.id,  # type: ignore
        body=comment_db.body,  # type: ignore
        createdAt=comment_db.created_at,  # type: ignore
        updatedAt=comment_db.updated_at,  # type: ignore
        author=profile,  # type: ignore
    ))
Exemple #5
0
async def get_comments_from_an_article(
    slug: str,
    current_user: schemas.UserDB = Depends(
        deps.get_current_user(required=False)),
) -> schemas.MultipleCommentsInResponse:
    article_db = await crud_article.get_article_by_sluq(slug=slug)
    if article_db is None:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="article with this slug not found",
        )
    comment_dbs = await crud_comment.get_comments_from_an_article(
        article_id=article_db.id)
    comments = []
    for comment_db in comment_dbs:
        profile = await crud_profile.get_profile_by_user_id(
            comment_db.author_id, requested_user=current_user)
        comments.append(
            schemas.CommentForResponse(
                id=comment_db.id,
                body=comment_db.body,
                createdAt=comment_db.created_at,
                updatedAt=comment_db.updated_at,
                author=profile,  # type: ignore
            ))
    return schemas.MultipleCommentsInResponse(comments=comments)
async def update_current_user(
    user_update: schemas.UserUpdate = Body(..., embed=True, alias="user"),
    current_user: schemas.UserDB = Depends(deps.get_current_user()),
) -> schemas.UserResponse:
    if user_update.username and user_update.username != current_user.username:
        user_db = await crud_user.get_user_by_username(
            username=user_update.username)
        if user_db:
            raise HTTPException(
                status_code=status.HTTP_400_BAD_REQUEST,
                detail="user with this username already exists",
            )
    if user_update.email and user_update.email != current_user.email:
        user_db = await crud_user.get_user_by_email(email=user_update.email)
        if user_db:
            raise HTTPException(
                status_code=HTTP_400_BAD_REQUEST,
                detail="user with this email already exists",
            )
    user_id = await crud_user.update(user_id=current_user.id,
                                     payload=user_update)
    user_db = await crud_user.get(user_id)
    token = security.create_access_token(current_user.id)
    return schemas.UserResponse(user=schemas.UserWithToken(
        username=user_db.username,  # type: ignore
        email=user_db.email,  # type: ignore
        bio=user_db.bio,  # type: ignore
        image=user_db.image,  # type: ignore
        token=token,
    ))
async def get_profile(
    username: str,
    requested_user: schemas.UserDB = Depends(
        deps.get_current_user(required=False)),
) -> schemas.ProfileResponse:
    return await get_profile_response(requested_user=requested_user,
                                      username=username)
async def retrieve_current_user(
    current_user: schemas.UserDB = Depends(deps.get_current_user()),
) -> schemas.UserResponse:
    token = security.create_access_token(current_user.id)
    return schemas.UserResponse(user=schemas.UserWithToken(
        username=current_user.username,
        email=current_user.email,
        bio=current_user.bio,
        image=current_user.image,
        token=token,
    ))
Exemple #9
0
async def favorite_article(
    slug: str,
    current_user: schemas.UserDB = Depends(deps.get_current_user()),
) -> schemas.ArticleInResponse:
    article = await crud_article.get_article_by_sluq(slug=slug)
    if article is None:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=SLUG_NOT_FOUND,
        )
    await crud_article.favorite(article_id=article.id, user_id=current_user.id)
    return await get_article_response_by_slug(slug=slug,
                                              current_user=current_user)
async def follow_user(
    username: str,
    requested_user: schemas.UserDB = Depends(deps.get_current_user()),
) -> schemas.ProfileResponse:
    follower_user = await get_follow_user(requested_user=requested_user,
                                          username=username)
    if await crud_profile.is_following(follower_user, requested_user):
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="you follow this user already",
        )
    await crud_profile.follow(follower_user, requested_user)
    return await get_profile_response(requested_user=requested_user,
                                      username=username)
Exemple #11
0
async def delete_article(
    slug: str,
    current_user: schemas.UserDB = Depends(
        deps.get_current_user(required=False)),
) -> None:
    article_db = await crud_article.get_article_by_sluq(slug=slug)
    if article_db is None:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=SLUG_NOT_FOUND,
        )
    if article_db.author_id != current_user.id:
        raise HTTPException(
            status_code=status.HTTP_403_FORBIDDEN,
            detail="can not delete an article owner by other user",
        )
    await crud_article.delete(article_db)
Exemple #12
0
async def update_article(
    slug: str,
    article_in: schemas.ArticleInUpdate = Body(...,
                                               embed=True,
                                               alias="article"),
    current_user: schemas.UserDB = Depends(deps.get_current_user()),
) -> schemas.ArticleInResponse:
    article_db = await crud_article.get_article_by_sluq(slug)
    if article_db is None:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=SLUG_NOT_FOUND,
        )
    await crud_article.update(article_db, payload=article_in)

    return await get_article_response_by_slug(slug=slug,
                                              current_user=current_user)
Exemple #13
0
async def create_article(
    article_in: schemas.ArticleInCreate = Body(...,
                                               embed=True,
                                               alias="article"),
    current_user: schemas.UserDB = Depends(deps.get_current_user()),
) -> schemas.ArticleInResponse:
    article_id = await crud_article.create(article_in,
                                           author_id=current_user.id)
    article = await crud_article.get(article_id)
    profile = await crud_profile.get_profile_by_user_id(
        article.author_id,
        requested_user=current_user  # type: ignore
    )
    tags = await crud_article.get_article_tags(article_id)
    favorited = await crud_article.is_article_favorited_by_user(
        article_id, current_user.id)
    favorites_count = await crud_article.count_article_favorites(article_id)
    return gen_article_in_response(
        article=article,  # type: ignore
        favorited=favorited,
        favorites_count=favorites_count,
        profile=profile,  # type: ignore
        tags=tags,
    )
async def unfollow_user(
    username: str,
    requested_user: schemas.UserDB = Depends(deps.get_current_user()),
) -> schemas.ProfileResponse:
    follower_user = await crud_user.get_user_by_username(username=username)
    if follower_user is None:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="user with this username is not existed",
        )
    if follower_user.id == requested_user.id:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="cannot unfollow yourself",
        )
    if not await crud_profile.is_following(follower_user, requested_user):
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail="you don't follow this user already",
        )
    await crud_profile.unfollow(follower_user, requested_user)
    profile = await crud_profile.get_profile_by_username(
        username, requested_user)
    return schemas.ProfileResponse(profile=profile)  # type: ignore