mirror of
https://github.com/aquatix/digimarks.git
synced 2025-12-06 19:45:12 +01:00
Refactoring tags endpoints to use tags_service
This commit is contained in:
@@ -288,25 +288,18 @@ async def list_tags_for_user(
|
||||
user_key: str,
|
||||
) -> list[str]:
|
||||
"""List all tags in use by the user."""
|
||||
result = await session.exec(
|
||||
select(Bookmark).where(Bookmark.user_key == user_key, Bookmark.status != Visibility.DELETED)
|
||||
)
|
||||
bookmarks = result.all()
|
||||
tags = []
|
||||
for bookmark in bookmarks:
|
||||
tags += bookmark.tag_list
|
||||
return tags_service.clean_tags(tags)
|
||||
return await tags_service.list_tags_for_user(session, user_key)
|
||||
|
||||
|
||||
@app.get('/api/v1/{user_key}/tags/{tag_key}')
|
||||
async def list_bookmarks_for_tag_for_user(
|
||||
session: SessionDep,
|
||||
user_key: str,
|
||||
tag_key: str,
|
||||
) -> list[str]:
|
||||
"""List all tags in use by the user."""
|
||||
result = await session.exec(select(Bookmark).where(Bookmark.user_key == user_key))
|
||||
bookmarks = result.all()
|
||||
return tags_service.list_tags_for_bookmarks(bookmarks)
|
||||
logger.info('List bookmarks for tag %s user %s', user_key)
|
||||
return await tags_service.list_bookmarks_for_tag_for_user(session, user_key, tag_key)
|
||||
|
||||
|
||||
@app.get('/{user_key}', response_class=HTMLResponse)
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
"""Helper functions for tags used with Bookmark models."""
|
||||
|
||||
from sqlalchemy import Sequence
|
||||
from sqlmodel import select
|
||||
|
||||
from src.digimarks.models import Bookmark
|
||||
from src.digimarks.models import Bookmark, Visibility
|
||||
|
||||
|
||||
def i_filter_false(predicate, iterable):
|
||||
@@ -56,7 +57,7 @@ def list_tags_for_bookmarks(bookmarks: Sequence[Bookmark]) -> list[str]:
|
||||
"""Generate a unique list of the tags from the list of bookmarks."""
|
||||
tags = []
|
||||
for bookmark in bookmarks:
|
||||
tags += bookmark.tags_list
|
||||
tags += bookmark.tag_list
|
||||
return clean_tags(tags)
|
||||
|
||||
|
||||
@@ -69,3 +70,30 @@ def set_tags(bookmark: Bookmark, new_tags: str) -> None:
|
||||
tags_split = new_tags.split(',')
|
||||
tags_clean = clean_tags(tags_split)
|
||||
bookmark.tags = ','.join(tags_clean)
|
||||
|
||||
|
||||
async def list_tags_for_user(
|
||||
session,
|
||||
user_key: str,
|
||||
) -> list[str]:
|
||||
"""List all tags in use by the user."""
|
||||
result = await session.exec(
|
||||
select(Bookmark).where(Bookmark.user_key == user_key, Bookmark.status != Visibility.DELETED)
|
||||
)
|
||||
bookmarks = result.all()
|
||||
tags = []
|
||||
for bookmark in bookmarks:
|
||||
tags += bookmark.tag_list
|
||||
return clean_tags(tags)
|
||||
|
||||
|
||||
async def list_bookmarks_for_tag_for_user(
|
||||
session,
|
||||
user_key: str,
|
||||
tag_key: str,
|
||||
) -> list[str]:
|
||||
"""List all tags in use by the user."""
|
||||
result = await session.exec(select(Bookmark).where(Bookmark.user_key == user_key))
|
||||
# TODO: filter on tag_key
|
||||
bookmarks = result.all()
|
||||
return list_tags_for_bookmarks(bookmarks)
|
||||
|
||||
Reference in New Issue
Block a user