2
2
Collections API
3
3
"""
4
4
5
+ # pylint: disable=too-many-lines
6
+
5
7
from collections import Counter
6
8
from uuid import UUID , uuid4
7
9
from typing import Optional , List , TYPE_CHECKING , cast , Dict , Tuple , Any , Union
@@ -210,11 +212,11 @@ async def remove_crawls_from_collection(
210
212
return await self .get_collection_out (coll_id , org )
211
213
212
214
async def get_collection_raw (
213
- self , coll_id : UUID , public_only : bool = False
215
+ self , coll_id : UUID , public_or_unlisted_only : bool = False
214
216
) -> Dict [str , Any ]:
215
217
"""Get collection by id as dict from database"""
216
218
query : dict [str , object ] = {"_id" : coll_id }
217
- if public_only :
219
+ if public_or_unlisted_only :
218
220
query ["access" ] = {"$in" : ["public" , "unlisted" ]}
219
221
220
222
result = await self .collections .find_one (query )
@@ -224,17 +226,21 @@ async def get_collection_raw(
224
226
return result
225
227
226
228
async def get_collection (
227
- self , coll_id : UUID , public_only : bool = False
229
+ self , coll_id : UUID , public_or_unlisted_only : bool = False
228
230
) -> Collection :
229
231
"""Get collection by id"""
230
- result = await self .get_collection_raw (coll_id , public_only )
232
+ result = await self .get_collection_raw (coll_id , public_or_unlisted_only )
231
233
return Collection .from_dict (result )
232
234
233
235
async def get_collection_out (
234
- self , coll_id : UUID , org : Organization , resources = False , public_only = False
236
+ self ,
237
+ coll_id : UUID ,
238
+ org : Organization ,
239
+ resources = False ,
240
+ public_or_unlisted_only = False ,
235
241
) -> CollOut :
236
242
"""Get CollOut by id"""
237
- result = await self .get_collection_raw (coll_id , public_only )
243
+ result = await self .get_collection_raw (coll_id , public_or_unlisted_only )
238
244
239
245
if resources :
240
246
result ["resources" ] = await self .get_collection_crawl_resources (coll_id )
@@ -248,6 +254,26 @@ async def get_collection_out(
248
254
249
255
return CollOut .from_dict (result )
250
256
257
+ async def get_public_collection_out (
258
+ self , coll_id : UUID , org : Organization
259
+ ) -> PublicCollOut :
260
+ """Get PublicCollOut by id"""
261
+ result = await self .get_collection_raw (coll_id )
262
+
263
+ if result .get ("access" ) != "public" :
264
+ raise HTTPException (status_code = 404 , detail = "collection_not_found" )
265
+
266
+ result ["resources" ] = await self .get_collection_crawl_resources (coll_id )
267
+
268
+ thumbnail = result .get ("thumbnail" )
269
+ if thumbnail :
270
+ image_file = ImageFile (** thumbnail )
271
+ result ["thumbnail" ] = await image_file .get_public_image_file_out (
272
+ org , self .storage_ops
273
+ )
274
+
275
+ return PublicCollOut .from_dict (result )
276
+
251
277
async def list_collections (
252
278
self ,
253
279
org : Organization ,
@@ -825,7 +851,7 @@ async def get_collection_public_replay(
825
851
org : Organization = Depends (org_public ),
826
852
):
827
853
coll = await colls .get_collection_out (
828
- coll_id , org , resources = True , public_only = True
854
+ coll_id , org , resources = True , public_or_unlisted_only = True
829
855
)
830
856
response .headers ["Access-Control-Allow-Origin" ] = "*"
831
857
response .headers ["Access-Control-Allow-Headers" ] = "*"
@@ -920,6 +946,24 @@ async def get_org_public_collections(
920
946
sort_direction = sortDirection ,
921
947
)
922
948
949
+ @app .get (
950
+ "/public-collections/{org_slug}/collections/{coll_id}" ,
951
+ tags = ["collections" ],
952
+ response_model = PublicCollOut ,
953
+ )
954
+ async def get_public_collection (
955
+ org_slug : str ,
956
+ coll_id : UUID ,
957
+ ):
958
+ try :
959
+ org = await colls .orgs .get_org_by_slug (org_slug )
960
+ # pylint: disable=broad-exception-caught
961
+ except Exception :
962
+ # pylint: disable=raise-missing-from
963
+ raise HTTPException (status_code = 404 , detail = "collection_not_found" )
964
+
965
+ return await colls .get_public_collection_out (coll_id , org )
966
+
923
967
@app .get (
924
968
"/orgs/{oid}/collections/{coll_id}/urls" ,
925
969
tags = ["collections" ],
0 commit comments