Fix source actions and toggle regressions

This commit is contained in:
Abel Luck 2026-03-30 17:25:37 +02:00
parent 94717b1d1b
commit 2a99edeec3
5 changed files with 122 additions and 5 deletions

View file

@ -305,6 +305,15 @@ def delete_job_source(job_id: int) -> bool:
return source.delete_instance() > 0
def delete_source(slug: str) -> bool:
with database.connection_context():
with database.atomic():
source = Source.get_or_none(Source.slug == slug)
if source is None:
return False
return source.delete_instance() > 0
def load_sources() -> tuple[dict[str, object], ...]:
with database.connection_context():
sources = tuple(Source.select().order_by(Source.created_at.desc()))

View file

@ -54,6 +54,29 @@ def _checked(source: Mapping[str, object] | None, key: str, default: bool) -> bo
return bool(value)
def _action_button(
*,
label: str,
tone: str = "default",
post_path: str | None = None,
) -> Renderable:
classes = {
"default": "bg-stone-100 text-slate-700 hover:bg-stone-200",
"danger": "bg-rose-50 text-rose-700 hover:bg-rose-100",
}
attributes: dict[str, str] = {}
if post_path is not None:
attributes["data-on:pointerdown"] = f"@post('{post_path}')"
return h.button(
attributes,
type="button",
class_=(
"inline-flex items-center whitespace-nowrap rounded-full px-3 py-1.5 "
f"text-sm font-semibold transition {classes[tone]}"
),
)[label]
def _source_row(source: Mapping[str, object]) -> tuple[Node, ...]:
return (
h.div[
@ -81,6 +104,11 @@ def _source_row(source: Mapping[str, object]) -> tuple[Node, ...]:
href=f"/sources/{source['slug']}/edit", label="Edit", tone="amber"
),
inline_link(href="/runs", label="View runs"),
_action_button(
label="Delete",
tone="danger",
post_path=f"/actions/sources/{source['slug']}/delete",
),
],
)

View file

@ -26,6 +26,7 @@ from repub.model import (
Job,
create_source,
delete_job_source,
delete_source,
initialize_database,
load_source_form,
load_sources,
@ -209,6 +210,13 @@ def create_app(*, dev_mode: bool = False) -> Quart:
trigger_refresh(app)
return DatastarResponse(SSE.redirect("/sources"))
@app.post("/actions/sources/<string:slug>/delete")
async def delete_source_action(slug: str) -> Response:
delete_source(slug)
get_job_runtime(app).sync_jobs()
trigger_refresh(app)
return Response(status=204)
@app.post("/runs")
async def runs_patch() -> DatastarResponse:
return _page_patch_response(app, lambda: render_runs(app))
@ -383,7 +391,7 @@ def validate_source_form(
source_type = _read_string(signals, "sourceType")
feed_url = _read_string(signals, "feedUrl")
pangea_domain = _read_string(signals, "pangeaDomain")
pangea_category = _read_string(signals, "pangeaCategory")
pangea_category = _read_string(signals, "pangeaCategory", strip=False)
content_format = _read_string(signals, "contentFormat")
content_type = _read_string(signals, "contentType")
max_articles = _read_string(signals, "maxArticles")
@ -474,8 +482,9 @@ def validate_source_form(
return source, None
def _read_string(signals: dict[str, object], key: str) -> str:
return str(signals.get(key, "")).strip()
def _read_string(signals: dict[str, object], key: str, *, strip: bool = True) -> str:
value = str(signals.get(key, ""))
return value.strip() if strip else value
def _read_bool(signals: dict[str, object], key: str, *, default: bool = False) -> bool: