Skip to content

Commit 7404160

Browse files
committed
Fix edit file metadata bug by making sure
that the backend properly sets context when a metatdata definition is provided.
1 parent 1712540 commit 7404160

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

backend/app/models/metadata.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -307,9 +307,11 @@ async def validate_context(
307307
detail="Context is required",
308308
)
309309
if context is not None:
310-
pass
310+
# TODO validate context
311+
return content
311312
if context_url is not None:
312-
pass
313+
# TODO validate context
314+
return content
313315
if definition is not None:
314316
if (
315317
md_def := await MetadataDefinitionDB.find_one(
@@ -322,7 +324,7 @@ async def validate_context(
322324
status_code=400,
323325
detail=f"{definition} is not valid metadata definition",
324326
)
325-
return content
327+
return content
326328

327329

328330
def deep_update(orig: dict, new: dict):

backend/app/routers/metadata_datasets.py

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
from app.search.connect import delete_document_by_id
2424
from app.search.index import index_dataset
2525
from beanie import PydanticObjectId
26-
from bson import ObjectId
2726
from elasticsearch import Elasticsearch
2827
from fastapi import APIRouter, Depends, Form, HTTPException
2928

@@ -105,6 +104,16 @@ async def add_dataset_metadata(
105104
f"Metadata for {metadata_in.definition} already exists on this dataset",
106105
)
107106

107+
# lookup json_ld context in metadata definition
108+
if metadata_in.definition is not None:
109+
if (
110+
definition := await MetadataDefinitionDB.find_one(
111+
MetadataDefinitionDB.name == metadata_in.definition
112+
)
113+
) is not None:
114+
metadata_in.context = definition.context
115+
metadata_in.context_url = definition.context_url
116+
108117
md = await _build_metadata_db_obj(
109118
metadata_in, DatasetOut(**dataset.dict()), user
110119
)

backend/app/routers/metadata_files.py

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
11
from typing import List, Optional
22

3-
from beanie import PydanticObjectId
4-
from beanie.operators import Or
5-
from bson import ObjectId
6-
from elasticsearch import Elasticsearch
7-
from fastapi import APIRouter, Depends, Form, HTTPException
8-
93
from app import dependencies
104
from app.config import settings
115
from app.deps.authorization_deps import FileAuthorization
@@ -28,6 +22,11 @@
2822
)
2923
from app.search.connect import delete_document_by_id
3024
from app.search.index import index_file
25+
from beanie import PydanticObjectId
26+
from beanie.operators import Or
27+
from bson import ObjectId
28+
from elasticsearch import Elasticsearch
29+
from fastapi import APIRouter, Depends, Form, HTTPException
3130

3231
router = APIRouter()
3332

@@ -145,6 +144,16 @@ async def add_file_metadata(
145144
409, f"Metadata for {definition} already exists on this file"
146145
)
147146

147+
# lookup json_ld context in metadata definition
148+
if metadata_in.definition is not None:
149+
if (
150+
definition := await MetadataDefinitionDB.find_one(
151+
MetadataDefinitionDB.name == metadata_in.definition
152+
)
153+
) is not None:
154+
metadata_in.context = definition.context
155+
metadata_in.context_url = definition.context_url
156+
148157
md = await _build_metadata_db_obj(metadata_in, FileOut(**file.dict()), user)
149158
await md.insert()
150159

@@ -210,19 +219,31 @@ async def replace_file_metadata(
210219

211220
if (orig_md := await MetadataDB.find_one(*query)) is not None:
212221
# Metadata exists, so prepare the new document we are going to replace it with
213-
md = await _build_metadata_db_obj(
222+
223+
# lookup json_ld context in metadata definition
224+
if metadata_in.definition is not None:
225+
if (
226+
definition := await MetadataDefinitionDB.find_one(
227+
MetadataDefinitionDB.name == metadata_in.definition
228+
)
229+
) is not None:
230+
metadata_in.context = definition.context
231+
metadata_in.context_url = definition.context_url
232+
233+
new_md = await _build_metadata_db_obj(
214234
metadata_in,
215235
FileOut(**file.dict()),
216236
user,
217237
agent=agent,
218238
version=target_version,
219239
)
220-
md.id = orig_md.id
221-
await md.save()
240+
new_md.id = orig_md.id
241+
242+
patched_md = await patch_metadata(orig_md, new_md.content, es)
222243

223244
# Update entry to the metadata index
224245
await index_file(es, FileOut(**file.dict()), update=True)
225-
return md.dict()
246+
return patched_md.dict()
226247
else:
227248
raise HTTPException(status_code=404, detail="No metadata found to update")
228249
else:

0 commit comments

Comments
 (0)