|
1 | 1 | from typing import List, Optional |
2 | 2 |
|
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 | | - |
9 | 3 | from app import dependencies |
10 | 4 | from app.config import settings |
11 | 5 | from app.deps.authorization_deps import FileAuthorization |
|
28 | 22 | ) |
29 | 23 | from app.search.connect import delete_document_by_id |
30 | 24 | 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 |
31 | 30 |
|
32 | 31 | router = APIRouter() |
33 | 32 |
|
@@ -145,6 +144,16 @@ async def add_file_metadata( |
145 | 144 | 409, f"Metadata for {definition} already exists on this file" |
146 | 145 | ) |
147 | 146 |
|
| 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 | + |
148 | 157 | md = await _build_metadata_db_obj(metadata_in, FileOut(**file.dict()), user) |
149 | 158 | await md.insert() |
150 | 159 |
|
@@ -210,19 +219,31 @@ async def replace_file_metadata( |
210 | 219 |
|
211 | 220 | if (orig_md := await MetadataDB.find_one(*query)) is not None: |
212 | 221 | # 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( |
214 | 234 | metadata_in, |
215 | 235 | FileOut(**file.dict()), |
216 | 236 | user, |
217 | 237 | agent=agent, |
218 | 238 | version=target_version, |
219 | 239 | ) |
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) |
222 | 243 |
|
223 | 244 | # Update entry to the metadata index |
224 | 245 | await index_file(es, FileOut(**file.dict()), update=True) |
225 | | - return md.dict() |
| 246 | + return patched_md.dict() |
226 | 247 | else: |
227 | 248 | raise HTTPException(status_code=404, detail="No metadata found to update") |
228 | 249 | else: |
|
0 commit comments