2025-12-26 18:53:14 +01:00
|
|
|
import logging
|
2025-12-20 04:31:02 +01:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
|
|
from sqlmodel import (
|
2026-02-07 02:48:27 +01:00
|
|
|
Field,
|
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-02-07 02:49:38 +01:00
|
|
|
TeilchenDatum = tuple[int, str, str, str, str]
|
|
|
|
|
|
|
|
|
|
|
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):
|
2026-02-07 02:48:27 +01:00
|
|
|
id: uuid.UUID = Field(default_factory=uuid.uuid7, primary_key=True) # ty:ignore[unresolved-attribute]
|
2025-12-26 18:53:14 +01:00
|
|
|
|
2025-12-20 04:31:02 +01:00
|
|
|
|
2026-02-07 02:47:36 +01:00
|
|
|
async def make_teilchen_input(text: str) -> TeilchenCreate | None:
|
2026-02-07 02:46:01 +01:00
|
|
|
import re
|
|
|
|
|
from natsort import natsorted
|
|
|
|
|
|
2025-12-26 18:53:14 +01:00
|
|
|
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)
|
2026-02-07 02:46:44 +01:00
|
|
|
if description_end > description_start:
|
|
|
|
|
description = text[description_start:description_end]
|
|
|
|
|
else:
|
|
|
|
|
description = ""
|
2025-12-26 18:53:14 +01:00
|
|
|
|
|
|
|
|
tags = re.findall(r"#\w+", text.lower())
|
|
|
|
|
if not tags:
|
2026-02-07 02:46:44 +01:00
|
|
|
logger.info("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,
|
|
|
|
|
)
|
2026-02-07 02:52:13 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
async def load_initial_data() -> list[TeilchenDatum]:
|
|
|
|
|
return [
|
|
|
|
|
(0, "Name0", "Description0", "9000", "#tag0 #tag00 #tag000"),
|
|
|
|
|
(1, "Name1", "Description1", "9001", "#tag1 #tag11 #tag111"),
|
|
|
|
|
(2, "Name2", "Description2", "9002", "#tag2 #tag22 #tag222"),
|
|
|
|
|
(3, "Name3", "Description3", "9003", "#tag3 #tag33 #tag333"),
|
|
|
|
|
(4, "Name4", "Description4", "9004", "#tag4 #tag44 #tag444"),
|
|
|
|
|
(5, "Name5", "Description5", "9005", "#tag5 #tag55 #tag555"),
|
|
|
|
|
]
|