document-processor/app/logic/id_gen.py

24 lines
498 B
Python

import time
from sqlalchemy.orm import Session
from app.models.document import Document
def generate_document_id(db: Session) -> str:
ts = int(time.time())
ts_hex = format(ts, "x")
n = 0
while True:
n_hex = format(n, "x")
candidate = f"doc_{ts_hex}-{n_hex}"
exists = (
db.query(Document)
.filter(Document.document_id == candidate)
.first()
)
if not exists:
return candidate
n += 1