From 28fc9c39958e2c25049e379b185e78ecc716b926 Mon Sep 17 00:00:00 2001 From: claustra01 Date: Thu, 21 Dec 2023 12:20:21 +0900 Subject: [PATCH 1/4] fix: get scheduled blogs --- blogs/cruds/blogs/blogs.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/blogs/cruds/blogs/blogs.py b/blogs/cruds/blogs/blogs.py index 7178d4b..f4cb0e1 100644 --- a/blogs/cruds/blogs/blogs.py +++ b/blogs/cruds/blogs/blogs.py @@ -110,10 +110,12 @@ def get_blogs_pagination( .group_by(blog_models.Blog.id) .order_by(desc(blog_models.Blog.created_at)) ) - if not (user_id == searched_user_id and user_id is not None): - blogs_orm = blogs_orm.filter(blog_models.Blog.published_at <= datetime.now()) if user_id != searched_user_id: blogs_orm = blogs_orm.filter(blog_models.Blog.visibility != Visibility.draft) + blogs_orm = blogs_orm.filter( + blog_models.Blog.published_at + and blog_models.Blog.published_at <= datetime.now() + ) if user_id is None: blogs_orm = blogs_orm.filter(blog_models.Blog.visibility == Visibility.public) elif visibility is not None: From 24a1aa5a53bd418ba550668691046178f8009c44 Mon Sep 17 00:00:00 2001 From: claustra01 Date: Thu, 21 Dec 2023 12:23:15 +0900 Subject: [PATCH 2/4] chore: fix blog filter condition --- blogs/cruds/blogs/blogs.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/blogs/cruds/blogs/blogs.py b/blogs/cruds/blogs/blogs.py index f4cb0e1..dad674a 100644 --- a/blogs/cruds/blogs/blogs.py +++ b/blogs/cruds/blogs/blogs.py @@ -110,7 +110,7 @@ def get_blogs_pagination( .group_by(blog_models.Blog.id) .order_by(desc(blog_models.Blog.created_at)) ) - if user_id != searched_user_id: + if user_id != searched_user_id and user_id is not None: blogs_orm = blogs_orm.filter(blog_models.Blog.visibility != Visibility.draft) blogs_orm = blogs_orm.filter( blog_models.Blog.published_at From 55df1aef3422478b79364f9250ca3e8f8eb6d84f Mon Sep 17 00:00:00 2001 From: claustra01 Date: Fri, 22 Dec 2023 19:01:27 +0900 Subject: [PATCH 3/4] =?UTF-8?q?fix:=20get=5Fblog=5Fby=5Fid()=E3=81=A7publi?= =?UTF-8?q?shed=5Fat=E3=81=8C=E7=84=A1=E3=81=84=E6=99=82=E3=81=AE=E3=83=90?= =?UTF-8?q?=E3=82=B0=E3=82=92=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blogs/cruds/blogs/blogs.py | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/blogs/cruds/blogs/blogs.py b/blogs/cruds/blogs/blogs.py index dad674a..cb373e9 100644 --- a/blogs/cruds/blogs/blogs.py +++ b/blogs/cruds/blogs/blogs.py @@ -161,12 +161,14 @@ def get_blogs_pagination( def get_blog_by_id(db: Session, blog_id: str, user_id: str) -> Blog: blog_orm = db.query(blog_models.Blog).get(blog_id) - if (blog_orm is None) or ( - ( - blog_orm.visibility == Visibility.draft - or blog_orm.published_at > datetime.now() - ) - and user_id != blog_orm.user_id + if blog_orm is None: + raise HTTPException(status_code=404, detail="work is not found") + if blog_orm.user_id != user_id and blog_orm.visibility == Visibility.draft: + raise HTTPException(status_code=404, detail="work is not found") + if ( + blog_orm.user_id != user_id + and blog_orm.published_at is not None + and blog_orm.published_at > datetime.now() ): raise HTTPException(status_code=404, detail="work is not found") if blog_orm.visibility == Visibility.private and user_id is None: From a2d072db8441bf1ecc7c5e2cccd692a011d87419 Mon Sep 17 00:00:00 2001 From: claustra01 Date: Fri, 22 Dec 2023 19:20:41 +0900 Subject: [PATCH 4/4] =?UTF-8?q?fix:=20sqlalchemy=E3=81=AE=E6=96=87?= =?UTF-8?q?=E6=B3=95=E3=81=AB=E4=BF=AE=E6=AD=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blogs/cruds/blogs/blogs.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/blogs/cruds/blogs/blogs.py b/blogs/cruds/blogs/blogs.py index cb373e9..b83354c 100644 --- a/blogs/cruds/blogs/blogs.py +++ b/blogs/cruds/blogs/blogs.py @@ -2,7 +2,7 @@ from typing import Optional from fastapi import HTTPException -from sqlalchemy import desc +from sqlalchemy import desc, null, or_ from sqlalchemy.exc import SQLAlchemyError from sqlalchemy.orm import Session @@ -113,8 +113,10 @@ def get_blogs_pagination( if user_id != searched_user_id and user_id is not None: blogs_orm = blogs_orm.filter(blog_models.Blog.visibility != Visibility.draft) blogs_orm = blogs_orm.filter( - blog_models.Blog.published_at - and blog_models.Blog.published_at <= datetime.now() + or_( + blog_models.Blog.published_at.is_(null()), + blog_models.Blog.published_at <= datetime.now(), + ) ) if user_id is None: blogs_orm = blogs_orm.filter(blog_models.Blog.visibility == Visibility.public)