Merge branch 'dev' into feature/cog-3532-empower-test_search-db-retrievers-tests-reorg

This commit is contained in:
hajdul88 2025-12-11 10:54:46 +01:00 committed by GitHub
commit a75cb07aec
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -5,6 +5,7 @@ from pathlib import Path
from datetime import datetime, timezone from datetime import datetime, timezone
from typing import Optional, List from typing import Optional, List
from dataclasses import dataclass from dataclasses import dataclass
from fastapi import UploadFile
@dataclass @dataclass
@ -45,8 +46,10 @@ class OntologyService:
json.dump(metadata, f, indent=2) json.dump(metadata, f, indent=2)
async def upload_ontology( async def upload_ontology(
self, ontology_key: str, file, user, description: Optional[str] = None self, ontology_key: str, file: UploadFile, user, description: Optional[str] = None
) -> OntologyMetadata: ) -> OntologyMetadata:
if not file.filename:
raise ValueError("File must have a filename")
if not file.filename.lower().endswith(".owl"): if not file.filename.lower().endswith(".owl"):
raise ValueError("File must be in .owl format") raise ValueError("File must be in .owl format")
@ -57,8 +60,6 @@ class OntologyService:
raise ValueError(f"Ontology key '{ontology_key}' already exists") raise ValueError(f"Ontology key '{ontology_key}' already exists")
content = await file.read() content = await file.read()
if len(content) > 10 * 1024 * 1024:
raise ValueError("File size exceeds 10MB limit")
file_path = user_dir / f"{ontology_key}.owl" file_path = user_dir / f"{ontology_key}.owl"
with open(file_path, "wb") as f: with open(file_path, "wb") as f:
@ -82,7 +83,11 @@ class OntologyService:
) )
async def upload_ontologies( async def upload_ontologies(
self, ontology_key: List[str], files: List, user, descriptions: Optional[List[str]] = None self,
ontology_key: List[str],
files: List[UploadFile],
user,
descriptions: Optional[List[str]] = None,
) -> List[OntologyMetadata]: ) -> List[OntologyMetadata]:
""" """
Upload ontology files with their respective keys. Upload ontology files with their respective keys.
@ -105,47 +110,17 @@ class OntologyService:
if len(set(ontology_key)) != len(ontology_key): if len(set(ontology_key)) != len(ontology_key):
raise ValueError("Duplicate ontology keys not allowed") raise ValueError("Duplicate ontology keys not allowed")
if descriptions and len(descriptions) != len(files):
raise ValueError("Number of descriptions must match number of files")
results = [] results = []
user_dir = self._get_user_dir(str(user.id))
metadata = self._load_metadata(user_dir)
for i, (key, file) in enumerate(zip(ontology_key, files)): for i, (key, file) in enumerate(zip(ontology_key, files)):
if key in metadata:
raise ValueError(f"Ontology key '{key}' already exists")
if not file.filename.lower().endswith(".owl"):
raise ValueError(f"File '{file.filename}' must be in .owl format")
content = await file.read()
if len(content) > 10 * 1024 * 1024:
raise ValueError(f"File '{file.filename}' exceeds 10MB limit")
file_path = user_dir / f"{key}.owl"
with open(file_path, "wb") as f:
f.write(content)
ontology_metadata = {
"filename": file.filename,
"size_bytes": len(content),
"uploaded_at": datetime.now(timezone.utc).isoformat(),
"description": descriptions[i] if descriptions else None,
}
metadata[key] = ontology_metadata
results.append( results.append(
OntologyMetadata( await self.upload_ontology(
ontology_key=key, ontology_key=key,
filename=file.filename, file=file,
size_bytes=len(content), user=user,
uploaded_at=ontology_metadata["uploaded_at"],
description=descriptions[i] if descriptions else None, description=descriptions[i] if descriptions else None,
) )
) )
self._save_metadata(user_dir, metadata)
return results return results
def get_ontology_contents(self, ontology_key: List[str], user) -> List[str]: def get_ontology_contents(self, ontology_key: List[str], user) -> List[str]: