Skip to content

Commit d4e0692

Browse files
mein Namealphatownsman
authored andcommitted
add api to get a collection and review with known id
1 parent 30c5068 commit d4e0692

File tree

3 files changed

+71
-6
lines changed

3 files changed

+71
-6
lines changed

journal/apis/collection.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
class CollectionSchema(Schema):
1818
uuid: str
1919
url: str
20+
api_url: str
2021
visibility: int = Field(ge=0, le=2)
2122
post_id: int | None = Field(alias="latest_post_id")
2223
created_time: datetime
@@ -50,7 +51,7 @@ class CollectionItemInSchema(Schema):
5051
tags=["collection"],
5152
)
5253
@paginate(PageNumberPagination)
53-
def list_collections(request):
54+
def list_user_collections(request):
5455
"""
5556
Get collections created by current user
5657
"""
@@ -63,7 +64,7 @@ def list_collections(request):
6364
response={200: CollectionSchema, 401: Result, 403: Result, 404: Result},
6465
tags=["collection"],
6566
)
66-
def get_collection(request, collection_uuid: str):
67+
def get_user_collection(request, collection_uuid: str):
6768
"""
6869
Get collections by its uuid
6970
"""
@@ -75,6 +76,48 @@ def get_collection(request, collection_uuid: str):
7576
return c
7677

7778

79+
@api.get(
80+
"/collection/{collection_uuid}",
81+
response={200: CollectionSchema, 401: Result, 403: Result, 404: Result},
82+
tags=["collection"],
83+
auth=None,
84+
)
85+
def get_collection(request, collection_uuid: str):
86+
"""
87+
Get details of a collection
88+
"""
89+
c = Collection.get_by_url(collection_uuid)
90+
if not c:
91+
return 404, {"message": "Collection not found"}
92+
if not c.is_visible_to(request.user):
93+
return 403, {"message": "Permission denied"}
94+
return c
95+
96+
97+
@api.get(
98+
"/collection/{collection_uuid}/item/",
99+
response={200: List[CollectionItemSchema], 401: Result, 403: Result, 404: Result},
100+
tags=["collection"],
101+
auth=None,
102+
)
103+
@paginate(PageNumberPagination)
104+
def collection_list_items(request, collection_uuid: str):
105+
"""
106+
Get items in a collection collections
107+
"""
108+
c = Collection.get_by_url(collection_uuid)
109+
if not c:
110+
return 404, {"message": "Collection not found"}
111+
if not c.is_visible_to(request.user):
112+
return 403, {"message": "Permission denied"}
113+
if c.is_dynamic:
114+
items = c.query_result.items if c.query_result else []
115+
members = [{"item": i, "note": ""} for i in items]
116+
return members
117+
else:
118+
return c.ordered_members
119+
120+
78121
@api.post(
79122
"/me/collection/",
80123
response={200: CollectionSchema, 401: Result, 403: Result, 404: Result},
@@ -140,7 +183,7 @@ def delete_collection(request, collection_uuid: str):
140183
tags=["collection"],
141184
)
142185
@paginate(PageNumberPagination)
143-
def collection_list_items(request, collection_uuid: str):
186+
def user_collection_list_items(request, collection_uuid: str):
144187
"""
145188
Get items in a collection collections
146189
"""

journal/apis/review.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
class ReviewSchema(Schema):
1818
url: str
19+
api_url: str
1920
visibility: int = Field(ge=0, le=2)
2021
post_id: int | None = Field(alias="latest_post_id")
2122
item: ItemSchema
@@ -113,3 +114,24 @@ def delete_review(request, item_uuid: str):
113114
return 404, {"message": "Item not found"}
114115
Review.update_item_review(item, request.user.identity, None, None)
115116
return 200, {"message": "OK"}
117+
118+
119+
@api.get(
120+
"/review/{review_uuid}",
121+
response={200: ReviewSchema, 401: Result, 403: Result, 404: Result},
122+
tags=["review"],
123+
auth=None,
124+
)
125+
def get_any_review(request, review_uuid: str):
126+
"""
127+
Get a review by its uuid with permission checks.
128+
129+
Returns the review if it is visible to the requesting user based on
130+
its visibility and the relationship to the owner; otherwise 403.
131+
"""
132+
r = Review.get_by_url(review_uuid)
133+
if not r:
134+
return 404, {"message": "Review not found"}
135+
if not r.is_visible_to(request.user):
136+
return 403, {"message": "Permission denied"}
137+
return r

journal/models/mixins.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,15 @@ class UserOwnedObjectMixin:
2525

2626
def is_visible_to(
2727
self: "Piece", # type: ignore
28-
viewing_user: "User",
28+
viewing_user: "User | None",
2929
) -> bool:
3030
owner = self.owner
3131
if not owner or not owner.is_active:
3232
return False
3333
if owner.user == viewing_user:
3434
return True
35-
if not viewing_user.is_authenticated:
36-
return self.visibility == 0
35+
if not viewing_user or not viewing_user.is_authenticated:
36+
return self.visibility == 0 and owner.anonymous_viewable
3737
viewer = viewing_user.identity
3838
if not viewer:
3939
return False

0 commit comments

Comments
 (0)