Skip to content

Commit dd102f3

Browse files
committed
commit data changes to include file names
1 parent 05715dd commit dd102f3

File tree

2 files changed

+68
-38
lines changed

2 files changed

+68
-38
lines changed

src/webapp/routers/data.py

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
import uuid
55

6-
from typing import Annotated, Any, Tuple
6+
from typing import Annotated, Any, Tuple, Dict
77
from fastapi import APIRouter, Depends, HTTPException, status
88
from pydantic import BaseModel
99
from sqlalchemy import and_, update
@@ -34,15 +34,6 @@
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

4738
class 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

6054
class 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:
540536
def 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,

src/webapp/routers/data_test.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ def same_orderless(a: DataOverview, b: DataOverview):
7777
found = True
7878
if (
7979
a_elem["inst_id"] != b_elem["inst_id"]
80-
or counter_repr(a_elem["file_ids"]) != counter_repr(b_elem["file_ids"])
80+
or a_elem["file_names_to_ids"] == b_elem["file_names_to_ids"]
8181
or a_elem["name"] != b_elem["name"]
8282
or a_elem["description"] != b_elem["description"]
8383
or a_elem["creator"] != b_elem["creator"]
@@ -217,9 +217,9 @@ def test_read_inst_all_input_files(client: TestClient):
217217
{
218218
"batch_id": "5b2420f3103546ab90eb74d5df97de43",
219219
"inst_id": "1d7c75c33eda42949c6675ea8af97b55",
220-
"file_ids": [
221-
"f0bb3a206d924254afed6a72f43c562a",
222-
"fbe67a2e50e040c7b7b807043cb813a5",
220+
"file_names_to_ids": [
221+
{"file_input_one": "f0bb3a206d924254afed6a72f43c562a"},
222+
{"file_output_one": "fbe67a2e50e040c7b7b807043cb813a5"},
223223
],
224224
"name": "batch_foo",
225225
"description": None,
@@ -287,9 +287,9 @@ def test_read_inst_all_output_files(client: TestClient):
287287
{
288288
"batch_id": "5b2420f3103546ab90eb74d5df97de43",
289289
"inst_id": "1d7c75c33eda42949c6675ea8af97b55",
290-
"file_ids": [
291-
"fbe67a2e50e040c7b7b807043cb813a5",
292-
"f0bb3a206d924254afed6a72f43c562a",
290+
"file_names_to_ids": [
291+
{"file_input_one": "f0bb3a206d924254afed6a72f43c562a"},
292+
{"file_output_one": "fbe67a2e50e040c7b7b807043cb813a5"},
293293
],
294294
"name": "batch_foo",
295295
"description": None,
@@ -350,9 +350,9 @@ def test_read_batch_info(client: TestClient):
350350
{
351351
"batch_id": "5b2420f3103546ab90eb74d5df97de43",
352352
"inst_id": "1d7c75c33eda42949c6675ea8af97b55",
353-
"file_ids": [
354-
"f0bb3a206d924254afed6a72f43c562a",
355-
"fbe67a2e50e040c7b7b807043cb813a5",
353+
"file_names_to_ids": [
354+
{"file_input_one": "f0bb3a206d924254afed6a72f43c562a"},
355+
{"file_output_one": "fbe67a2e50e040c7b7b807043cb813a5"},
356356
],
357357
"name": "batch_foo",
358358
"description": None,
@@ -440,6 +440,7 @@ def test_read_file_id_info(client: TestClient):
440440
},
441441
)
442442

443+
443444
# TODO: xxx add more test cases including sst generated = true etc.
444445
def test_create_batch(client: TestClient):
445446
"""Test POST /institutions/<uuid>/batch."""
@@ -471,9 +472,17 @@ def test_create_batch(client: TestClient):
471472
assert response.json()["completed"] == False
472473
assert response.json()["deletion_request_time"] == None
473474
assert response.json()["inst_id"] == uuid_to_str(USER_VALID_INST_UUID)
474-
assert uuid_to_str(FILE_UUID_2) in response.json()["file_ids"]
475-
assert uuid_to_str(FILE_UUID_1) in response.json()["file_ids"]
476-
assert len(response.json()["file_ids"]) == 2
475+
assert "file_input_two" in response.json()["file_names_to_ids"]
476+
assert (
477+
uuid_to_str(FILE_UUID_2)
478+
in response.json()["file_names_to_ids"]["file_input_two"]
479+
)
480+
assert "file_input_one" in response.json()["file_names_to_ids"]
481+
assert (
482+
uuid_to_str(FILE_UUID_1)
483+
in response.json()["file_names_to_ids"]["file_input_one"]
484+
)
485+
assert len(response.json()["file_names_to_ids"]) == 2
477486

478487

479488
def test_update_batch(client: TestClient):
@@ -498,7 +507,7 @@ def test_update_batch(client: TestClient):
498507
+ uuid_to_str(BATCH_UUID),
499508
json={
500509
"name": "batch_name_updated_foo",
501-
"completed": "True",
510+
"completed": True,
502511
"file_ids": [uuid_to_str(FILE_UUID_2)],
503512
},
504513
)
@@ -510,7 +519,9 @@ def test_update_batch(client: TestClient):
510519
assert response.json()["completed"] == True
511520
assert response.json()["deletion_request_time"] == None
512521
assert response.json()["inst_id"] == uuid_to_str(USER_VALID_INST_UUID)
513-
assert response.json()["file_ids"] == [uuid_to_str(FILE_UUID_2)]
522+
assert response.json()["file_names_to_ids"] == {
523+
"file_input_two": uuid_to_str(FILE_UUID_2)
524+
}
514525

515526

516527
def test_validate_success_batch(client: TestClient):

0 commit comments

Comments
 (0)