From a887d93c8fcb09fa51600debd8f91652671b3e03 Mon Sep 17 00:00:00 2001 From: Michiel Scholten Date: Fri, 12 Sep 2025 23:01:35 +0200 Subject: [PATCH] Cleanups --- src/digimarks/main.py | 14 +++++++------- src/digimarks/tags_helpers.py | 6 ++++-- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/src/digimarks/main.py b/src/digimarks/main.py index ae893cb..86c25c0 100644 --- a/src/digimarks/main.py +++ b/src/digimarks/main.py @@ -148,7 +148,8 @@ async def list_users( if system_key != settings.system_key: raise HTTPException(status_code=404) - users = session.exec(select(User).offset(offset).limit(limit)).all() + result = await session.exec(select(User).offset(offset).limit(limit)) + users = result.all() return users @@ -159,7 +160,7 @@ async def list_bookmarks( offset: int = 0, limit: Annotated[int, Query(le=10000)] = 100, ) -> list[Bookmark]: - """List all bookmarks in the database. By default 100 items are returned.""" + """List all bookmarks in the database. By default, 100 items are returned.""" result = await session.exec( select(Bookmark) .where(Bookmark.user_key == user_key, Bookmark.status != Visibility.DELETED) @@ -183,7 +184,6 @@ async def get_bookmark( ) ) bookmark = result.first() - # bookmark = session.get(Bookmark, {'url_hash': url_hash, 'userkey': user_key}) return bookmark @@ -279,13 +279,13 @@ async def delete_bookmark( url_hash: str, ): """(Soft)Delete bookmark `bookmark_key` for user `user_key`.""" - result = await session.get(Bookmark, {'url_hash': url_hash, 'userkey': user_key}) + result = await session.get(Bookmark, {'url_hash': url_hash, 'user_key': user_key}) bookmark = result if not bookmark: raise HTTPException(status_code=404, detail='Bookmark not found') bookmark.deleted_date = datetime.now(UTC) bookmark.status = Visibility.DELETED - await session.add(bookmark) + session.add(bookmark) await session.commit() return {'ok': True} @@ -332,11 +332,11 @@ async def list_tags_for_user( tags = [] for bookmark in bookmarks: tags += bookmark.tag_list - return tags.clean_tags(tags) + return tags_helpers.clean_tags(tags) @app.get('/api/v1/{user_key}/tags/{tag_key}') -async def list_tags_for_user( +async def list_bookmarks_for_tag_for_user( session: SessionDep, user_key: str, ) -> list[str]: diff --git a/src/digimarks/tags_helpers.py b/src/digimarks/tags_helpers.py index 22cd0b0..ad8fca4 100644 --- a/src/digimarks/tags_helpers.py +++ b/src/digimarks/tags_helpers.py @@ -1,5 +1,7 @@ """Helper functions for tags used with Bookmark models.""" +from sqlalchemy import Sequence + from src.digimarks.models import Bookmark @@ -35,7 +37,7 @@ def unique_ever_seen(iterable, key=None): yield element -def clean_tags(tags_list: list) -> list: +def clean_tags(tags_list: list) -> list[str]: """Generate a unique list of the tags. :param list tags_list: List with all tags @@ -50,7 +52,7 @@ def clean_tags(tags_list: list) -> list: return tags_res -def list_tags_for_bookmarks(bookmarks: list) -> list: +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: