fix(knowledge): improve upload workflow and seed manufacturers
This commit is contained in:
parent
964b545bc5
commit
612e1ad8ed
13 changed files with 429 additions and 20 deletions
56
backend/hermes/app/knowledge_seed.py
Normal file
56
backend/hermes/app/knowledge_seed.py
Normal file
|
|
@ -0,0 +1,56 @@
|
|||
import logging
|
||||
|
||||
from sqlalchemy.orm import Session
|
||||
|
||||
from app.models.knowledge import KnowledgeManufacturer
|
||||
from app.repositories.knowledge_repository import KnowledgeRepository
|
||||
from app.services.knowledge_service import unique_slug
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
STANDARD_MANUFACTURERS = [
|
||||
"Stabo",
|
||||
"President",
|
||||
"Albrecht",
|
||||
"Marconi",
|
||||
"Rohde & Schwarz",
|
||||
"HP",
|
||||
"CRT",
|
||||
"Alinco",
|
||||
"Motorola",
|
||||
"Team",
|
||||
]
|
||||
|
||||
|
||||
def seed_knowledge_manufacturers(db: Session) -> None:
|
||||
created = 0
|
||||
|
||||
for name in STANDARD_MANUFACTURERS:
|
||||
manufacturer = KnowledgeRepository.get_manufacturer_by_name(db, name)
|
||||
if manufacturer is not None:
|
||||
changed = False
|
||||
if not manufacturer.slug:
|
||||
manufacturer.slug = unique_slug(
|
||||
db,
|
||||
manufacturer.name,
|
||||
lambda session, value: KnowledgeRepository.get_manufacturer_by_slug(
|
||||
session,
|
||||
value,
|
||||
manufacturer.id,
|
||||
) is not None,
|
||||
)
|
||||
changed = True
|
||||
if changed:
|
||||
db.add(manufacturer)
|
||||
continue
|
||||
|
||||
slug = unique_slug(
|
||||
db,
|
||||
name,
|
||||
lambda session, value: KnowledgeRepository.get_manufacturer_by_slug(session, value) is not None,
|
||||
)
|
||||
db.add(KnowledgeManufacturer(name=name, slug=slug, website="", notes=""))
|
||||
created += 1
|
||||
|
||||
db.commit()
|
||||
logger.info("knowledge.manufacturers.seeded", extra={"created": created})
|
||||
|
|
@ -20,6 +20,7 @@ from app.api.users import router as users_router
|
|||
from app.db.database import SessionLocal
|
||||
from app.db.health import check_database
|
||||
from app.core.logging import configure_logging
|
||||
from app.knowledge_seed import seed_knowledge_manufacturers
|
||||
from app.rbac.seed import seed_rbac
|
||||
from app.services.initial_admin_bootstrap import bootstrap_initial_admin
|
||||
|
||||
|
|
@ -76,6 +77,7 @@ def startup_seed_rbac():
|
|||
db = SessionLocal()
|
||||
try:
|
||||
seed_rbac(db)
|
||||
seed_knowledge_manufacturers(db)
|
||||
bootstrap_initial_admin(db)
|
||||
finally:
|
||||
db.close()
|
||||
|
|
|
|||
|
|
@ -125,7 +125,7 @@ class KnowledgeService:
|
|||
file: UploadFile,
|
||||
payload: KnowledgeDocumentCreate,
|
||||
) -> KnowledgeDocument:
|
||||
KnowledgeService._validate_document_links(db, payload.manufacturer_id, payload.device_id)
|
||||
KnowledgeService._validate_upload_links(db, payload.manufacturer_id, payload.device_id)
|
||||
storage_service = get_storage_service()
|
||||
max_bytes = storage_service.max_upload_mb * 1024 * 1024
|
||||
content = await file.read(max_bytes + 1)
|
||||
|
|
@ -208,6 +208,8 @@ class KnowledgeService:
|
|||
|
||||
@staticmethod
|
||||
def _validate_document_links(db: Session, manufacturer_id: int, device_id: int | None) -> None:
|
||||
if not manufacturer_id:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Bitte wählen Sie einen Hersteller aus.")
|
||||
if KnowledgeRepository.get_manufacturer(db, manufacturer_id) is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Hersteller nicht gefunden")
|
||||
if device_id is not None:
|
||||
|
|
@ -227,3 +229,23 @@ class KnowledgeService:
|
|||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Gerät nicht gefunden")
|
||||
if manufacturer_id is not None and device.manufacturer_id != manufacturer_id:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Gerät gehört nicht zum Hersteller")
|
||||
|
||||
@staticmethod
|
||||
def _validate_upload_links(db: Session, manufacturer_id: int, device_id: int | None) -> None:
|
||||
if not manufacturer_id:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Bitte wählen Sie einen Hersteller aus.")
|
||||
if device_id is None:
|
||||
raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST, detail="Bitte wählen Sie ein Gerät aus.")
|
||||
|
||||
manufacturer = KnowledgeRepository.get_manufacturer(db, manufacturer_id)
|
||||
if manufacturer is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Der ausgewählte Hersteller existiert nicht.")
|
||||
|
||||
device = KnowledgeRepository.get_device(db, device_id)
|
||||
if device is None:
|
||||
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail="Das ausgewählte Gerät existiert nicht.")
|
||||
if device.manufacturer_id != manufacturer_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail="Das ausgewählte Gerät gehört nicht zum ausgewählten Hersteller.",
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue