Skip to content
This repository has been archived by the owner on Oct 24, 2024. It is now read-only.

Commit

Permalink
feat(postlist): Filter items by selected tag
Browse files Browse the repository at this point in the history
y0nei committed May 16, 2023

Verified

This commit was signed with the committer’s verified signature.
y0nei Yonei
1 parent 6bab98d commit 171f90b
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/routes/posts.py
Original file line number Diff line number Diff line change
@@ -21,6 +21,7 @@ async def post_list(
order: OrderChoices = OrderChoices.ascending,
page: int = Query(1, gt=0),
page_items: int = Query(2, gt=0),
tag: str | None = Query(None),
hx_request: str | None = Header(None)
):
posts = []
@@ -46,6 +47,11 @@ def getPost(post_id: int):
except ValueError:
pass

if tag in taglist:
posts = [post for post in posts if post.get("tags") and tag in post["tags"]]
elif tag is not None:
raise HTTPException(status_code=400, detail=f"The tag [{tag}] does not exist.")

posts = sortPosts(posts, sort, order)

# Simple pagination
@@ -59,14 +65,20 @@ def getPost(post_id: int):
# next and previous pages
pagination = {}
if page > 1:
pagination["prev"] = f"/posts?sort={sort.value}&order={order.value}&page={page-1}&page_items={page_items}"
pagination["prev"] = (
f"/posts?sort={sort.value}&order={order.value}"
f"&page={page-1}&page_items={page_items}&tag={tag}"
)
else:
pagination["prev"] = None

if end_index >= len(posts):
pagination["next"] = None
else:
pagination["next"] = f"/posts?sort={sort.value}&order={order.value}&page={page+1}&page_items={page_items}"
pagination["next"] = (
f"/posts?sort={sort.value}&order={order.value}"
f"&page={page+1}&page_items={page_items}&tag={tag}"
)

# Total pages
if len(posts) <= page_items:

0 comments on commit 171f90b

Please sign in to comment.