async def describe_from_user( username: str = Path(..., min_length=1), user: User = Depends(get_current_user_authorizer()), db: DataBase = Depends(get_database), ): if username == user.username: raise HTTPException( status_code=HTTP_422_UNPROCESSABLE_ENTITY, detail=f"User can not describe from them self", ) async with db.pool.acquire() as conn: profile = await get_profile_for_user(conn, username, user.username) if not profile.following: raise HTTPException( status_code=HTTP_422_UNPROCESSABLE_ENTITY, detail=f"You did not follow this user", ) async with conn.transaction(): await unfollow_user(conn, user.username, profile.username) profile.following = False return ProfileInResponse(profile=profile)
async def update(user: UserSave = Body(...), current_user: UserBase = Depends( get_current_user_authorizer())): if user.email != current_user.email: raise HTTPException(500, 'Email can\'t be changed!') db_user = await update_user(current_user.username, user) return UserResponse(**db_user.serialize())
async def delete_comment_from_article( slug: str = Path(..., min_length=1), id: int = Path(..., ge=1), user: User = Depends(get_current_user_authorizer()), db: DataBase = Depends(get_database), ): async with db.pool.acquire() as conn: await get_article_or_404(conn, slug, user.username) await delete_comment(conn, id, user.username)
async def get_comment_from_article( slug: str = Path(..., min_length=1), user: User = Depends(get_current_user_authorizer(required=False)), db: DataBase = Depends(get_database), ): async with db.pool.acquire() as conn: await get_article_or_404(conn, slug, user.username) dbcomments = await get_comments_for_article(conn, slug, user.username) return create_aliased_response( ManyCommentsInResponse(comments=dbcomments))
async def articles_feed( limit: int = Query(20, gt=0), offset: int = Query(0, ge=0), user: User = Depends(get_current_user_authorizer()), db: DataBase = Depends(get_database), ): async with db.pool.acquire() as conn: dbarticles = await get_user_articles(conn, user.username, limit, offset) return create_aliased_response( ManyArticlesInResponse(articles=dbarticles, articles_count=len(dbarticles)) )
async def retrieve_profile( username: str = Path(..., min_length=1), user: Optional[User] = Depends(get_current_user_authorizer(required=False)), db: DataBase = Depends(get_database), ): async with db.pool.acquire() as conn: profile = await get_profile_for_user( conn, username, user.username if user else None ) profile = ProfileInResponse(profile=profile) return profile
async def delete_article( slug: str = Path(..., min_length=1), user: User = Depends(get_current_user_authorizer()), db: DataBase = Depends(get_database), ): async with db.pool.acquire() as conn: await check_article_for_existence_and_modifying_permissions( conn, slug, user.username ) async with conn.transaction(): await delete_article_by_slug(conn, slug, user.username)
async def register(user: UserSave = Body(...), current_user: UserBase = Depends( get_current_user_authorizer())): if user.email == current_user.email: raise HTTPException( status_code=HTTP_409_CONFLICT, detail="Email already in use.", ) await check_free_username_and_email(user.email) db_user = await create_user(user) return UserResponse(**db_user.serialize())
async def update_article( slug: str = Path(..., min_length=1), article: ArticleInUpdate = Body(..., embed=True), user: User = Depends(get_current_user_authorizer()), db: DataBase = Depends(get_database), ): async with db.pool.acquire() as conn: await check_article_for_existence_and_modifying_permissions( conn, slug, user.username ) async with conn.transaction(): dbarticle = await update_article_by_slug(conn, slug, article, user.username) return create_aliased_response(ArticleInResponse(article=dbarticle))
async def create_comment_for_article( *, slug: str = Path(..., min_length=1), comment: CommentInCreate = Body(..., embed=True), user: User = Depends(get_current_user_authorizer()), db: DataBase = Depends(get_database), ): async with db.pool.acquire() as conn: await get_article_or_404(conn, slug, user.username) async with conn.transaction(): dbcomment = await create_comment(conn, slug, comment, user.username) return create_aliased_response( CommentInResponse(comment=dbcomment))
async def get_article( slug: str = Path(..., min_length=1), user: Optional[User] = Depends(get_current_user_authorizer(required=False)), db: DataBase = Depends(get_database), ): async with db.pool.acquire() as conn: dbarticle = await get_article_by_slug( conn, slug, user.username if user else None ) if not dbarticle: raise HTTPException( status_code=HTTP_404_NOT_FOUND, detail=f"Article with slug '{slug}' not found", ) return create_aliased_response(ArticleInResponse(article=dbarticle))
async def update_current_user( user: UserInUpdate = Body(..., embed=True), current_user: User = Depends(get_current_user_authorizer()), db: DataBase = Depends(get_database), ): async with db.pool.acquire() as conn: if user.username == current_user.username: user.username = None if user.email == current_user.email: user.email = None await check_free_username_and_email(conn, user.username, user.email) async with conn.transaction(): dbuser = await update_user(conn, current_user.username, user) return UserInResponse( user=User(**dbuser.dict(), token=current_user.token))
async def create_new_article( article: ArticleInCreate = Body(..., embed=True), user: User = Depends(get_current_user_authorizer()), db: DataBase = Depends(get_database), ): async with db.pool.acquire() as conn: article_by_slug = await get_article_by_slug( conn, slugify(article.title), user.username ) if article_by_slug: raise HTTPException( status_code=HTTP_422_UNPROCESSABLE_ENTITY, detail=f"Article with slug '{article_by_slug.slug}' already exists", ) async with conn.transaction(): dbarticle = await create_article_by_slug(conn, article, user.username) return create_aliased_response(ArticleInResponse(article=dbarticle))
async def get_articles( tag: str = "", author: str = "", favorited: str = "", limit: int = Query(20, gt=0), offset: int = Query(0, ge=0), user: User = Depends(get_current_user_authorizer(required=False)), db: DataBase = Depends(get_database), ): filters = ArticleFilterParams( tag=tag, author=author, favorited=favorited, limit=limit, offset=offset ) async with db.pool.acquire() as conn: dbarticles = await get_articles_with_filters( conn, filters, user.username if user else None ) return create_aliased_response( ManyArticlesInResponse(articles=dbarticles, articles_count=len(dbarticles)) )
async def favorite_article( slug: str = Path(..., min_length=1), user: User = Depends(get_current_user_authorizer()), db: DataBase = Depends(get_database), ): async with db.pool.acquire() as conn: dbarticle = await get_article_or_404(conn, slug, user.username) if dbarticle.favorited: raise HTTPException( status_code=HTTP_400_BAD_REQUEST, detail="You already added this article to favorites", ) dbarticle.favorited = True dbarticle.favorites_count += 1 async with conn.transaction(): await add_article_to_favorites(conn, slug, user.username) return create_aliased_response(ArticleInResponse(article=dbarticle))
async def delete_article_from_favorites( slug: str = Path(..., min_length=1), user: User = Depends(get_current_user_authorizer()), db: DataBase = Depends(get_database), ): async with db.pool.acquire() as conn: dbarticle = await get_article_or_404(conn, slug, user.username) if not dbarticle.favorited: raise HTTPException( status_code=HTTP_400_BAD_REQUEST, detail="You don't have this article in favorites", ) dbarticle.favorited = False dbarticle.favorites_count -= 1 async with conn.transaction(): await remove_article_from_favorites(conn, slug, user.username) return create_aliased_response(ArticleInResponse(article=dbarticle))
async def retrieve_current_user(user: User = Depends( get_current_user_authorizer())): return UserInResponse(user=user)
)) return UserPaginate( items=items, total=all_users.total, current_page=all_users.current_page, last_page=all_users.last_page, per_page=all_users.per_page, ) @user_router.post("/user", response_model=UserResponse, tags=["Users"], status_code=HTTP_201_CREATED, dependencies=[Depends(get_current_user_authorizer())]) async def register(user: UserSave = Body(...), current_user: UserBase = Depends( get_current_user_authorizer())): if user.email == current_user.email: raise HTTPException( status_code=HTTP_409_CONFLICT, detail="Email already in use.", ) await check_free_username_and_email(user.email) db_user = await create_user(user) return UserResponse(**db_user.serialize())
async def retrieve_current_user(user: User = Depends( get_current_user_authorizer(required=False))): logger.info(user) return {}