teilchensammler-cli/src/teilchensammler_cli/models.py
2026-02-07 02:52:13 +01:00

68 lines
1.8 KiB
Python

import logging
import uuid
from sqlmodel import (
Field,
SQLModel,
)
logger = logging.getLogger(__name__)
TeilchenDatum = tuple[int, str, str, str, str]
class TeilchenCreate(SQLModel):
description: str | None
name: str = Field(index=True)
number: int = Field(default=1)
text: str
tags: str | None
class Teilchen(TeilchenCreate, table=True):
id: uuid.UUID = Field(default_factory=uuid.uuid7, primary_key=True) # ty:ignore[unresolved-attribute]
async def make_teilchen_input(text: str) -> TeilchenCreate | None:
import re
from natsort import natsorted
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)
if description_end > description_start:
description = text[description_start:description_end]
else:
description = ""
tags = re.findall(r"#\w+", text.lower())
if not tags:
logger.info("No tags found in text.")
return TeilchenCreate(
name=name,
description=description,
tags=" ".join(natsorted(tags)),
text=text,
)
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"),
]