From 533b2787bb1ac716a9ba4c670d675432b84b3cec Mon Sep 17 00:00:00 2001 From: Tom Hacohen Date: Sun, 27 Dec 2020 18:34:40 +0200 Subject: [PATCH] Implement item revisions. --- etebase_fastapi/collection.py | 43 +++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/etebase_fastapi/collection.py b/etebase_fastapi/collection.py index 2f8d1ed..8eabf9a 100644 --- a/etebase_fastapi/collection.py +++ b/etebase_fastapi/collection.py @@ -136,6 +136,12 @@ class CollectionItemListResponse(BaseModel): done: bool +class CollectionItemRevisionListResponse(BaseModel): + data: t.List[CollectionItemRevisionInOut] + iterator: t.Optional[str] + done: bool + + class CollectionItemBulkGetIn(BaseModel): uid: str etag: t.Optional[str] @@ -422,6 +428,43 @@ def item_bulk_common(data: ItemBatchIn, user: User, stoken: t.Optional[str], uid return MsgpackResponse({}) +@collection_router.get("/{collection_uid}/item/{uid}/revision/") +def item_revisions( + collection_uid: str, + uid: str, + limit: int = 50, + iterator: t.Optional[str] = None, + prefetch: Prefetch = PrefetchQuery, + user: User = Depends(get_authenticated_user), +): + _, items = get_item_queryset(user, collection_uid) + item = get_object_or_404(items, uid=uid) + + queryset = item.revisions.order_by("-id") + + if iterator is not None: + iterator_obj = get_object_or_404(queryset, uid=iterator) + queryset = queryset.filter(id__lt=iterator_obj.id) + + result = list(queryset[: limit + 1]) + if len(result) < limit + 1: + done = True + else: + done = False + result = result[:-1] + + context = Context(user, prefetch) + ret_data = [CollectionItemRevisionInOut.from_orm_context(revision, context) for revision in result] + iterator = ret_data[-1].uid if len(result) > 0 else None + + ret = CollectionItemRevisionListResponse( + data=ret_data, + iterator=iterator, + done=done, + ) + return MsgpackResponse(ret) + + @collection_router.post("/{collection_uid}/item/fetch_updates/") def fetch_updates( collection_uid: str,