33# Copyright (C) 2015-2020 Picheral, Colin, Irisson (UPMC-CNRS)
44#
55from typing import List , Union , Optional , Tuple , Dict , Any
6-
6+ from fastapi import HTTPException
77from API_models .crud import CreateCollectionReq , CollectionAggregatedRsp
88from API_models .exports import TaxonomyRecast
99from BO .Collection import CollectionBO , CollectionIDT
@@ -38,20 +38,23 @@ def create(
3838 coll_id = CollectionBO .create (self .session , req .title , req .project_ids )
3939 return coll_id
4040
41- def _check_access (
42- self , a_coll : CollectionBO , user_id : UserIDT
43- ) -> Optional [CollectionBO ]:
41+ def update (
42+ self ,
43+ current_user_id : UserIDT ,
44+ collection_id : CollectionIDT ,
45+ req : Dict [str , Any ],
46+ ):
4447 """
45- Quick & dirty access check by catching the exception .
48+ Update a collection .
4649 """
47- try :
50+ if "project_ids" in req :
4851 PermissionConsistentProjectSet (
49- self .ro_session ,
50- a_coll . project_ids , # Need the R/W session here, as the projects MRU is written to. TODO
51- ). can_be_administered_by ( user_id , update_preference = False )
52- except AssertionError :
53- return None
54- return a_coll
52+ self .session , req [ "project_ids" ]
53+ ). can_be_administered_by ( current_user_id )
54+ present_collection = self . query ( current_user_id , collection_id , for_update = True )
55+ if present_collection is None :
56+ raise HTTPException ( status_code = 404 , detail = "Collection not found" )
57+ present_collection . update ( session = self . session , collection_update = req )
5558
5659 def list (
5760 self , current_user_id : UserIDT , collection_ids : Optional [str ] = None
@@ -65,11 +68,11 @@ def list(
6568 for a_rec in qry :
6669 coll_bo = CollectionBO (a_rec )
6770 coll_bo ._read_composing_projects ()
68- checked = self ._check_access (coll_bo , current_user_id )
71+ checked = self ._check_access (current_user_id , coll_bo . project_ids )
6972 if checked is None :
7073 continue
71- checked .enrich ()
72- ret .append (checked )
74+ coll_bo .enrich ()
75+ ret .append (coll_bo )
7376 return ret
7477
7578 def search (self , current_user_id : UserIDT , title : str ) -> List [CollectionBO ]:
@@ -78,11 +81,11 @@ def search(self, current_user_id: UserIDT, title: str) -> List[CollectionBO]:
7881 for a_rec in qry :
7982 coll_bo = CollectionBO (a_rec )
8083 coll_bo ._read_composing_projects ()
81- checked = self ._check_access (coll_bo , current_user_id )
84+ checked = self ._check_access (current_user_id , coll_bo . project_ids )
8285 if checked is None :
8386 continue
84- checked .enrich ()
85- ret .append (checked )
87+ coll_bo .enrich ()
88+ ret .append (coll_bo )
8689 return ret
8790
8891 def query (
@@ -93,7 +96,10 @@ def query(
9396 )
9497 if ret is None :
9598 return ret
96- return self ._check_access (ret , current_user_id )
99+ check = self ._check_access (current_user_id , ret .project_ids )
100+ if check is None :
101+ return None
102+ return ret
97103
98104 def query_by_title (self , title : str ) -> CollectionBO :
99105 # Return a unique collection from its title
@@ -151,6 +157,21 @@ def _query_recast_for_coll(self, coll_id: CollectionIDT):
151157 )
152158 return qry
153159
160+ def _check_access (
161+ self , user_id : UserIDT , project_ids : ProjectIDListT
162+ ) -> Optional [ProjectIDListT ]:
163+ """
164+ Quick & dirty access check by catching the exception.
165+ """
166+ try :
167+ PermissionConsistentProjectSet (
168+ self .ro_session ,
169+ project_ids , # Need the R/W session here, as the projects MRU is written to. TODO
170+ ).can_be_administered_by (user_id , update_preference = False )
171+ except AssertionError :
172+ return None
173+ return project_ids
174+
154175 def aggregated_from_projects (
155176 self ,
156177 current_user_id : UserIDT ,
0 commit comments