From 8ec35babe87ff8139ec23cc10b858b7481f132db Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 19 Nov 2026 18:15:34 +0100 Subject: refactor(sender)!: align sent Webmention terminology Rename source-list forms, views, helpers, and tests around the Sent Webmentions UI while keeping source documents as the grouping model. BREAKING CHANGE: authenticated outgoing routes move from `/sources` to `/sent`, with corresponding blueprint endpoint and Python module renames. --- tests/forms/test_sent.py | 81 ++++ tests/forms/test_sources.py | 81 ---- tests/views/test_sent.py | 708 +++++++++++++++++++++++++++++ tests/views/test_sources.py | 701 ---------------------------- webmentions_ssg/forms/sent.py | 81 ++++ webmentions_ssg/forms/sources.py | 81 ---- webmentions_ssg/templates/base.html | 4 +- webmentions_ssg/templates/dashboard.html | 2 +- webmentions_ssg/templates/sent.html | 14 +- webmentions_ssg/templates/sent_source.html | 4 +- webmentions_ssg/views/__init__.py | 4 +- webmentions_ssg/views/sent.py | 277 +++++++++++ webmentions_ssg/views/sources.py | 277 ----------- 13 files changed, 1161 insertions(+), 1154 deletions(-) create mode 100644 tests/forms/test_sent.py delete mode 100644 tests/forms/test_sources.py create mode 100644 tests/views/test_sent.py delete mode 100644 tests/views/test_sources.py create mode 100644 webmentions_ssg/forms/sent.py delete mode 100644 webmentions_ssg/forms/sources.py create mode 100644 webmentions_ssg/views/sent.py delete mode 100644 webmentions_ssg/views/sources.py diff --git a/tests/forms/test_sent.py b/tests/forms/test_sent.py new file mode 100644 index 0000000..1cdd463 --- /dev/null +++ b/tests/forms/test_sent.py @@ -0,0 +1,81 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Validation of sent Webmentions GET filters.""" + +import pytest +from flask import Flask +from werkzeug.datastructures import MultiDict + +from webmentions_ssg.forms.sent import SentFilterForm + + +def test_sent_filter_form_accepts_get_params(app: Flask) -> None: + with app.test_request_context("/sent"): + form = SentFilterForm( + formdata=MultiDict( + { + "status": "retrying", + "q": "example.org", + "date_from": "2026-11-08", + "date_to": "2026-11-14", + "sort": "webmentions", + "order": "asc", + "per_page": "37", + } + ), + meta={"csrf": False}, + ) + assert form.validate() + assert form.status.data == "retrying" + assert form.date_from.data is not None + assert form.date_to.data is not None + assert form.date_from.data.isoformat() == "2026-11-08" + assert form.date_to.data.isoformat() == "2026-11-14" + assert (form.sort.data, form.order.data, form.per_page.data) == ( + "webmentions", + "asc", + 37, + ) + assert "csrf_token" not in form._fields + + +@pytest.mark.parametrize( + "data", + [ + {"status": "unknown"}, + {"sort": "unknown"}, + {"order": "up"}, + {"per_page": "0"}, + {"per_page": "101"}, + {"per_page": "nope"}, + {"date_from": "2026-11-8"}, + {"date_to": "not-a-date"}, + {"date_from": "2026-11-15", "date_to": "2026-11-08"}, + ], +) +def test_sent_filter_form_rejects_bad_get_params( + app: Flask, data: dict[str, str] +) -> None: + with app.test_request_context("/sent"): + form = SentFilterForm(formdata=MultiDict(data), meta={"csrf": False}) + assert not form.validate() + assert form.errors + + +def test_sent_filter_form_defaults_and_configured_maximum(app: Flask) -> None: + app.config["SOURCES_MAX_PAGE_SIZE"] = 10 + with app.test_request_context("/sent"): + form = SentFilterForm(formdata=MultiDict(), meta={"csrf": False}) + assert form.validate() + assert ( + form.status.data, + form.sort.data, + form.order.data, + form.per_page.data, + ) == ("", "last_seen", "desc", 10) + form = SentFilterForm( + formdata=MultiDict({"per_page": "11"}), meta={"csrf": False} + ) + assert not form.validate() diff --git a/tests/forms/test_sources.py b/tests/forms/test_sources.py deleted file mode 100644 index 082b0d3..0000000 --- a/tests/forms/test_sources.py +++ /dev/null @@ -1,81 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Validation of source-list GET filters.""" - -import pytest -from flask import Flask -from werkzeug.datastructures import MultiDict - -from webmentions_ssg.forms.sources import SourceFilterForm - - -def test_source_filter_form_accepts_get_params(app: Flask) -> None: - with app.test_request_context("/sources"): - form = SourceFilterForm( - formdata=MultiDict( - { - "status": "retrying", - "q": "example.org", - "date_from": "2026-11-08", - "date_to": "2026-11-14", - "sort": "webmentions", - "order": "asc", - "per_page": "37", - } - ), - meta={"csrf": False}, - ) - assert form.validate() - assert form.status.data == "retrying" - assert form.date_from.data is not None - assert form.date_to.data is not None - assert form.date_from.data.isoformat() == "2026-11-08" - assert form.date_to.data.isoformat() == "2026-11-14" - assert (form.sort.data, form.order.data, form.per_page.data) == ( - "webmentions", - "asc", - 37, - ) - assert "csrf_token" not in form._fields - - -@pytest.mark.parametrize( - "data", - [ - {"status": "unknown"}, - {"sort": "unknown"}, - {"order": "up"}, - {"per_page": "0"}, - {"per_page": "101"}, - {"per_page": "nope"}, - {"date_from": "2026-11-8"}, - {"date_to": "not-a-date"}, - {"date_from": "2026-11-15", "date_to": "2026-11-08"}, - ], -) -def test_source_filter_form_rejects_bad_get_params( - app: Flask, data: dict[str, str] -) -> None: - with app.test_request_context("/sources"): - form = SourceFilterForm(formdata=MultiDict(data), meta={"csrf": False}) - assert not form.validate() - assert form.errors - - -def test_source_filter_form_defaults_and_configured_maximum(app: Flask) -> None: - app.config["SOURCES_MAX_PAGE_SIZE"] = 10 - with app.test_request_context("/sources"): - form = SourceFilterForm(formdata=MultiDict(), meta={"csrf": False}) - assert form.validate() - assert ( - form.status.data, - form.sort.data, - form.order.data, - form.per_page.data, - ) == ("", "last_seen", "desc", 10) - form = SourceFilterForm( - formdata=MultiDict({"per_page": "11"}), meta={"csrf": False} - ) - assert not form.validate() diff --git a/tests/views/test_sent.py b/tests/views/test_sent.py new file mode 100644 index 0000000..9f9e65f --- /dev/null +++ b/tests/views/test_sent.py @@ -0,0 +1,708 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +import re +import uuid +from datetime import UTC, datetime, timedelta +from html import unescape +from http import HTTPStatus +from types import ModuleType +from unittest.mock import Mock +from urllib.parse import parse_qs, urlsplit + +import pytest +from bs4 import BeautifulSoup, Tag +from flask import Flask +from flask.testing import FlaskClient + +from webmentions_ssg import db +from webmentions_ssg.models.sources import ( + SentWebmention, + SentWebmentionStatus, + SourceDocument, +) +from webmentions_ssg.models.user import User + +from .helpers import ( + create_outgoing, + create_resendable_sent_webmention, + create_sent_source, + log_in, + mock_admin_action_form, +) + + +def _href(tag: Tag) -> str: + """Return an HTML link target, checking BeautifulSoup's attribute type.""" + href = tag.get("href") + assert isinstance(href, str) + return unescape(href) + + +@pytest.fixture +def sent_module(app: Flask) -> ModuleType: + _ = app + from webmentions_ssg.views import sent as module + + return module + + +@pytest.mark.parametrize("path", ["/sent", f"/sent/{uuid.uuid7()}"]) +def test_sent_views_require_login(client: FlaskClient, path: str) -> None: + response = client.get(path) + + assert response.status_code == HTTPStatus.FOUND + assert "/login" in response.headers["Location"] + + +def test_sent_lists_source_documents(app: Flask, client: FlaskClient) -> None: + log_in(app, client) + identifier = create_sent_source(app) + + response = client.get("/sent") + + assert response.status_code == HTTPStatus.OK + + html = response.get_data(as_text=True) + soup = BeautifulSoup(html, "html.parser") + + assert soup.h1 is not None + assert soup.h1.get_text(strip=True) == "Sent Webmentions" + assert "1 source document" in html + assert soup.select_one('form[action="/sent"]') is not None + assert "blog/example/index.html" in html + assert "https://dennisfink.me/blog/example/" in html + assert f"/sent/{identifier}" in html + + +def test_sent_source_documents_table_hides_secondary_columns_on_mobile( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + create_sent_source(app) + + response = client.get("/sent") + + assert response.status_code == HTTPStatus.OK + html = BeautifulSoup(response.get_data(as_text=True), "html.parser") + table = html.select_one(".table-responsive > table") + assert table is not None + headers = table.select("thead th") + cells = table.select("tbody tr:first-child td") + assert len(headers) == len(cells) == 7 + for index in (2, 4, 5, 6): # URL, revision, created, last seen + for cell in (headers[index], cells[index]): + assert {"d-none", "d-md-table-cell"} <= set( + cell.get_attribute_list("class") + ) + for index in (0, 1, 3): # Path, count, status stay visible + assert "d-none" not in cells[index].get_attribute_list("class") + assert "text-break" in cells[1].get_attribute_list("class") + + +def test_sent_source_lists_webmentions(app: Flask, client: FlaskClient) -> None: + log_in(app, client) + identifier = create_sent_source(app) + + response = client.get(f"/sent/{identifier}") + + assert response.status_code == HTTPStatus.OK + + html = response.get_data(as_text=True) + + assert "blog/example/index.html" in html + assert "https://dennisfink.me/blog/example/" in html + assert "Sent Webmention Source" in html + assert "Back to Sent Webmentions" in html + + first_target = "https://example.com/a" + second_target = "https://example.com/b" + + assert first_target in html + assert second_target in html + assert html.index(first_target) < html.index(second_target) + + assert "Webmention endpoint returned HTTP 400" in html + assert "https://example.com/webmention" in html + + +def test_sent_webmentions_table_keeps_status_and_actions_on_mobile( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + identifier = create_sent_source(app) + + response = client.get(f"/sent/{identifier}") + + assert response.status_code == HTTPStatus.OK + html = BeautifulSoup(response.get_data(as_text=True), "html.parser") + table = html.select_one(".table-responsive > table") + assert table is not None + headers = table.select("thead th") + cells = table.select("tbody tr:first-child td") + assert len(headers) == len(cells) == 7 + for index in (2, 3, 4, 5): # Active, revisions, delivery, activity + for cell in (headers[index], cells[index]): + assert {"d-none", "d-md-table-cell"} <= set( + cell.get_attribute_list("class") + ) + for index in (0, 1, 6): # Target, status, resend action stay visible + assert "d-none" not in cells[index].get_attribute_list("class") + assert cells[6].select_one("button[data-action]") is not None + + +def test_sent_source_returns_404_for_unknown_source( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + response = client.get(f"/sent/{uuid.uuid7()}") + assert response.status_code == HTTPStatus.NOT_FOUND + + +def test_rescan_sent_sources_requires_login(client: FlaskClient) -> None: + response = client.post("/sent/rescan") + + assert response.status_code == HTTPStatus.FOUND + assert "/login" in response.headers["Location"] + + +@pytest.mark.parametrize("valid", [False, True]) +def test_rescan_sent_sources( + app: Flask, + client: FlaskClient, + sent_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, + valid: bool, +) -> None: + log_in(app, client) + form = mock_admin_action_form(sent_module, monkeypatch, valid=valid) + manual_scan_sources = Mock() + monkeypatch.setattr(sent_module, "manual_scan_sources", manual_scan_sources) + + response = client.post("/sent/rescan") + + assert response.status_code == ( + HTTPStatus.FOUND if valid else HTTPStatus.BAD_REQUEST + ) + form.validate_on_submit.assert_called_once_with() + + if valid: + assert response.headers["Location"].endswith("/sent") + manual_scan_sources.assert_called_once_with() + else: + manual_scan_sources.assert_not_called() + + +def test_resend_sent_webmention_requires_login(client: FlaskClient) -> None: + response = client.post(f"/sent/{uuid.uuid7()}/{uuid.uuid7()}/resend") + + assert response.status_code == HTTPStatus.FOUND + assert "/login" in response.headers["Location"] + + +def test_resend_sent_webmention_rejects_invalid_form( + app: Flask, + client: FlaskClient, + sent_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + source_identifier, webmention_identifier = create_resendable_sent_webmention(app) + mock_admin_action_form(sent_module, monkeypatch, valid=False) + + response = client.post( + f"/sent/{source_identifier}/{webmention_identifier}/resend" + ) + + assert response.status_code == HTTPStatus.BAD_REQUEST + + +def test_resend_sent_webmention_returns_404_for_unknown_webmention( + app: Flask, + client: FlaskClient, + sent_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + source_identifier = create_sent_source(app) + mock_admin_action_form(sent_module, monkeypatch, valid=True) + + response = client.post(f"/sent/{source_identifier}/{uuid.uuid7()}/resend") + + assert response.status_code == HTTPStatus.NOT_FOUND + + +def test_resend_sent_webmention_rejects_wrong_source( + app: Flask, + client: FlaskClient, + sent_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + _, webmention_identifier = create_resendable_sent_webmention(app) + wrong_source_identifier = create_sent_source(app) + mock_admin_action_form(sent_module, monkeypatch, valid=True) + + response = client.post( + f"/sent/{wrong_source_identifier}/{webmention_identifier}/resend" + ) + + assert response.status_code == HTTPStatus.NOT_FOUND + + +def test_resend_sent_webmention_reopens_current_revision( + app: Flask, + client: FlaskClient, + sent_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + source_identifier, webmention_identifier = create_resendable_sent_webmention(app) + mock_admin_action_form(sent_module, monkeypatch, valid=True) + send_webmention = Mock() + monkeypatch.setattr(sent_module, "send_webmention", send_webmention) + + with app.app_context(): + webmention = db.session.get(SentWebmention, webmention_identifier) + assert webmention is not None + webmention.attempt_count = 6 + webmention.next_retry_at = datetime.now(UTC) + timedelta(hours=24) + db.session.commit() + + response = client.post( + f"/sent/{source_identifier}/{webmention_identifier}/resend" + ) + + assert response.status_code == HTTPStatus.FOUND + assert response.headers["Location"].endswith(f"/sent/{source_identifier}") + + with app.app_context(): + webmention = db.session.get(SentWebmention, webmention_identifier) + assert webmention is not None + assert webmention.desired_revision == 3 + assert webmention.processed_revision is None + assert webmention.attempt_count == 0 + assert webmention.next_retry_at is None + assert webmention.sent_revision == 2 + assert webmention.status is None + assert webmention.failure_reason is None + assert webmention.webmention_endpoint is None + assert webmention.endpoint_response_status is None + assert webmention.status_url is None + + send_webmention.assert_called_once_with(webmention_identifier) + + +@pytest.mark.parametrize( + ("status_url", "safe"), + [ + ("https://example.com/status", True), + ("http://example.com/status", True), + ("javascript:alert(1)", False), + ("data:text/html,evil", False), + ("ftp://example.com/status", False), + ], +) +def test_admin_only_links_safe_existing_status_urls( + app: Flask, client: FlaskClient, status_url: str, safe: bool +) -> None: + source_identifier, _ = create_outgoing(app, status_url=status_url) + with app.app_context(): + user = User(username="admin", password="test") + db.session.add(user) + db.session.commit() + user_id = user.get_id() + + with client.session_transaction() as session: + session["_user_id"] = str(user_id) + session["_fresh"] = True + + response = client.get(f"/sent/{source_identifier}") + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + assert (f'href="{status_url}"' in html) is safe + assert status_url in html + + +def make_source( + app: Flask, + name: str, + *, + statuses: tuple[SentWebmentionStatus | None, ...] = (SentWebmentionStatus.SENT,), + retrying: bool = False, + processed: bool = True, + deleted: bool = False, + last_seen_at: datetime | None = None, + revision: int = 1, +) -> uuid.UUID: + """Create distinguishable sources with outgoing rows for list filtering.""" + now = last_seen_at or datetime(2026, 11, 12, 12, tzinfo=UTC) + with app.app_context(): + source = SourceDocument( + path=f"blog/{name}/index.html", + url=f"https://source.example/{name}/", + content_hash="a" * 32, + revision=revision, + last_seen_at=now, + revised_at=now, + deleted_at=now if deleted else None, + ) + for number, status in enumerate(statuses): + source.sent_webmentions.append( + SentWebmention( + target=f"https://target.example/{name}/{number}", + linked=True, + desired_revision=revision, + processed_revision=revision if processed else None, + status=status, + attempt_count=2 if retrying else 0, + next_retry_at=now + timedelta(days=10) if retrying else None, + ) + ) + db.session.add(source) + db.session.commit() + return source.uuid + + +@pytest.mark.parametrize( + ("status", "expected"), + [ + ("failing", {"failed", "retrying", "stale", "multiple"}), + ("retrying", {"retrying"}), + ("deleted", {"deleted"}), + ], +) +def test_sent_filter_by_delivery_state_without_duplicate_rows( + app: Flask, client: FlaskClient, status: str, expected: set[str] +) -> None: + log_in(app, client) + make_source(app, "failed", statuses=(SentWebmentionStatus.FAILED,)) + make_source( + app, + "retrying", + statuses=(SentWebmentionStatus.FAILED,), + retrying=True, + processed=False, + ) + make_source(app, "stale", statuses=(SentWebmentionStatus.FAILED,), retrying=True) + make_source( + app, + "multiple", + statuses=(SentWebmentionStatus.FAILED, SentWebmentionStatus.FAILED), + ) + make_source(app, "deleted", deleted=True) + make_source(app, "sent") + make_source(app, "pending", statuses=(None,), processed=False) + + response = client.get("/sent", query_string={"status": status}) + assert response.status_code == HTTPStatus.OK + soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") + paths = [] + for row in soup.select("tbody tr"): + link = row.select_one("td a") + assert link is not None + paths.append(link.get_text(strip=True)) + assert set(paths) == {f"blog/{name}/index.html" for name in expected} + assert len(paths) == len(expected) + assert ( + soup.select_one(f'select[name="status"] option[value="{status}"][selected]') + is not None + ) + + +@pytest.mark.parametrize( + ("q", "expected"), + [ + ("blog/alpha", "alpha"), + ("source.example/alpha", "alpha"), + ("target.example/alpha", "alpha"), + ("alpha/0", "alpha"), + ("alpha%", None), + ], +) +def test_sent_search_matches_path_source_url_and_sent_target( + app: Flask, client: FlaskClient, q: str, expected: str | None +) -> None: + log_in(app, client) + make_source(app, "alpha") + make_source(app, "beta") + + response = client.get("/sent", query_string={"q": f" {q} "}) + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + assert ("blog/alpha/index.html" in html) == (expected == "alpha") + assert "blog/beta/index.html" not in html + if expected is None: + assert "No source documents match these filters." in html + + +def test_sent_filter_by_inclusive_last_seen_dates( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + make_source(app, "old", last_seen_at=datetime(2026, 11, 7, tzinfo=UTC)) + make_source(app, "included", last_seen_at=datetime(2026, 11, 10, 22, tzinfo=UTC)) + make_source(app, "new", last_seen_at=datetime(2026, 11, 15, tzinfo=UTC)) + response = client.get( + "/sent", query_string={"date_from": "2026-11-08", "date_to": "2026-11-10"} + ) + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + assert "blog/included/index.html" in html + assert "blog/old/index.html" not in html + assert "blog/new/index.html" not in html + + +@pytest.mark.parametrize( + "sort", ["path", "url", "webmentions", "revision", "created", "last_seen", "status"] +) +def test_sent_sort_links_preserve_filters( + app: Flask, client: FlaskClient, sort: str +) -> None: + log_in(app, client) + make_source(app, "alpha", statuses=(SentWebmentionStatus.FAILED,)) + response = client.get( + "/sent", + query_string={ + "status": "failing", + "q": "alpha", + "date_from": "2026-11-01", + "sort": sort, + "order": "asc", + "per_page": 37, + }, + ) + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + soup = BeautifulSoup(html, "html.parser") + assert soup.select_one('th[aria-sort="ascending"]') is not None + links = [ + parse_qs(urlsplit(_href(a)).query) + for a in soup.select("th a[href]") + if urlsplit(_href(a)).path == "/sent" + ] + assert len(links) == 7 + for link in links: + assert link["status"] == ["failing"] + assert link["q"] == ["alpha"] + assert link["date_from"] == ["2026-11-01"] + assert link["per_page"] == ["37"] + assert link["sort"][0] in ( + "path", + "url", + "webmentions", + "revision", + "created", + "last_seen", + "status", + ) + assert any(link["sort"] == [sort] and link["order"] == ["desc"] for link in links) + + +def test_sent_sort_orders_by_count_and_path(app: Flask, client: FlaskClient) -> None: + log_in(app, client) + make_source(app, "z", statuses=(SentWebmentionStatus.SENT,)) + make_source( + app, "a", statuses=(SentWebmentionStatus.SENT, SentWebmentionStatus.FAILED) + ) + for sort, order, expected_first in ( + ("webmentions", "desc", "a"), + ("webmentions", "asc", "z"), + ("path", "asc", "a"), + ("path", "desc", "z"), + ): + response = client.get("/sent", query_string={"sort": sort, "order": order}) + assert response.status_code == HTTPStatus.OK + soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") + link = soup.select_one("tbody tr td a") + assert link is not None + assert link.get_text(strip=True) == f"blog/{expected_first}/index.html" + + +def test_sent_filter_pagination_and_navigation_preserve_parameters( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + for number in range(28): + make_source(app, f"failed-{number:02}", statuses=(SentWebmentionStatus.FAILED,)) + filters = { + "status": "failing", + "q": "failed", + "sort": "path", + "order": "asc", + "per_page": "10", + } + response = client.get("/sent", query_string={**filters, "page": "2"}) + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + assert "28 source documents" in html + soup = BeautifulSoup(html, "html.parser") + assert len(soup.select("tbody tr")) == 10 + urls = [_href(a) for a in soup.select("a[href]")] + assert any( + urlsplit(url).path == "/sent" + and parse_qs(urlsplit(url).query) + == {**{k: [v] for k, v in filters.items()}, "page": ["3"]} + for url in urls + ) + detail_urls = [ + url for url in urls if re.search(r"/sent/[0-9a-f-]{36}", urlsplit(url).path) + ] + assert detail_urls + assert parse_qs(urlsplit(detail_urls[0]).query) == { + **{k: [v] for k, v in filters.items()}, + "source_page": ["2"], + } + detail = client.get(detail_urls[0]) + assert detail.status_code == HTTPStatus.OK + back = BeautifulSoup(detail.get_data(as_text=True), "html.parser").select_one( + 'a[href*="/sent?"]' + ) + assert back is not None + assert parse_qs(urlsplit(_href(back)).query) == { + **{k: [v] for k, v in filters.items()}, + "page": ["2"], + } + + +@pytest.mark.parametrize( + "invalid", + [ + "status=unknown", + "sort=unsupported", + "order=reverse", + "per_page=0", + "per_page=101", + "per_page=abc", + "date_from=2026-11-8", + "date_to=bad", + "date_from=2026-11-10&date_to=2026-11-01", + ], +) +def test_sent_bad_filters_return_400( + app: Flask, client: FlaskClient, invalid: str +) -> None: + log_in(app, client) + assert client.get(f"/sent?{invalid}").status_code == HTTPStatus.BAD_REQUEST + + +def test_rescan_sources_preserves_filter_query( + app: Flask, + client: FlaskClient, + sent_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + mock_admin_action_form(sent_module, monkeypatch, valid=True) + scan = Mock() + monkeypatch.setattr(sent_module, "manual_scan_sources", scan) + response = client.post( + "/sent/rescan", + query_string={"status": "retrying", "per_page": 37, "page": 2}, + ) + assert response.status_code == HTTPStatus.FOUND + assert parse_qs(urlsplit(response.headers["Location"]).query) == { + "status": ["retrying"], + "per_page": ["37"], + "page": ["2"], + } + scan.assert_called_once_with() + + +def test_sent_show_failed_and_retrying_counts_and_retry_deadline( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + identifier = make_source( + app, + "retry-badges", + statuses=(SentWebmentionStatus.FAILED,), + retrying=True, + processed=False, + ) + response = client.get("/sent", query_string={"status": "retrying"}) + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + assert "1 failed" in html + assert "1 awaiting retry" in html + + response = client.get(f"/sent/{identifier}") + assert response.status_code == HTTPStatus.OK + assert "Retry scheduled after attempt 2:" in response.get_data(as_text=True) + + +def test_resend_preserves_source_filter_context( + app: Flask, + client: FlaskClient, + sent_module: ModuleType, + monkeypatch: pytest.MonkeyPatch, +) -> None: + log_in(app, client) + source_id, mention_id = create_resendable_sent_webmention(app) + mock_admin_action_form(sent_module, monkeypatch, valid=True) + send = Mock() + monkeypatch.setattr(sent_module, "send_webmention", send) + + response = client.post( + f"/sent/{source_id}/{mention_id}/resend", + query_string={ + "status": "failing", + "q": "resend", + "sort": "path", + "source_page": 2, + "page": 3, + }, + ) + assert response.status_code == HTTPStatus.FOUND + assert urlsplit(response.headers["Location"]).path == f"/sent/{source_id}" + assert parse_qs(urlsplit(response.headers["Location"]).query) == { + "status": ["failing"], + "q": ["resend"], + "sort": ["path"], + "source_page": ["2"], + "page": ["3"], + } + send.assert_called_once_with(mention_id) + + +def test_sent_show_sql_statistics_with_zero_webmentions( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + make_source( + app, + "mixed-stats", + statuses=( + SentWebmentionStatus.SENT, + SentWebmentionStatus.SENT, + SentWebmentionStatus.FAILED, + SentWebmentionStatus.NO_ENDPOINT, + ), + ) + make_source(app, "empty-stats", statuses=()) + + response = client.get("/sent", query_string={"sort": "path", "order": "asc"}) + assert response.status_code == HTTPStatus.OK + rows = BeautifulSoup(response.get_data(as_text=True), "html.parser").select( + "tbody tr" + ) + assert len(rows) == 2 + assert "0 Total" in rows[0].get_text(" ", strip=True) + assert " sent" not in rows[0].get_text(" ", strip=True) + mixed = rows[1].get_text(" ", strip=True) + assert "4 Total" in mixed + assert "2 sent" in mixed + assert "1 failed" in mixed + assert "awaiting retry" not in mixed + + +def test_sent_empty_filter_result_keeps_rescan_modal( + app: Flask, client: FlaskClient +) -> None: + log_in(app, client) + response = client.get("/sent", query_string={"status": "failing"}) + assert response.status_code == HTTPStatus.OK + html = response.get_data(as_text=True) + assert "No source documents match these filters." in html + assert 'id="confirmation-modal"' in html diff --git a/tests/views/test_sources.py b/tests/views/test_sources.py deleted file mode 100644 index 8f5672c..0000000 --- a/tests/views/test_sources.py +++ /dev/null @@ -1,701 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -import re -import uuid -from datetime import UTC, datetime, timedelta -from html import unescape -from http import HTTPStatus -from types import ModuleType -from unittest.mock import Mock -from urllib.parse import parse_qs, urlsplit - -import pytest -from bs4 import BeautifulSoup, Tag -from flask import Flask -from flask.testing import FlaskClient - -from webmentions_ssg import db -from webmentions_ssg.models.sources import ( - SentWebmention, - SentWebmentionStatus, - SourceDocument, -) -from webmentions_ssg.models.user import User - -from .helpers import ( - create_outgoing, - create_resendable_sent_webmention, - create_sent_source, - log_in, - mock_admin_action_form, -) - - -def _href(tag: Tag) -> str: - """Return an HTML link target, checking BeautifulSoup's attribute type.""" - href = tag.get("href") - assert isinstance(href, str) - return unescape(href) - - -@pytest.fixture -def sources_module(app: Flask) -> ModuleType: - _ = app - from webmentions_ssg.views import sources as module - - return module - - -@pytest.mark.parametrize("path", ["/sources", f"/sources/{uuid.uuid7()}"]) -def test_sent_views_require_login(client: FlaskClient, path: str) -> None: - response = client.get(path) - - assert response.status_code == HTTPStatus.FOUND - assert "/login" in response.headers["Location"] - - -def test_sent_lists_sources(app: Flask, client: FlaskClient) -> None: - log_in(app, client) - identifier = create_sent_source(app) - - response = client.get("/sources") - - assert response.status_code == HTTPStatus.OK - - html = response.get_data(as_text=True) - - assert "blog/example/index.html" in html - assert "https://dennisfink.me/blog/example/" in html - assert f"/sources/{identifier}" in html - - -def test_sent_sources_table_hides_secondary_columns_on_mobile( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - create_sent_source(app) - - response = client.get("/sources") - - assert response.status_code == HTTPStatus.OK - html = BeautifulSoup(response.get_data(as_text=True), "html.parser") - table = html.select_one(".table-responsive > table") - assert table is not None - headers = table.select("thead th") - cells = table.select("tbody tr:first-child td") - assert len(headers) == len(cells) == 7 - for index in (2, 4, 5, 6): # URL, revision, created, last seen - for cell in (headers[index], cells[index]): - assert {"d-none", "d-md-table-cell"} <= set( - cell.get_attribute_list("class") - ) - for index in (0, 1, 3): # Path, count, status stay visible - assert "d-none" not in cells[index].get_attribute_list("class") - assert "text-break" in cells[1].get_attribute_list("class") - - -def test_sent_source_lists_webmentions(app: Flask, client: FlaskClient) -> None: - log_in(app, client) - identifier = create_sent_source(app) - - response = client.get(f"/sources/{identifier}") - - assert response.status_code == HTTPStatus.OK - - html = response.get_data(as_text=True) - - assert "blog/example/index.html" in html - assert "https://dennisfink.me/blog/example/" in html - - first_target = "https://example.com/a" - second_target = "https://example.com/b" - - assert first_target in html - assert second_target in html - assert html.index(first_target) < html.index(second_target) - - assert "Webmention endpoint returned HTTP 400" in html - assert "https://example.com/webmention" in html - - -def test_sent_webmentions_table_keeps_status_and_actions_on_mobile( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - identifier = create_sent_source(app) - - response = client.get(f"/sources/{identifier}") - - assert response.status_code == HTTPStatus.OK - html = BeautifulSoup(response.get_data(as_text=True), "html.parser") - table = html.select_one(".table-responsive > table") - assert table is not None - headers = table.select("thead th") - cells = table.select("tbody tr:first-child td") - assert len(headers) == len(cells) == 7 - for index in (2, 3, 4, 5): # Active, revisions, delivery, activity - for cell in (headers[index], cells[index]): - assert {"d-none", "d-md-table-cell"} <= set( - cell.get_attribute_list("class") - ) - for index in (0, 1, 6): # Target, status, resend action stay visible - assert "d-none" not in cells[index].get_attribute_list("class") - assert cells[6].select_one("button[data-action]") is not None - - -def test_sent_source_returns_404_for_unknown_source( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - response = client.get(f"/sources/{uuid.uuid7()}") - assert response.status_code == HTTPStatus.NOT_FOUND - - -def test_rescan_sent_sources_requires_login(client: FlaskClient) -> None: - response = client.post("/sources/rescan") - - assert response.status_code == HTTPStatus.FOUND - assert "/login" in response.headers["Location"] - - -@pytest.mark.parametrize("valid", [False, True]) -def test_rescan_sent_sources( - app: Flask, - client: FlaskClient, - sources_module: ModuleType, - monkeypatch: pytest.MonkeyPatch, - valid: bool, -) -> None: - log_in(app, client) - form = mock_admin_action_form(sources_module, monkeypatch, valid=valid) - manual_scan_sources = Mock() - monkeypatch.setattr(sources_module, "manual_scan_sources", manual_scan_sources) - - response = client.post("/sources/rescan") - - assert response.status_code == ( - HTTPStatus.FOUND if valid else HTTPStatus.BAD_REQUEST - ) - form.validate_on_submit.assert_called_once_with() - - if valid: - assert response.headers["Location"].endswith("/sources") - manual_scan_sources.assert_called_once_with() - else: - manual_scan_sources.assert_not_called() - - -def test_resend_sent_webmention_requires_login(client: FlaskClient) -> None: - response = client.post(f"/sources/{uuid.uuid7()}/{uuid.uuid7()}/resend") - - assert response.status_code == HTTPStatus.FOUND - assert "/login" in response.headers["Location"] - - -def test_resend_sent_webmention_rejects_invalid_form( - app: Flask, - client: FlaskClient, - sources_module: ModuleType, - monkeypatch: pytest.MonkeyPatch, -) -> None: - log_in(app, client) - source_identifier, webmention_identifier = create_resendable_sent_webmention(app) - mock_admin_action_form(sources_module, monkeypatch, valid=False) - - response = client.post( - f"/sources/{source_identifier}/{webmention_identifier}/resend" - ) - - assert response.status_code == HTTPStatus.BAD_REQUEST - - -def test_resend_sent_webmention_returns_404_for_unknown_webmention( - app: Flask, - client: FlaskClient, - sources_module: ModuleType, - monkeypatch: pytest.MonkeyPatch, -) -> None: - log_in(app, client) - source_identifier = create_sent_source(app) - mock_admin_action_form(sources_module, monkeypatch, valid=True) - - response = client.post(f"/sources/{source_identifier}/{uuid.uuid7()}/resend") - - assert response.status_code == HTTPStatus.NOT_FOUND - - -def test_resend_sent_webmention_rejects_wrong_source( - app: Flask, - client: FlaskClient, - sources_module: ModuleType, - monkeypatch: pytest.MonkeyPatch, -) -> None: - log_in(app, client) - _, webmention_identifier = create_resendable_sent_webmention(app) - wrong_source_identifier = create_sent_source(app) - mock_admin_action_form(sources_module, monkeypatch, valid=True) - - response = client.post( - f"/sources/{wrong_source_identifier}/{webmention_identifier}/resend" - ) - - assert response.status_code == HTTPStatus.NOT_FOUND - - -def test_resend_sent_webmention_reopens_current_revision( - app: Flask, - client: FlaskClient, - sources_module: ModuleType, - monkeypatch: pytest.MonkeyPatch, -) -> None: - log_in(app, client) - source_identifier, webmention_identifier = create_resendable_sent_webmention(app) - mock_admin_action_form(sources_module, monkeypatch, valid=True) - send_webmention = Mock() - monkeypatch.setattr(sources_module, "send_webmention", send_webmention) - - with app.app_context(): - webmention = db.session.get(SentWebmention, webmention_identifier) - assert webmention is not None - webmention.attempt_count = 6 - webmention.next_retry_at = datetime.now(UTC) + timedelta(hours=24) - db.session.commit() - - response = client.post( - f"/sources/{source_identifier}/{webmention_identifier}/resend" - ) - - assert response.status_code == HTTPStatus.FOUND - assert response.headers["Location"].endswith(f"/sources/{source_identifier}") - - with app.app_context(): - webmention = db.session.get(SentWebmention, webmention_identifier) - assert webmention is not None - assert webmention.desired_revision == 3 - assert webmention.processed_revision is None - assert webmention.attempt_count == 0 - assert webmention.next_retry_at is None - assert webmention.sent_revision == 2 - assert webmention.status is None - assert webmention.failure_reason is None - assert webmention.webmention_endpoint is None - assert webmention.endpoint_response_status is None - assert webmention.status_url is None - - send_webmention.assert_called_once_with(webmention_identifier) - - -@pytest.mark.parametrize( - ("status_url", "safe"), - [ - ("https://example.com/status", True), - ("http://example.com/status", True), - ("javascript:alert(1)", False), - ("data:text/html,evil", False), - ("ftp://example.com/status", False), - ], -) -def test_admin_only_links_safe_existing_status_urls( - app: Flask, client: FlaskClient, status_url: str, safe: bool -) -> None: - source_identifier, _ = create_outgoing(app, status_url=status_url) - with app.app_context(): - user = User(username="admin", password="test") - db.session.add(user) - db.session.commit() - user_id = user.get_id() - - with client.session_transaction() as session: - session["_user_id"] = str(user_id) - session["_fresh"] = True - - response = client.get(f"/sources/{source_identifier}") - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - assert (f'href="{status_url}"' in html) is safe - assert status_url in html - - -def make_source( - app: Flask, - name: str, - *, - statuses: tuple[SentWebmentionStatus | None, ...] = (SentWebmentionStatus.SENT,), - retrying: bool = False, - processed: bool = True, - deleted: bool = False, - last_seen_at: datetime | None = None, - revision: int = 1, -) -> uuid.UUID: - """Create distinguishable sources with outgoing rows for list filtering.""" - now = last_seen_at or datetime(2026, 11, 12, 12, tzinfo=UTC) - with app.app_context(): - source = SourceDocument( - path=f"blog/{name}/index.html", - url=f"https://source.example/{name}/", - content_hash="a" * 32, - revision=revision, - last_seen_at=now, - revised_at=now, - deleted_at=now if deleted else None, - ) - for number, status in enumerate(statuses): - source.sent_webmentions.append( - SentWebmention( - target=f"https://target.example/{name}/{number}", - linked=True, - desired_revision=revision, - processed_revision=revision if processed else None, - status=status, - attempt_count=2 if retrying else 0, - next_retry_at=now + timedelta(days=10) if retrying else None, - ) - ) - db.session.add(source) - db.session.commit() - return source.uuid - - -@pytest.mark.parametrize( - ("status", "expected"), - [ - ("failing", {"failed", "retrying", "stale", "multiple"}), - ("retrying", {"retrying"}), - ("deleted", {"deleted"}), - ], -) -def test_sources_filter_by_delivery_state_without_duplicate_rows( - app: Flask, client: FlaskClient, status: str, expected: set[str] -) -> None: - log_in(app, client) - make_source(app, "failed", statuses=(SentWebmentionStatus.FAILED,)) - make_source( - app, - "retrying", - statuses=(SentWebmentionStatus.FAILED,), - retrying=True, - processed=False, - ) - make_source(app, "stale", statuses=(SentWebmentionStatus.FAILED,), retrying=True) - make_source( - app, - "multiple", - statuses=(SentWebmentionStatus.FAILED, SentWebmentionStatus.FAILED), - ) - make_source(app, "deleted", deleted=True) - make_source(app, "sent") - make_source(app, "pending", statuses=(None,), processed=False) - - response = client.get("/sources", query_string={"status": status}) - assert response.status_code == HTTPStatus.OK - soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") - paths = [] - for row in soup.select("tbody tr"): - link = row.select_one("td a") - assert link is not None - paths.append(link.get_text(strip=True)) - assert set(paths) == {f"blog/{name}/index.html" for name in expected} - assert len(paths) == len(expected) - assert ( - soup.select_one(f'select[name="status"] option[value="{status}"][selected]') - is not None - ) - - -@pytest.mark.parametrize( - ("q", "expected"), - [ - ("blog/alpha", "alpha"), - ("source.example/alpha", "alpha"), - ("target.example/alpha", "alpha"), - ("alpha/0", "alpha"), - ("alpha%", None), - ], -) -def test_sources_search_matches_path_source_url_and_sent_target( - app: Flask, client: FlaskClient, q: str, expected: str | None -) -> None: - log_in(app, client) - make_source(app, "alpha") - make_source(app, "beta") - - response = client.get("/sources", query_string={"q": f" {q} "}) - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - assert ("blog/alpha/index.html" in html) == (expected == "alpha") - assert "blog/beta/index.html" not in html - if expected is None: - assert "No sources match these filters." in html - - -def test_sources_filter_by_inclusive_last_seen_dates( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - make_source(app, "old", last_seen_at=datetime(2026, 11, 7, tzinfo=UTC)) - make_source(app, "included", last_seen_at=datetime(2026, 11, 10, 22, tzinfo=UTC)) - make_source(app, "new", last_seen_at=datetime(2026, 11, 15, tzinfo=UTC)) - response = client.get( - "/sources", query_string={"date_from": "2026-11-08", "date_to": "2026-11-10"} - ) - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - assert "blog/included/index.html" in html - assert "blog/old/index.html" not in html - assert "blog/new/index.html" not in html - - -@pytest.mark.parametrize( - "sort", ["path", "url", "webmentions", "revision", "created", "last_seen", "status"] -) -def test_sources_sort_links_preserve_filters( - app: Flask, client: FlaskClient, sort: str -) -> None: - log_in(app, client) - make_source(app, "alpha", statuses=(SentWebmentionStatus.FAILED,)) - response = client.get( - "/sources", - query_string={ - "status": "failing", - "q": "alpha", - "date_from": "2026-11-01", - "sort": sort, - "order": "asc", - "per_page": 37, - }, - ) - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - soup = BeautifulSoup(html, "html.parser") - assert soup.select_one('th[aria-sort="ascending"]') is not None - links = [ - parse_qs(urlsplit(_href(a)).query) - for a in soup.select("th a[href]") - if urlsplit(_href(a)).path == "/sources" - ] - assert len(links) == 7 - for link in links: - assert link["status"] == ["failing"] - assert link["q"] == ["alpha"] - assert link["date_from"] == ["2026-11-01"] - assert link["per_page"] == ["37"] - assert link["sort"][0] in ( - "path", - "url", - "webmentions", - "revision", - "created", - "last_seen", - "status", - ) - assert any(link["sort"] == [sort] and link["order"] == ["desc"] for link in links) - - -def test_sources_sort_orders_by_count_and_path(app: Flask, client: FlaskClient) -> None: - log_in(app, client) - make_source(app, "z", statuses=(SentWebmentionStatus.SENT,)) - make_source( - app, "a", statuses=(SentWebmentionStatus.SENT, SentWebmentionStatus.FAILED) - ) - for sort, order, expected_first in ( - ("webmentions", "desc", "a"), - ("webmentions", "asc", "z"), - ("path", "asc", "a"), - ("path", "desc", "z"), - ): - response = client.get("/sources", query_string={"sort": sort, "order": order}) - assert response.status_code == HTTPStatus.OK - soup = BeautifulSoup(response.get_data(as_text=True), "html.parser") - link = soup.select_one("tbody tr td a") - assert link is not None - assert link.get_text(strip=True) == f"blog/{expected_first}/index.html" - - -def test_sources_filter_pagination_and_navigation_preserve_parameters( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - for number in range(28): - make_source(app, f"failed-{number:02}", statuses=(SentWebmentionStatus.FAILED,)) - filters = { - "status": "failing", - "q": "failed", - "sort": "path", - "order": "asc", - "per_page": "10", - } - response = client.get("/sources", query_string={**filters, "page": "2"}) - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - assert "28 sources" in html - soup = BeautifulSoup(html, "html.parser") - assert len(soup.select("tbody tr")) == 10 - urls = [_href(a) for a in soup.select("a[href]")] - assert any( - urlsplit(url).path == "/sources" - and parse_qs(urlsplit(url).query) - == {**{k: [v] for k, v in filters.items()}, "page": ["3"]} - for url in urls - ) - detail_urls = [ - url for url in urls if re.search(r"/sources/[0-9a-f-]{36}", urlsplit(url).path) - ] - assert detail_urls - assert parse_qs(urlsplit(detail_urls[0]).query) == { - **{k: [v] for k, v in filters.items()}, - "source_page": ["2"], - } - detail = client.get(detail_urls[0]) - assert detail.status_code == HTTPStatus.OK - back = BeautifulSoup(detail.get_data(as_text=True), "html.parser").select_one( - 'a[href*="/sources?"]' - ) - assert back is not None - assert parse_qs(urlsplit(_href(back)).query) == { - **{k: [v] for k, v in filters.items()}, - "page": ["2"], - } - - -@pytest.mark.parametrize( - "invalid", - [ - "status=unknown", - "sort=unsupported", - "order=reverse", - "per_page=0", - "per_page=101", - "per_page=abc", - "date_from=2026-11-8", - "date_to=bad", - "date_from=2026-11-10&date_to=2026-11-01", - ], -) -def test_sources_bad_filters_return_400( - app: Flask, client: FlaskClient, invalid: str -) -> None: - log_in(app, client) - assert client.get(f"/sources?{invalid}").status_code == HTTPStatus.BAD_REQUEST - - -def test_rescan_sources_preserves_filter_query( - app: Flask, - client: FlaskClient, - sources_module: ModuleType, - monkeypatch: pytest.MonkeyPatch, -) -> None: - log_in(app, client) - mock_admin_action_form(sources_module, monkeypatch, valid=True) - scan = Mock() - monkeypatch.setattr(sources_module, "manual_scan_sources", scan) - response = client.post( - "/sources/rescan", - query_string={"status": "retrying", "per_page": 37, "page": 2}, - ) - assert response.status_code == HTTPStatus.FOUND - assert parse_qs(urlsplit(response.headers["Location"]).query) == { - "status": ["retrying"], - "per_page": ["37"], - "page": ["2"], - } - scan.assert_called_once_with() - - -def test_sources_show_failed_and_retrying_counts_and_retry_deadline( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - identifier = make_source( - app, - "retry-badges", - statuses=(SentWebmentionStatus.FAILED,), - retrying=True, - processed=False, - ) - response = client.get("/sources", query_string={"status": "retrying"}) - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - assert "1 failed" in html - assert "1 awaiting retry" in html - - response = client.get(f"/sources/{identifier}") - assert response.status_code == HTTPStatus.OK - assert "Retry scheduled after attempt 2:" in response.get_data(as_text=True) - - -def test_resend_preserves_source_filter_context( - app: Flask, - client: FlaskClient, - sources_module: ModuleType, - monkeypatch: pytest.MonkeyPatch, -) -> None: - log_in(app, client) - source_id, mention_id = create_resendable_sent_webmention(app) - mock_admin_action_form(sources_module, monkeypatch, valid=True) - send = Mock() - monkeypatch.setattr(sources_module, "send_webmention", send) - - response = client.post( - f"/sources/{source_id}/{mention_id}/resend", - query_string={ - "status": "failing", - "q": "resend", - "sort": "path", - "source_page": 2, - "page": 3, - }, - ) - assert response.status_code == HTTPStatus.FOUND - assert urlsplit(response.headers["Location"]).path == f"/sources/{source_id}" - assert parse_qs(urlsplit(response.headers["Location"]).query) == { - "status": ["failing"], - "q": ["resend"], - "sort": ["path"], - "source_page": ["2"], - "page": ["3"], - } - send.assert_called_once_with(mention_id) - - -def test_sources_show_sql_statistics_with_zero_webmentions( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - make_source( - app, - "mixed-stats", - statuses=( - SentWebmentionStatus.SENT, - SentWebmentionStatus.SENT, - SentWebmentionStatus.FAILED, - SentWebmentionStatus.NO_ENDPOINT, - ), - ) - make_source(app, "empty-stats", statuses=()) - - response = client.get("/sources", query_string={"sort": "path", "order": "asc"}) - assert response.status_code == HTTPStatus.OK - rows = BeautifulSoup(response.get_data(as_text=True), "html.parser").select( - "tbody tr" - ) - assert len(rows) == 2 - assert "0 Total" in rows[0].get_text(" ", strip=True) - assert " sent" not in rows[0].get_text(" ", strip=True) - mixed = rows[1].get_text(" ", strip=True) - assert "4 Total" in mixed - assert "2 sent" in mixed - assert "1 failed" in mixed - assert "awaiting retry" not in mixed - - -def test_sources_empty_filter_result_keeps_rescan_modal( - app: Flask, client: FlaskClient -) -> None: - log_in(app, client) - response = client.get("/sources", query_string={"status": "failing"}) - assert response.status_code == HTTPStatus.OK - html = response.get_data(as_text=True) - assert "No sources match these filters." in html - assert 'id="confirmation-modal"' in html diff --git a/webmentions_ssg/forms/sent.py b/webmentions_ssg/forms/sent.py new file mode 100644 index 0000000..65a2708 --- /dev/null +++ b/webmentions_ssg/forms/sent.py @@ -0,0 +1,81 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Filters for the sent Webmentions page.""" + +from flask import current_app +from flask_wtf import FlaskForm +from wtforms import ( + DateField, + HiddenField, + IntegerField, + SearchField, + SelectField, + ValidationError, +) +from wtforms.validators import AnyOf, NumberRange, Optional + +from .validators import CanonicalDate + +SENT_STATUS_CHOICES = { + "": "All source documents", + "failing": "Failing sends", + "retrying": "Awaiting retry", + "deleted": "Deleted source documents", +} + + +def default_sent_page_size() -> int: + """Use 25 rows unless the configured maximum is lower.""" + return min(25, current_app.config["SOURCES_MAX_PAGE_SIZE"]) + + +class SentFilterForm(FlaskForm): + """GET filters for sent Webmentions grouped by source document.""" + + status = SelectField("Status", choices=SENT_STATUS_CHOICES, default="") + q = SearchField("Path or URL") + date_from = DateField( + "Last seen from (UTC)", validators=[Optional(), CanonicalDate()] + ) + date_to = DateField("Last seen to (UTC)", validators=[Optional(), CanonicalDate()]) + sort = HiddenField( + default="last_seen", + validators=[ + AnyOf( + ( + "path", + "url", + "webmentions", + "revision", + "created", + "last_seen", + "status", + ) + ) + ], + ) + order = HiddenField(default="desc", validators=[AnyOf(("asc", "desc"))]) + per_page = IntegerField( + "Rows per page", + default=default_sent_page_size, + validators=[NumberRange(min=1)], + ) + + def validate_per_page(self, field: IntegerField) -> None: + """Enforce the deployment's maximum source-document page size.""" + if ( + field.data is not None + and field.data > current_app.config["SOURCES_MAX_PAGE_SIZE"] + ): + raise ValidationError("Rows per page exceeds the configured maximum.") + + def validate_date_to(self, field: DateField) -> None: + """Require an end date on or after the start date.""" + if ( + self.date_from.data is not None + and field.data is not None + and field.data < self.date_from.data + ): + raise ValidationError("End date must be on or after start date.") diff --git a/webmentions_ssg/forms/sources.py b/webmentions_ssg/forms/sources.py deleted file mode 100644 index 04afe77..0000000 --- a/webmentions_ssg/forms/sources.py +++ /dev/null @@ -1,81 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Source document filtering form.""" - -from flask import current_app -from flask_wtf import FlaskForm -from wtforms import ( - DateField, - HiddenField, - IntegerField, - SearchField, - SelectField, - ValidationError, -) -from wtforms.validators import AnyOf, NumberRange, Optional - -from .validators import CanonicalDate - -SOURCE_STATUS_CHOICES = { - "": "All sources", - "failing": "Failing sends", - "retrying": "Awaiting retry", - "deleted": "Deleted sources", -} - - -def default_source_page_size() -> int: - """Use 25 rows unless the configured maximum is lower.""" - return min(25, current_app.config["SOURCES_MAX_PAGE_SIZE"]) - - -class SourceFilterForm(FlaskForm): - """GET filters for the source document list.""" - - status = SelectField("Status", choices=SOURCE_STATUS_CHOICES, default="") - q = SearchField("Path or URL") - date_from = DateField( - "Last seen from (UTC)", validators=[Optional(), CanonicalDate()] - ) - date_to = DateField("Last seen to (UTC)", validators=[Optional(), CanonicalDate()]) - sort = HiddenField( - default="last_seen", - validators=[ - AnyOf( - ( - "path", - "url", - "webmentions", - "revision", - "created", - "last_seen", - "status", - ) - ) - ], - ) - order = HiddenField(default="desc", validators=[AnyOf(("asc", "desc"))]) - per_page = IntegerField( - "Rows per page", - default=default_source_page_size, - validators=[NumberRange(min=1)], - ) - - def validate_per_page(self, field: IntegerField) -> None: - """Enforce the deployment's maximum source-list page size.""" - if ( - field.data is not None - and field.data > current_app.config["SOURCES_MAX_PAGE_SIZE"] - ): - raise ValidationError("Rows per page exceeds the configured maximum.") - - def validate_date_to(self, field: DateField) -> None: - """Require an end date on or after the start date.""" - if ( - self.date_from.data is not None - and field.data is not None - and field.data < self.date_from.data - ): - raise ValidationError("End date must be on or after start date.") diff --git a/webmentions_ssg/templates/base.html b/webmentions_ssg/templates/base.html index 17c06d2..e21c663 100644 --- a/webmentions_ssg/templates/base.html +++ b/webmentions_ssg/templates/base.html @@ -49,8 +49,8 @@ SPDX-License-Identifier: BSD-3-Clause href='{{ url_for("webmentions.received_list") }}'>Received Moderation - Sent + Sent User

{{ sent_total }} total

diff --git a/webmentions_ssg/templates/sent.html b/webmentions_ssg/templates/sent.html index 46296b8..9db0156 100644 --- a/webmentions_ssg/templates/sent.html +++ b/webmentions_ssg/templates/sent.html @@ -22,9 +22,9 @@ SPDX-License-Identifier: BSD-3-Clause {% block content %}
-

Sources

+

Sent Webmentions

- {{ sources.total }} sources + {{ sources.total }} source document{{ "s" if sources.total != 1 else "" }}
- @@ -64,7 +64,7 @@ SPDX-License-Identifier: BSD-3-Clause
- Clear
@@ -99,7 +99,7 @@ SPDX-License-Identifier: BSD-3-Clause {{ status_text }} - {{ source.path }} + {{ source.path }} {{ source.url }} @@ -142,9 +142,9 @@ SPDX-License-Identifier: BSD-3-Clause {% endif %} {% else %} {% if filters %} -

No sources match these filters.

+

No source documents match these filters.

{% else %} -

No Webmention sources have been discovered yet.

+

No source documents with outgoing Webmentions have been discovered yet.

{% endif %} {% endif %} {% include "includes/confirmation_modal.html" %} diff --git a/webmentions_ssg/templates/sent_source.html b/webmentions_ssg/templates/sent_source.html index 04f9d07..a24bde3 100644 --- a/webmentions_ssg/templates/sent_source.html +++ b/webmentions_ssg/templates/sent_source.html @@ -26,10 +26,10 @@ SPDX-License-Identifier: BSD-3-Clause {% block content %}
-

Source Details

+

Sent Webmention Source

diff --git a/webmentions_ssg/views/__init__.py b/webmentions_ssg/views/__init__.py index 5abaa14..1701838 100644 --- a/webmentions_ssg/views/__init__.py +++ b/webmentions_ssg/views/__init__.py @@ -4,7 +4,7 @@ """Collect and expose the routes for the blueprint.""" -from . import dashboard, endpoint, moderation, received, session, sources, user +from . import dashboard, endpoint, moderation, received, session, sent, user from .blueprint import webmentions_bp __all__ = [ @@ -13,7 +13,7 @@ __all__ = [ "moderation", "received", "session", - "sources", + "sent", "user", "webmentions_bp", ] diff --git a/webmentions_ssg/views/sent.py b/webmentions_ssg/views/sent.py new file mode 100644 index 0000000..27dffb3 --- /dev/null +++ b/webmentions_ssg/views/sent.py @@ -0,0 +1,277 @@ +# SPDX-FileCopyrightText: 2026 Dennis Fink +# +# SPDX-License-Identifier: BSD-3-Clause + +"""Authenticated sent Webmention views, grouped by source document.""" + +import uuid +from datetime import UTC, date, datetime, time +from http import HTTPStatus +from typing import TypedDict, cast + +import sqlalchemy as sa +from flask import abort, current_app, flash, redirect, render_template, request, url_for +from flask.typing import ResponseReturnValue +from flask_login import login_required + +from ..extensions import db +from ..forms.admin import ConfirmActionForm +from ..forms.sent import ( + SENT_STATUS_CHOICES, + SentFilterForm, + default_sent_page_size, +) +from ..models.sources import SentWebmention, SentWebmentionStatus, SourceDocument +from ..tasks.scanner import manual_scan_sources +from ..tasks.sender import send_webmention +from .blueprint import webmentions_bp + + +class SentFilterArgs(TypedDict, total=False): + """Validated sent-page query parameters; keys match GET form fields.""" + + status: str + q: str + date_from: str + date_to: str + sort: str + order: str + per_page: str + + +def sent_filter_form() -> SentFilterForm: + """Validate GET filters without requiring CSRF on the search form.""" + form = SentFilterForm(formdata=request.args, meta={"csrf": False}) + if not form.validate(): + abort(HTTPStatus.BAD_REQUEST) + form.q.data = (form.q.data or "").strip() + return form + + +def sent_filter_args(form: SentFilterForm) -> SentFilterArgs: + """Serialize non-default values for pagination, sorting, and navigation.""" + values: dict[str, str] = {} + for name in SentFilterArgs.__annotations__: + field = form[name] + value = field.data + default = field.default() if callable(field.default) else field.default + if value is None or value == "" or value == default: + continue + values[name] = value.isoformat() if isinstance(value, date) else str(value) + return cast(SentFilterArgs, values) + + +@webmentions_bp.route("/sent") +@login_required +def sent_list() -> ResponseReturnValue: + """Display sent Webmentions grouped by source document.""" + filter_form = sent_filter_form() + filters = sent_filter_args(filter_form) + query = sa.select(SourceDocument) + + # EXISTS avoids duplicate source rows when several Webmentions match. + sent = sa.select(SentWebmention.uuid).where( + SentWebmention.source_uuid == SourceDocument.uuid + ) + if status := filters.get("status"): + if status == "failing": + query = query.where( + sent.where( + SentWebmention.status == SentWebmentionStatus.FAILED + ).exists() + ) + elif status == "retrying": + query = query.where( + sent.where( + SentWebmention.awaiting_processing, + SentWebmention.attempt_count > 0, + SentWebmention.next_retry_at.is_not(None), + ).exists() + ) + else: # deleted + query = query.where(SourceDocument.deleted_at.is_not(None)) + + if text := filters.get("q"): + query = query.where( + sa.or_( + SourceDocument.path.icontains(text, autoescape=True), + SourceDocument.url.icontains(text, autoescape=True), + sent.where( + SentWebmention.target.icontains(text, autoescape=True) + ).exists(), + ) + ) + + if start := filters.get("date_from"): + query = query.where( + SourceDocument.last_seen_at + >= datetime.combine(date.fromisoformat(start), time.min, UTC) + ) + if end := filters.get("date_to"): + query = query.where( + SourceDocument.last_seen_at + <= datetime.combine(date.fromisoformat(end), time.max, UTC) + ) + + count = ( + sa.select(sa.func.count(SentWebmention.uuid)) + .where(SentWebmention.source_uuid == SourceDocument.uuid) + .scalar_subquery() + ) + sort_columns = { + "path": SourceDocument.path, + "url": SourceDocument.url, + "webmentions": count, + "revision": SourceDocument.revision, + "created": SourceDocument.uuid, + "last_seen": SourceDocument.last_seen_at, + "status": sa.case((SourceDocument.deleted_at.is_(None), 0), else_=1), + } + sort_key = filters.get("sort", "last_seen") + sort_order = filters.get("order", "desc") + column = sort_columns[sort_key] + query = query.order_by(column.asc() if sort_order == "asc" else column.desc()) + if sort_key != "created": + query = query.order_by(SourceDocument.uuid.desc()) + + page_size = int(filters.get("per_page", default_sent_page_size())) + sources = db.paginate( + query, + per_page=page_size, + max_per_page=current_app.config["SOURCES_MAX_PAGE_SIZE"], + ) + + statistics = {} + if sources.items: + statistics = { + row.source_uuid: row + for row in db.session.execute( + SourceDocument.sent_webmention_statistics( + [source.uuid for source in sources.items] + ) + ) + } + + sort_urls = {} + for key in sort_columns: + if key == sort_key: + next_order = "asc" if sort_order == "desc" else "desc" + else: + next_order = "asc" if key in ("path", "url", "status") else "desc" + sort_filters: SentFilterArgs = {**filters, "sort": key, "order": next_order} + sort_urls[key] = url_for("webmentions.sent_list", **sort_filters) + + return render_template( + "sent.html", + title="Sent Webmentions", + sources=sources, + statistics=statistics, + filters=filters, + sort_key=sort_key, + sort_order=sort_order, + sort_urls=sort_urls, + status_choices=SENT_STATUS_CHOICES, + page_size=page_size, + max_source_page_size=current_app.config["SOURCES_MAX_PAGE_SIZE"], + filter_form=filter_form, + action_form=ConfirmActionForm(), + ) + + +@webmentions_bp.post("/sent/rescan") +@login_required +def rescan_sources() -> ResponseReturnValue: + """Queue a manual scan of sent Webmention sources. + + :returns: Redirect response to the sent Webmentions view. + """ + if not ConfirmActionForm().validate_on_submit(): + abort(HTTPStatus.BAD_REQUEST) + + filters = sent_filter_args(sent_filter_form()) + manual_scan_sources() + flash("Source rescan queued.", "success") + + page = request.args.get("page", 1, type=int) + return redirect( + url_for("webmentions.sent_list", page=page if page > 1 else None, **filters) + ) + + +@webmentions_bp.route("/sent/") +@login_required +def sent_source_detail(source_uuid: uuid.UUID) -> ResponseReturnValue: + """Display the sent Webmentions associated with a source. + + :param source_uuid: UUID of the source to display. + :returns: Rendered source details page. + """ + filters = sent_filter_args(sent_filter_form()) + source_page = request.args.get("source_page", 1, type=int) + source = db.get_or_404(SourceDocument, source_uuid) + + webmentions = db.paginate( + sa.select(SentWebmention) + .where(SentWebmention.source_uuid == source.uuid) + .order_by(SentWebmention.target, SentWebmention.uuid), + per_page=25, + ) + + return render_template( + "sent_source.html", + title="Sent Webmention Source", + source=source, + webmentions=webmentions, + filters=filters, + source_page=source_page, + action_form=ConfirmActionForm(), + ) + + +@webmentions_bp.post("/sent///resend") +@login_required +def resend_webmention( + source_uuid: uuid.UUID, webmention_uuid: uuid.UUID +) -> ResponseReturnValue: + """Queue an outgoing Webmention to be sent again. + + The current desired source revision is reopened for processing so the + sender task does not treat the Webmention as already completed. Historical + successful-send fields are preserved, while result fields from the previous + attempt are cleared. + + :param source_uuid: UUID of the source owning the Webmention. + :param webmention_uuid: UUID of the sent Webmention to resend. + :returns: Redirect response to the source details view. + """ + if not ConfirmActionForm().validate_on_submit(): + abort(HTTPStatus.BAD_REQUEST) + + filters = sent_filter_args(sent_filter_form()) + webmention = db.get_or_404(SentWebmention, webmention_uuid) + + if webmention.source_uuid != source_uuid: + abort(HTTPStatus.NOT_FOUND) + + # Requeue the existing desired revision instead of inventing a new source + # revision. send_webmention() otherwise exits when this revision has already + # been processed. + webmention.clear_result_for_resend() + + db.session.commit() + + send_webmention(webmention_uuid) + + flash(f"Webmention {webmention.uuid} queued for resend.", "success") + + page = request.args.get("page", 1, type=int) + source_page = request.args.get("source_page", 1, type=int) + return redirect( + url_for( + "webmentions.sent_source_detail", + source_uuid=source_uuid, + page=page if page > 1 else None, + source_page=source_page if source_page > 1 else None, + **filters, + ) + ) diff --git a/webmentions_ssg/views/sources.py b/webmentions_ssg/views/sources.py deleted file mode 100644 index 22cabb6..0000000 --- a/webmentions_ssg/views/sources.py +++ /dev/null @@ -1,277 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Authenticated source document and sent Webmention views.""" - -import uuid -from datetime import UTC, date, datetime, time -from http import HTTPStatus -from typing import TypedDict, cast - -import sqlalchemy as sa -from flask import abort, current_app, flash, redirect, render_template, request, url_for -from flask.typing import ResponseReturnValue -from flask_login import login_required - -from ..extensions import db -from ..forms.admin import ConfirmActionForm -from ..forms.sources import ( - SOURCE_STATUS_CHOICES, - SourceFilterForm, - default_source_page_size, -) -from ..models.sources import SentWebmention, SentWebmentionStatus, SourceDocument -from ..tasks.scanner import manual_scan_sources -from ..tasks.sender import send_webmention -from .blueprint import webmentions_bp - - -class SourceFilterArgs(TypedDict, total=False): - """Validated source-list query parameters; keys match GET form fields.""" - - status: str - q: str - date_from: str - date_to: str - sort: str - order: str - per_page: str - - -def source_filter_form() -> SourceFilterForm: - """Validate GET filters without requiring CSRF on the search form.""" - form = SourceFilterForm(formdata=request.args, meta={"csrf": False}) - if not form.validate(): - abort(HTTPStatus.BAD_REQUEST) - form.q.data = (form.q.data or "").strip() - return form - - -def source_filter_args(form: SourceFilterForm) -> SourceFilterArgs: - """Serialize non-default values for pagination, sorting, and navigation.""" - values: dict[str, str] = {} - for name in SourceFilterArgs.__annotations__: - field = form[name] - value = field.data - default = field.default() if callable(field.default) else field.default - if value is None or value == "" or value == default: - continue - values[name] = value.isoformat() if isinstance(value, date) else str(value) - return cast(SourceFilterArgs, values) - - -@webmentions_bp.route("/sources") -@login_required -def source_list() -> ResponseReturnValue: - """Display source documents with filters for outgoing delivery problems.""" - filter_form = source_filter_form() - filters = source_filter_args(filter_form) - query = sa.select(SourceDocument) - - # EXISTS avoids duplicate source rows when several Webmentions match. - sent = sa.select(SentWebmention.uuid).where( - SentWebmention.source_uuid == SourceDocument.uuid - ) - if status := filters.get("status"): - if status == "failing": - query = query.where( - sent.where( - SentWebmention.status == SentWebmentionStatus.FAILED - ).exists() - ) - elif status == "retrying": - query = query.where( - sent.where( - SentWebmention.awaiting_processing, - SentWebmention.attempt_count > 0, - SentWebmention.next_retry_at.is_not(None), - ).exists() - ) - else: # deleted - query = query.where(SourceDocument.deleted_at.is_not(None)) - - if text := filters.get("q"): - query = query.where( - sa.or_( - SourceDocument.path.icontains(text, autoescape=True), - SourceDocument.url.icontains(text, autoescape=True), - sent.where( - SentWebmention.target.icontains(text, autoescape=True) - ).exists(), - ) - ) - - if start := filters.get("date_from"): - query = query.where( - SourceDocument.last_seen_at - >= datetime.combine(date.fromisoformat(start), time.min, UTC) - ) - if end := filters.get("date_to"): - query = query.where( - SourceDocument.last_seen_at - <= datetime.combine(date.fromisoformat(end), time.max, UTC) - ) - - count = ( - sa.select(sa.func.count(SentWebmention.uuid)) - .where(SentWebmention.source_uuid == SourceDocument.uuid) - .scalar_subquery() - ) - sort_columns = { - "path": SourceDocument.path, - "url": SourceDocument.url, - "webmentions": count, - "revision": SourceDocument.revision, - "created": SourceDocument.uuid, - "last_seen": SourceDocument.last_seen_at, - "status": sa.case((SourceDocument.deleted_at.is_(None), 0), else_=1), - } - sort_key = filters.get("sort", "last_seen") - sort_order = filters.get("order", "desc") - column = sort_columns[sort_key] - query = query.order_by(column.asc() if sort_order == "asc" else column.desc()) - if sort_key != "created": - query = query.order_by(SourceDocument.uuid.desc()) - - page_size = int(filters.get("per_page", default_source_page_size())) - sources = db.paginate( - query, - per_page=page_size, - max_per_page=current_app.config["SOURCES_MAX_PAGE_SIZE"], - ) - - statistics = {} - if sources.items: - statistics = { - row.source_uuid: row - for row in db.session.execute( - SourceDocument.sent_webmention_statistics( - [source.uuid for source in sources.items] - ) - ) - } - - sort_urls = {} - for key in sort_columns: - if key == sort_key: - next_order = "asc" if sort_order == "desc" else "desc" - else: - next_order = "asc" if key in ("path", "url", "status") else "desc" - sort_filters: SourceFilterArgs = {**filters, "sort": key, "order": next_order} - sort_urls[key] = url_for("webmentions.source_list", **sort_filters) - - return render_template( - "sent.html", - title="Sent Webmentions", - sources=sources, - statistics=statistics, - filters=filters, - sort_key=sort_key, - sort_order=sort_order, - sort_urls=sort_urls, - status_choices=SOURCE_STATUS_CHOICES, - page_size=page_size, - max_source_page_size=current_app.config["SOURCES_MAX_PAGE_SIZE"], - filter_form=filter_form, - action_form=ConfirmActionForm(), - ) - - -@webmentions_bp.post("/sources/rescan") -@login_required -def rescan_sources() -> ResponseReturnValue: - """Queue a manual scan of sent Webmention sources. - - :returns: Redirect response to the sent Webmentions view. - """ - if not ConfirmActionForm().validate_on_submit(): - abort(HTTPStatus.BAD_REQUEST) - - filters = source_filter_args(source_filter_form()) - manual_scan_sources() - flash("Source rescan queued.", "success") - - page = request.args.get("page", 1, type=int) - return redirect( - url_for("webmentions.source_list", page=page if page > 1 else None, **filters) - ) - - -@webmentions_bp.route("/sources/") -@login_required -def source_detail(source_uuid: uuid.UUID) -> ResponseReturnValue: - """Display the sent Webmentions associated with a source. - - :param source_uuid: UUID of the source to display. - :returns: Rendered source details page. - """ - filters = source_filter_args(source_filter_form()) - source_page = request.args.get("source_page", 1, type=int) - source = db.get_or_404(SourceDocument, source_uuid) - - webmentions = db.paginate( - sa.select(SentWebmention) - .where(SentWebmention.source_uuid == source.uuid) - .order_by(SentWebmention.target, SentWebmention.uuid), - per_page=25, - ) - - return render_template( - "sent_source.html", - title="Sent Webmentions", - source=source, - webmentions=webmentions, - filters=filters, - source_page=source_page, - action_form=ConfirmActionForm(), - ) - - -@webmentions_bp.post("/sources///resend") -@login_required -def resend_webmention( - source_uuid: uuid.UUID, webmention_uuid: uuid.UUID -) -> ResponseReturnValue: - """Queue an outgoing Webmention to be sent again. - - The current desired source revision is reopened for processing so the - sender task does not treat the Webmention as already completed. Historical - successful-send fields are preserved, while result fields from the previous - attempt are cleared. - - :param source_uuid: UUID of the source owning the Webmention. - :param webmention_uuid: UUID of the sent Webmention to resend. - :returns: Redirect response to the source details view. - """ - if not ConfirmActionForm().validate_on_submit(): - abort(HTTPStatus.BAD_REQUEST) - - filters = source_filter_args(source_filter_form()) - webmention = db.get_or_404(SentWebmention, webmention_uuid) - - if webmention.source_uuid != source_uuid: - abort(HTTPStatus.NOT_FOUND) - - # Requeue the existing desired revision instead of inventing a new source - # revision. send_webmention() otherwise exits when this revision has already - # been processed. - webmention.clear_result_for_resend() - - db.session.commit() - - send_webmention(webmention_uuid) - - flash(f"Webmention {webmention.uuid} queued for resend.", "success") - - page = request.args.get("page", 1, type=int) - source_page = request.args.get("source_page", 1, type=int) - return redirect( - url_for( - "webmentions.source_detail", - source_uuid=source_uuid, - page=page if page > 1 else None, - source_page=source_page if source_page > 1 else None, - **filters, - ) - ) -- cgit v1.3.1