33
44import uuid
55
6- from typing import Annotated , Any , Tuple
6+ from typing import Annotated , Any , Tuple , Dict
77from fastapi import APIRouter , Depends , HTTPException , status
88from pydantic import BaseModel
99from sqlalchemy import and_ , update
3434 tags = ["data" ],
3535)
3636
37- # the following fields are not allowed to be changed by any user or caller. They are programmatically set.
38- IMMUTABLE_BATCH_FIELDS = [
39- "batch_id" ,
40- "inst_id" ,
41- "creator" ,
42- "created_date" ,
43- "deletion_request_time" ,
44- ]
45-
4637
4738class BatchCreationRequest (BaseModel ):
4839 """The Batch creation request."""
@@ -55,6 +46,9 @@ class BatchCreationRequest(BaseModel):
5546 # You can specify files to include as ids or names.
5647 file_ids : set [str ] | None = None
5748 file_names : set [str ] | None = None
49+ completed : bool | None = None
50+ # Set this to set this batch for deletion.
51+ deleted : bool = False
5852
5953
6054class BatchInfo (BaseModel ):
@@ -63,7 +57,7 @@ class BatchInfo(BaseModel):
6357 # In order to allow PATCH commands, each field must be marked as nullable.
6458 batch_id : str | None = None
6559 inst_id : str | None = None
66- file_ids : set [ str ] = {}
60+ file_names_to_ids : Dict [ str , str ] = {}
6761 # Must be unique within an institution to avoid confusion
6862 name : str | None = None
6963 description : str | None = None
@@ -219,7 +213,7 @@ def get_all_batches(
219213 "inst_id" : uuid_to_str (elem .inst_id ),
220214 "name" : elem .name ,
221215 "description" : elem .description ,
222- "file_ids " : uuids_to_strs ( elem .files ) ,
216+ "file_names_to_ids " : { x . name : uuid_to_str ( x . id ) for x in elem .files } ,
223217 "creator" : uuid_to_str (elem .creator ),
224218 "deleted" : False if elem .deleted is None else elem .deleted ,
225219 "completed" : False if elem .completed is None else elem .completed ,
@@ -355,7 +349,7 @@ def read_batch_info(
355349 "inst_id" : uuid_to_str (res .inst_id ),
356350 "name" : res .name ,
357351 "description" : res .description ,
358- "file_ids " : uuids_to_strs ( res .files ) ,
352+ "file_names_to_ids " : { x . name : uuid_to_str ( x . id ) for x in res .files } ,
359353 "creator" : uuid_to_str (res .creator ),
360354 "deleted" : False if res .deleted is None else res .deleted ,
361355 "completed" : False if res .completed is None else res .completed ,
@@ -512,7 +506,9 @@ def create_batch(
512506 "inst_id" : uuid_to_str (query_result [0 ][0 ].inst_id ),
513507 "name" : query_result [0 ][0 ].name ,
514508 "description" : query_result [0 ][0 ].description ,
515- "file_ids" : uuids_to_strs (query_result [0 ][0 ].files ),
509+ "file_names_to_ids" : {
510+ x .name : uuid_to_str (x .id ) for x in query_result [0 ][0 ].files
511+ },
516512 "creator" : uuid_to_str (query_result [0 ][0 ].creator ),
517513 "deleted" : False ,
518514 "completed" : False ,
@@ -540,7 +536,7 @@ def construct_modify_query(modify_vals: dict, batch_id: str) -> Any:
540536def update_batch (
541537 inst_id : str ,
542538 batch_id : str ,
543- request : BatchInfo ,
539+ request : BatchCreationRequest ,
544540 current_user : Annotated [BaseUser , Depends (get_current_active_user )],
545541 sql_session : Annotated [Session , Depends (get_session )],
546542) -> Any :
@@ -553,11 +549,7 @@ def update_batch(
553549 model_owner_and_higher_or_err (current_user , "modify batch" )
554550
555551 update_data = request .model_dump (exclude_unset = True )
556- if [key for key in IMMUTABLE_BATCH_FIELDS if key in update_data ] != []:
557- raise HTTPException (
558- status_code = status .HTTP_401_UNAUTHORIZED ,
559- detail = "Immutable fields included in modify request." ,
560- )
552+ print ("aaaaaaaaaaaaaaaaaaaaaaaa:" + str (update_data ))
561553 local_session .set (sql_session )
562554 # Check that the batch exists.
563555 query_result = (
@@ -588,8 +580,10 @@ def update_batch(
588580 status_code = status .HTTP_401_UNAUTHORIZED ,
589581 detail = "Batch is set for deletion, no modifications allowed." ,
590582 )
591- if "file_ids" in update_data :
583+ if "file_ids" in update_data or "file_names" in update_data :
592584 existing_batch .files .clear ()
585+
586+ if "file_ids" in update_data :
593587 for f in strs_to_uuids (update_data ["file_ids" ]):
594588 # Check that the files requested for this batch exists
595589 query_result_file = (
@@ -615,7 +609,32 @@ def update_batch(
615609 detail = "Multiple files in request with same unique id found." ,
616610 )
617611 existing_batch .files .add (query_result_file [0 ][0 ])
618- # The below is unfortunate but it doesn't seem to work if we programmatically construct the query using values.
612+ if "file_names" in update_data :
613+ for f in update_data ["file_names" ]:
614+ # Check that the files requested for this batch exists
615+ query_result_file = (
616+ local_session .get ()
617+ .execute (
618+ select (FileTable ).where (
619+ and_ (
620+ FileTable .name == f ,
621+ FileTable .inst_id == str_to_uuid (inst_id ),
622+ )
623+ )
624+ )
625+ .all ()
626+ )
627+ if not query_result_file or len (query_result_file ) == 0 :
628+ raise HTTPException (
629+ status_code = status .HTTP_404_NOT_FOUND ,
630+ detail = "file in request not found." ,
631+ )
632+ elif len (query_result_file ) > 1 :
633+ raise HTTPException (
634+ status_code = status .HTTP_500_INTERNAL_SERVER_ERROR ,
635+ detail = "Multiple files in request with same unique id found." ,
636+ )
637+ existing_batch .files .add (query_result_file [0 ][0 ])
619638
620639 if "name" in update_data :
621640 existing_batch .name = update_data ["name" ]
@@ -645,7 +664,7 @@ def update_batch(
645664 "inst_id" : uuid_to_str (res [0 ][0 ].inst_id ),
646665 "name" : res [0 ][0 ].name ,
647666 "description" : res [0 ][0 ].description ,
648- "file_ids " : uuids_to_strs ( res [0 ][0 ].files ) ,
667+ "file_names_to_ids " : { x . name : uuid_to_str ( x . id ) for x in res [0 ][0 ].files } ,
649668 "creator" : uuid_to_str (res [0 ][0 ].creator ),
650669 "deleted" : res [0 ][0 ].deleted ,
651670 "completed" : res [0 ][0 ].completed ,
0 commit comments