ruff format
This commit is contained in:
parent
b47f825d54
commit
e156de22c3
5 changed files with 28 additions and 34 deletions
|
|
@ -26,9 +26,7 @@ def init_db(db_path: Path) -> None:
|
||||||
updated_at TEXT NOT NULL
|
updated_at TEXT NOT NULL
|
||||||
)
|
)
|
||||||
""")
|
""")
|
||||||
conn.execute(
|
conn.execute("CREATE INDEX IF NOT EXISTS idx_segments_order ON segments(sort_order)")
|
||||||
"CREATE INDEX IF NOT EXISTS idx_segments_order ON segments(sort_order)"
|
|
||||||
)
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
|
||||||
|
|
@ -17,9 +17,7 @@ DbPath = Annotated[Path, Depends(get_db_path)]
|
||||||
|
|
||||||
@router.get("/")
|
@router.get("/")
|
||||||
def get_site(db_path: DbPath) -> SiteResponse:
|
def get_site(db_path: DbPath) -> SiteResponse:
|
||||||
title = site_service.get_site_title(
|
title = site_service.get_site_title(db_path, default=get_settings().site_title)
|
||||||
db_path, default=get_settings().site_title
|
|
||||||
)
|
|
||||||
return SiteResponse(title=title)
|
return SiteResponse(title=title)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -36,8 +36,16 @@ def create_segment(db_path: Path, request: SegmentCreateRequest) -> Segment:
|
||||||
cols = "id, type, sort_order, title, content, metadata, created_at, updated_at"
|
cols = "id, type, sort_order, title, content, metadata, created_at, updated_at"
|
||||||
conn.execute(
|
conn.execute(
|
||||||
f"INSERT INTO segments ({cols}) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
f"INSERT INTO segments ({cols}) VALUES (?, ?, ?, ?, ?, ?, ?, ?)",
|
||||||
(str(segment_id), request.type.value, sort_order, request.title,
|
(
|
||||||
request.content, metadata_json, now, now),
|
str(segment_id),
|
||||||
|
request.type.value,
|
||||||
|
sort_order,
|
||||||
|
request.title,
|
||||||
|
request.content,
|
||||||
|
metadata_json,
|
||||||
|
now,
|
||||||
|
now,
|
||||||
|
),
|
||||||
)
|
)
|
||||||
conn.commit()
|
conn.commit()
|
||||||
|
|
||||||
|
|
@ -58,9 +66,7 @@ def create_segment(db_path: Path, request: SegmentCreateRequest) -> Segment:
|
||||||
def list_segments(db_path: Path) -> list[Segment]:
|
def list_segments(db_path: Path) -> list[Segment]:
|
||||||
conn = get_connection(db_path)
|
conn = get_connection(db_path)
|
||||||
try:
|
try:
|
||||||
rows = conn.execute(
|
rows = conn.execute("SELECT * FROM segments ORDER BY sort_order ASC").fetchall()
|
||||||
"SELECT * FROM segments ORDER BY sort_order ASC"
|
|
||||||
).fetchall()
|
|
||||||
return [_row_to_segment(row) for row in rows]
|
return [_row_to_segment(row) for row in rows]
|
||||||
finally:
|
finally:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
@ -69,9 +75,7 @@ def list_segments(db_path: Path) -> list[Segment]:
|
||||||
def get_segment(db_path: Path, segment_id: UUID) -> Segment | None:
|
def get_segment(db_path: Path, segment_id: UUID) -> Segment | None:
|
||||||
conn = get_connection(db_path)
|
conn = get_connection(db_path)
|
||||||
try:
|
try:
|
||||||
row = conn.execute(
|
row = conn.execute("SELECT * FROM segments WHERE id = ?", (str(segment_id),)).fetchone()
|
||||||
"SELECT * FROM segments WHERE id = ?", (str(segment_id),)
|
|
||||||
).fetchone()
|
|
||||||
if row is None:
|
if row is None:
|
||||||
return None
|
return None
|
||||||
return _row_to_segment(row)
|
return _row_to_segment(row)
|
||||||
|
|
@ -94,8 +98,7 @@ def update_segment(
|
||||||
title = request.title if request.title is not None else existing["title"]
|
title = request.title if request.title is not None else existing["title"]
|
||||||
content = request.content if request.content is not None else existing["content"]
|
content = request.content if request.content is not None else existing["content"]
|
||||||
metadata_json = (
|
metadata_json = (
|
||||||
json.dumps(request.metadata) if request.metadata is not None
|
json.dumps(request.metadata) if request.metadata is not None else existing["metadata"]
|
||||||
else existing["metadata"]
|
|
||||||
)
|
)
|
||||||
|
|
||||||
conn.execute(
|
conn.execute(
|
||||||
|
|
@ -113,9 +116,7 @@ def update_segment(
|
||||||
def delete_segment(db_path: Path, segment_id: UUID) -> bool:
|
def delete_segment(db_path: Path, segment_id: UUID) -> bool:
|
||||||
conn = get_connection(db_path)
|
conn = get_connection(db_path)
|
||||||
try:
|
try:
|
||||||
cursor = conn.execute(
|
cursor = conn.execute("DELETE FROM segments WHERE id = ?", (str(segment_id),))
|
||||||
"DELETE FROM segments WHERE id = ?", (str(segment_id),)
|
|
||||||
)
|
|
||||||
conn.commit()
|
conn.commit()
|
||||||
return cursor.rowcount > 0
|
return cursor.rowcount > 0
|
||||||
finally:
|
finally:
|
||||||
|
|
@ -125,10 +126,7 @@ def delete_segment(db_path: Path, segment_id: UUID) -> bool:
|
||||||
def reorder_segments(db_path: Path, segment_ids: list[UUID]) -> list[Segment]:
|
def reorder_segments(db_path: Path, segment_ids: list[UUID]) -> list[Segment]:
|
||||||
conn = get_connection(db_path)
|
conn = get_connection(db_path)
|
||||||
try:
|
try:
|
||||||
existing_ids = {
|
existing_ids = {row["id"] for row in conn.execute("SELECT id FROM segments").fetchall()}
|
||||||
row["id"]
|
|
||||||
for row in conn.execute("SELECT id FROM segments").fetchall()
|
|
||||||
}
|
|
||||||
requested_ids = {str(sid) for sid in segment_ids}
|
requested_ids = {str(sid) for sid in segment_ids}
|
||||||
missing = requested_ids - existing_ids
|
missing = requested_ids - existing_ids
|
||||||
if missing:
|
if missing:
|
||||||
|
|
|
||||||
|
|
@ -8,9 +8,7 @@ DEFAULT_SITE_TITLE = "Untitled Site"
|
||||||
def get_site_title(db_path: Path, *, default: str = DEFAULT_SITE_TITLE) -> str:
|
def get_site_title(db_path: Path, *, default: str = DEFAULT_SITE_TITLE) -> str:
|
||||||
conn = get_connection(db_path)
|
conn = get_connection(db_path)
|
||||||
try:
|
try:
|
||||||
row = conn.execute(
|
row = conn.execute("SELECT value FROM site WHERE key = ?", ("title",)).fetchone()
|
||||||
"SELECT value FROM site WHERE key = ?", ("title",)
|
|
||||||
).fetchone()
|
|
||||||
if row is None:
|
if row is None:
|
||||||
return default
|
return default
|
||||||
return str(row["value"])
|
return str(row["value"])
|
||||||
|
|
|
||||||
|
|
@ -9,9 +9,7 @@ def test_init_db_creates_tables(tmp_data_dir: Path) -> None:
|
||||||
init_db(db_path)
|
init_db(db_path)
|
||||||
|
|
||||||
conn = sqlite3.connect(db_path)
|
conn = sqlite3.connect(db_path)
|
||||||
cursor = conn.execute(
|
cursor = conn.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
|
||||||
"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"
|
|
||||||
)
|
|
||||||
tables = {row[0] for row in cursor.fetchall()}
|
tables = {row[0] for row in cursor.fetchall()}
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
@ -25,9 +23,7 @@ def test_init_db_idempotent(tmp_data_dir: Path) -> None:
|
||||||
init_db(db_path)
|
init_db(db_path)
|
||||||
|
|
||||||
conn = sqlite3.connect(db_path)
|
conn = sqlite3.connect(db_path)
|
||||||
cursor = conn.execute(
|
cursor = conn.execute("SELECT name FROM sqlite_master WHERE type='table' ORDER BY name")
|
||||||
"SELECT name FROM sqlite_master WHERE type='table' ORDER BY name"
|
|
||||||
)
|
|
||||||
tables = {row[0] for row in cursor.fetchall()}
|
tables = {row[0] for row in cursor.fetchall()}
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
|
|
@ -63,8 +59,14 @@ def test_segments_table_schema(db: Path) -> None:
|
||||||
conn.close()
|
conn.close()
|
||||||
|
|
||||||
expected = {
|
expected = {
|
||||||
"id", "type", "sort_order", "title",
|
"id",
|
||||||
"content", "metadata", "created_at", "updated_at",
|
"type",
|
||||||
|
"sort_order",
|
||||||
|
"title",
|
||||||
|
"content",
|
||||||
|
"metadata",
|
||||||
|
"created_at",
|
||||||
|
"updated_at",
|
||||||
}
|
}
|
||||||
assert columns == expected
|
assert columns == expected
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue