34 lines
985 B
Python
34 lines
985 B
Python
from sqlalchemy import select
|
|
from sqlalchemy.orm import Session
|
|
|
|
from app.models import Document, DocumentTag
|
|
|
|
|
|
def get_or_create_tags(db: Session, names: list[str]) -> list[DocumentTag]:
|
|
tags: list[DocumentTag] = []
|
|
for raw_name in names:
|
|
name = raw_name.strip()
|
|
if not name:
|
|
continue
|
|
tag = db.scalar(select(DocumentTag).where(DocumentTag.name == name))
|
|
if not tag:
|
|
tag = DocumentTag(name=name)
|
|
db.add(tag)
|
|
db.flush()
|
|
tags.append(tag)
|
|
return tags
|
|
|
|
|
|
def serialize_document(document: Document) -> dict:
|
|
return {
|
|
"id": document.id,
|
|
"title": document.title,
|
|
"version": document.version,
|
|
"adp_doc_id": document.adp_doc_id,
|
|
"secrecy_level": document.secrecy_level,
|
|
"status": document.status,
|
|
"owner_dept": document.owner_dept,
|
|
"tags": [tag.name for tag in document.tags],
|
|
"created_at": document.created_at,
|
|
}
|