diff options
| author | Dennis Fink | 2026-11-13 21:20:40 +0100 |
|---|---|---|
| committer | Dennis Fink | 2026-11-13 21:20:40 +0100 |
| commit | a285fdf29e59c3b957247ea9241943c7202e9a28 (patch) | |
| tree | 17ceeb703de23af1e67081d99a8864bbe353fb46 /webmentions_ssg/views | |
| parent | 78ecd8bc227bdcbbd0e45eea025f7223252ed0da (diff) | |
| download | webmentions-ssg-a285fdf29e59c3b957247ea9241943c7202e9a28.tar.gz webmentions-ssg-a285fdf29e59c3b957247ea9241943c7202e9a28.zip | |
refactor(core): consolidate repeated helper logic
Centralize SQL row counts, hostname parsing, and content-type
normalization so tasks, forms, and scanners share consistent
implementations.
Simplify periodic task registration and admin lookups with table-driven
setup and Flask-SQLAlchemy's built-in 404 helper.
Diffstat (limited to '')
| -rw-r--r-- | webmentions_ssg/views/admin.py | 21 | ||||
| -rw-r--r-- | webmentions_ssg/views/endpoint.py | 32 |
2 files changed, 18 insertions, 35 deletions
diff --git a/webmentions_ssg/views/admin.py b/webmentions_ssg/views/admin.py index 1ca06b5..794bcdf 100644 --- a/webmentions_ssg/views/admin.py +++ b/webmentions_ssg/views/admin.py @@ -54,10 +54,7 @@ def delete_received_webmention(identifier: uuid.UUID) -> ResponseReturnValue: if not form.validate_on_submit(): abort(400) - webmention = db.session.get(ReceivedWebmention, identifier) - - if webmention is None: - abort(404) + webmention = db.get_or_404(ReceivedWebmention, identifier) db.session.delete(webmention) db.session.commit() @@ -84,10 +81,7 @@ def reverify_received_webmention(identifier: uuid.UUID) -> ResponseReturnValue: if not form.validate_on_submit(): abort(400) - webmention = db.session.get(ReceivedWebmention, identifier) - - if webmention is None: - abort(404) + webmention = db.get_or_404(ReceivedWebmention, identifier) webmention.reset_for_verification() @@ -145,12 +139,7 @@ def sent_source(source_identifier: uuid.UUID) -> ResponseReturnValue: :param source_identifier: Identifier of the source to display. :return: Rendered source details page. """ - source = db.session.scalar( - sa.select(Source).where(Source.uuid == source_identifier) - ) - - if source is None: - abort(404) + source = db.get_or_404(Source, source_identifier) webmentions = db.paginate( sa.select(SentWebmention) @@ -189,9 +178,9 @@ def resend_sent_webmention( if not form.validate_on_submit(): abort(400) - webmention = db.session.get(SentWebmention, webmention_identifier) + webmention = db.get_or_404(SentWebmention, webmention_identifier) - if webmention is None or webmention.source_id != source_identifier: + if webmention.source_id != source_identifier: abort(404) # Requeue the existing desired revision instead of inventing a new source diff --git a/webmentions_ssg/views/endpoint.py b/webmentions_ssg/views/endpoint.py index e3f1368..a7badfd 100644 --- a/webmentions_ssg/views/endpoint.py +++ b/webmentions_ssg/views/endpoint.py @@ -6,15 +6,15 @@ import uuid from datetime import UTC, datetime, timedelta -from typing import Any, cast +from typing import cast import sqlalchemy as sa import sqlalchemy.exc as sa_exc -from flask import Response, abort, current_app, render_template, url_for +from flask import Response, current_app, render_template, url_for from flask.typing import ResponseReturnValue -from sqlalchemy.engine import CursorResult from .. import forms +from ..db_helpers import rows_affected from ..extensions import csrf, db, limiter from ..models import ReceivedWebmention, received_url_digest from ..tasks.receiver import verify_webmention @@ -63,20 +63,17 @@ def reopen_if_cooled_down(webmention: ReceivedWebmention) -> bool: seconds=current_app.config["ENDPOINT_REVERIFY_COOLDOWN_SECONDS"] ) - result = cast( - CursorResult[Any], - db.session.execute( - sa.update(ReceivedWebmention) - .where( - ReceivedWebmention.uuid == webmention.uuid, - ReceivedWebmention.updated_at <= cutoff, - ) - .values(ReceivedWebmention.verification_reset_values(at=now)) - .execution_options(synchronize_session=False) - ), + result = db.session.execute( + sa.update(ReceivedWebmention) + .where( + ReceivedWebmention.uuid == webmention.uuid, + ReceivedWebmention.updated_at <= cutoff, + ) + .values(ReceivedWebmention.verification_reset_values(at=now)) + .execution_options(synchronize_session=False) ) db.session.commit() - return result.rowcount == 1 + return rows_affected(result) == 1 def submit_received(source: str, target: str) -> tuple[uuid.UUID, bool] | None: @@ -144,10 +141,7 @@ def status(identifier: uuid.UUID) -> ResponseReturnValue: :param identifier: Identifier of the Webmention to display. :return: Rendered Webmention status page. """ - webmention = db.session.get(ReceivedWebmention, identifier) - - if webmention is None: - abort(404) + webmention = db.get_or_404(ReceivedWebmention, identifier) return render_template( "status.html", title="Webmention status", webmention=webmention |
