2025-12-26 18:53:14 +01:00
|
|
|
import logging
|
|
|
|
|
import re
|
2025-12-20 04:31:02 +01:00
|
|
|
import uuid
|
|
|
|
|
|
2025-12-26 18:53:14 +01:00
|
|
|
from natsort import natsorted
|
2025-12-20 04:31:02 +01:00
|
|
|
from sqlmodel import (
|
2026-01-08 12:07:51 +01:00
|
|
|
Field, # pyright: ignore[reportUnknownVariableType]
|
2025-12-20 04:31:02 +01:00
|
|
|
SQLModel,
|
|
|
|
|
)
|
|
|
|
|
|
2025-12-26 18:53:14 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
2025-12-20 04:31:02 +01:00
|
|
|
|
|
|
|
|
|
2026-01-08 12:06:12 +01:00
|
|
|
class TeilchenCreate(SQLModel):
|
2025-12-20 04:31:02 +01:00
|
|
|
description: str | None
|
2025-12-26 18:53:14 +01:00
|
|
|
name: str = Field(index=True)
|
2025-12-20 04:31:02 +01:00
|
|
|
number: int = Field(default=1)
|
2026-01-08 12:06:12 +01:00
|
|
|
text: str
|
2025-12-26 18:53:14 +01:00
|
|
|
tags: str | None
|
2025-12-20 04:31:02 +01:00
|
|
|
|
|
|
|
|
|
2025-12-26 18:53:14 +01:00
|
|
|
class Teilchen(TeilchenCreate, table=True):
|
|
|
|
|
id: uuid.UUID = Field(default_factory=uuid.uuid7, primary_key=True)
|
|
|
|
|
|
2025-12-20 04:31:02 +01:00
|
|
|
|
2025-12-26 18:53:14 +01:00
|
|
|
def make_teilchen_input(text: str) -> TeilchenCreate | None:
|
|
|
|
|
if not text:
|
|
|
|
|
logger.error("Empty text.")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
name_end = text.find(".")
|
|
|
|
|
name = text[0:name_end]
|
|
|
|
|
if not name:
|
|
|
|
|
logger.error("Could not extract name.")
|
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
description_start = text.find('"', name_end + 1)
|
|
|
|
|
description_end = text.find('"', description_start + 1)
|
|
|
|
|
description = text[description_start:description_end]
|
|
|
|
|
if not description:
|
|
|
|
|
logger.warning("Could not extract description.")
|
|
|
|
|
|
|
|
|
|
tags = re.findall(r"#\w+", text.lower())
|
|
|
|
|
if not tags:
|
2026-01-08 12:07:31 +01:00
|
|
|
logger.warning("No tags found in text.")
|
2025-12-26 18:53:14 +01:00
|
|
|
|
|
|
|
|
return TeilchenCreate(
|
|
|
|
|
name=name,
|
|
|
|
|
description=description,
|
|
|
|
|
tags=" ".join(natsorted(tags)),
|
|
|
|
|
text=text,
|
|
|
|
|
)
|