Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions backend/app/models/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,9 +307,11 @@ async def validate_context(
detail="Context is required",
)
if context is not None:
pass
# TODO validate context
return content
if context_url is not None:
pass
# TODO validate context
return content
if definition is not None:
if (
md_def := await MetadataDefinitionDB.find_one(
Expand All @@ -322,7 +324,7 @@ async def validate_context(
status_code=400,
detail=f"{definition} is not valid metadata definition",
)
return content
return content


def deep_update(orig: dict, new: dict):
Expand Down
11 changes: 10 additions & 1 deletion backend/app/routers/metadata_datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
from app.search.connect import delete_document_by_id
from app.search.index import index_dataset
from beanie import PydanticObjectId
from bson import ObjectId
from elasticsearch import Elasticsearch
from fastapi import APIRouter, Depends, Form, HTTPException

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

# lookup json_ld context in metadata definition
if metadata_in.definition is not None:
if (
definition := await MetadataDefinitionDB.find_one(
MetadataDefinitionDB.name == metadata_in.definition
)
) is not None:
metadata_in.context = definition.context
metadata_in.context_url = definition.context_url

md = await _build_metadata_db_obj(
metadata_in, DatasetOut(**dataset.dict()), user
)
Expand Down
41 changes: 31 additions & 10 deletions backend/app/routers/metadata_files.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
from typing import List, Optional

from beanie import PydanticObjectId
from beanie.operators import Or
from bson import ObjectId
from elasticsearch import Elasticsearch
from fastapi import APIRouter, Depends, Form, HTTPException

from app import dependencies
from app.config import settings
from app.deps.authorization_deps import FileAuthorization
Expand All @@ -28,6 +22,11 @@
)
from app.search.connect import delete_document_by_id
from app.search.index import index_file
from beanie import PydanticObjectId
from beanie.operators import Or
from bson import ObjectId
from elasticsearch import Elasticsearch
from fastapi import APIRouter, Depends, Form, HTTPException

router = APIRouter()

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

# lookup json_ld context in metadata definition
if metadata_in.definition is not None:
if (
definition := await MetadataDefinitionDB.find_one(
MetadataDefinitionDB.name == metadata_in.definition
)
) is not None:
metadata_in.context = definition.context
metadata_in.context_url = definition.context_url

md = await _build_metadata_db_obj(metadata_in, FileOut(**file.dict()), user)
await md.insert()

Expand Down Expand Up @@ -210,19 +219,31 @@ async def replace_file_metadata(

if (orig_md := await MetadataDB.find_one(*query)) is not None:
# Metadata exists, so prepare the new document we are going to replace it with
md = await _build_metadata_db_obj(

# lookup json_ld context in metadata definition
if metadata_in.definition is not None:
if (
definition := await MetadataDefinitionDB.find_one(
MetadataDefinitionDB.name == metadata_in.definition
)
) is not None:
metadata_in.context = definition.context
metadata_in.context_url = definition.context_url

new_md = await _build_metadata_db_obj(
metadata_in,
FileOut(**file.dict()),
user,
agent=agent,
version=target_version,
)
md.id = orig_md.id
await md.save()
new_md.id = orig_md.id

patched_md = await patch_metadata(orig_md, new_md.content, es)

# Update entry to the metadata index
await index_file(es, FileOut(**file.dict()), update=True)
return md.dict()
return patched_md.dict()
else:
raise HTTPException(status_code=404, detail="No metadata found to update")
else:
Expand Down
2 changes: 1 addition & 1 deletion backend/app/tests/test_metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ async def test_dataset_patch_metadata_definition(client: TestClient, headers: di
result.body["responses"][0]["hits"]["hits"][0]["_source"]["metadata"][0][
"latitude"
]
== 24.4
== "24.4"
)


Expand Down