async def update_article( # noqa: WPS211 self, *, article: Article, slug: Optional[str] = None, title: Optional[str] = None, body: Optional[str] = None, description: Optional[str] = None, ) -> Article: updated_article = article.copy(deep=True) updated_article.slug = slug or updated_article.slug updated_article.title = title or article.title updated_article.body = body or article.body updated_article.description = description or article.description async with self.connection.transaction(): updated_article.updated_at = await self._log_and_fetch_value( UPDATE_ARTICLE_QUERY, updated_article.slug, updated_article.title, updated_article.body, updated_article.description, article.slug, article.author.username, ) return updated_article
async def test_user_can_update_article( app: FastAPI, authorized_client: Client, test_article: Article, update_field: str, update_value: str, extra_updates: dict, ) -> None: response = await authorized_client.put( app.url_path_for("articles:update-article", slug=test_article.slug), json={"article": { update_field: update_value }}, ) assert response.status_code == status.HTTP_200_OK article = ArticleInResponse(**response.json()).article article_as_dict = article.dict() assert article_as_dict[update_field] == update_value for extra_field, extra_value in extra_updates.items(): assert article_as_dict[extra_field] == extra_value exclude_fields = {update_field, *extra_updates.keys(), "updated_at"} assert article.dict(exclude=exclude_fields) == test_article.dict( exclude=exclude_fields)
async def update_article( # noqa: WPS211 self, *, article: Article, slug: Optional[str] = None, title: Optional[str] = None, body: Optional[str] = None, description: Optional[str] = None, ) -> Article: updated_article = article.copy(deep=True) updated_article.slug = slug or updated_article.slug updated_article.title = title or article.title updated_article.body = body or article.body updated_article.description = description or article.description async with self.connection.transaction(): updated_article.updated_at = await queries.update_article( self.connection, slug=article.slug, author_username=article.author.username, new_slug=updated_article.slug, new_title=updated_article.title, new_body=updated_article.body, new_description=updated_article.description, ) return updated_article
async def _get_article_from_db_record( self, *, article_row: Record, slug: str, author_username: str, requested_user: Optional[User], ) -> Article: return Article( id_=article_row["id"], slug=slug, title=article_row["title"], description=article_row["description"], body=article_row["body"], author=await self._profiles_repo.get_profile_by_username( username=author_username, requested_user=requested_user, ), tags=await self.get_tags_for_article_by_slug(slug=slug), favorites_count=await self.get_favorites_count_for_article_by_slug(slug=slug, ), favorited=await self.is_article_favorited_by_user( slug=slug, user=requested_user, ) if requested_user else False, created_at=article_row["created_at"], updated_at=article_row["updated_at"], )
async def remove_article_from_favorites( article: Article = Depends(get_article_by_slug_from_path), user: User = Depends(get_current_user_authorizer()), articles_repo: ArticlesRepository = Depends( get_repository(ArticlesRepository)), ) -> ArticleInResponse: if article.favorited: await articles_repo.remove_article_from_favorites(article=article, user=user) return ArticleInResponse(article=article.copy( update={ "favorited": False, "favorites_count": article.favorites_count - 1, })) raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail=strings.ARTICLE_IS_NOT_FAVORITED)
async def mark_article_as_favorite( article: Article = Depends(get_article_by_slug_from_path), user: User = Depends(get_current_user_authorizer()), articles_repo: ArticlesRepository = Depends( get_repository(ArticlesRepository)), ) -> ArticleInResponse: if not article.favorited: await articles_repo.add_article_into_favorites(article=article, user=user) return ArticleInResponse(article=article.copy( update={ "favorited": True, "favorites_count": article.favorites_count + 1, })) raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail=strings.ARTICLE_IS_ALREADY_FAVORITED, )
async def _get_article_from_db_record( self, *, article_row: Record, slug: str, author_username: str, requested_user: Optional[User], ) -> Article: return Article( **article_row, author=await self._profiles_repo.get_profile_by_username( username=author_username, requested_user=requested_user ), tags=await self.get_tags_for_article_by_slug(slug=slug), favorites_count=await self.get_favorites_count_for_article_by_slug( slug=slug ), favorited=await self.is_article_favorited_by_user( slug=slug, user=requested_user ) if requested_user else False, )