From 4bd1d9e348918b2e55db47a1ac3489d035cf55e2 Mon Sep 17 00:00:00 2001 From: Dennis Fink Date: Thu, 12 Nov 2026 19:57:08 +0100 Subject: refactor(core): split modules by responsibility Break up monolithic views, task helpers, networking, CLI, and application extension setup so each module owns a focused concern. Reorganize the test suite around the new boundaries while preserving existing Webmention behavior and coverage. --- webmentions_ssg/cli/__init__.py | 5 -- webmentions_ssg/cli/database.py | 49 ----------------- webmentions_ssg/cli/mail.py | 53 ------------------ webmentions_ssg/cli/users.py | 117 ---------------------------------------- 4 files changed, 224 deletions(-) delete mode 100644 webmentions_ssg/cli/__init__.py delete mode 100644 webmentions_ssg/cli/database.py delete mode 100644 webmentions_ssg/cli/mail.py delete mode 100644 webmentions_ssg/cli/users.py (limited to 'webmentions_ssg/cli') diff --git a/webmentions_ssg/cli/__init__.py b/webmentions_ssg/cli/__init__.py deleted file mode 100644 index accaaa3..0000000 --- a/webmentions_ssg/cli/__init__.py +++ /dev/null @@ -1,5 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Group the application command-line management modules.""" diff --git a/webmentions_ssg/cli/database.py b/webmentions_ssg/cli/database.py deleted file mode 100644 index e918600..0000000 --- a/webmentions_ssg/cli/database.py +++ /dev/null @@ -1,49 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Simple database commands built on Flask-Migrate.""" - -import click -from flask.cli import AppGroup -from flask_migrate import upgrade as migrate_upgrade -from sqlalchemy import inspect - -from .. import DATABASE - -DB_CLI = AppGroup("db", help="Handle database") - - -def _table_names() -> set[str]: - """Return existing table names without modifying the database schema.""" - with DATABASE.engine.connect() as connection: - return set(inspect(connection).get_table_names()) - - -@DB_CLI.command("create-db", help="Create a new database using all migrations") -def create_db() -> None: - """Initialize an empty database and record its Alembic revision.""" - if _table_names(): - raise click.ClickException( - "The database already contains tables. " - "Use 'flask db upgrade' for an existing migrated database." - ) - - # Applying migrations (rather than create_all()) keeps alembic_version in sync. - migrate_upgrade() - click.echo("Database created at the latest migration revision.") - - -@DB_CLI.command("upgrade", help="Upgrade an existing database to the latest revision") -def upgrade_db() -> None: - """Apply pending migrations to a previously initialized database.""" - if "alembic_version" not in _table_names(): - raise click.ClickException( - "No Flask-Migrate revision table found. " - "For a new, empty database run 'flask db create-db'. " - "For a legacy database, inspect its schema and use " - "'flask migrate stamp' only if appropriate." - ) - - migrate_upgrade() - click.echo("Database upgraded to the latest migration revision.") diff --git a/webmentions_ssg/cli/mail.py b/webmentions_ssg/cli/mail.py deleted file mode 100644 index 47687e2..0000000 --- a/webmentions_ssg/cli/mail.py +++ /dev/null @@ -1,53 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Provide a command for testing outgoing email configuration.""" - -import click -from flask import current_app -from flask.cli import AppGroup -from flask_mail import Message - -from .. import MAIL - -MAIL_CLI = AppGroup("mail", help="Handle mail") - - -@MAIL_CLI.command("test", help="Send an email to test the SMTP configuration") -@click.option( - "--to", "recipient", help="Recipient address (defaults to NOTIFICATION_EMAIL)." -) -def send_test_mail(recipient: str | None) -> None: - """ - Send a test email using the configured SMTP server. - - :param recipient: Optional recipient overriding NOTIFICATION_EMAIL. - :raises click.ClickException: If configuration is missing or sending fails. - """ - recipient = recipient or current_app.config.get("NOTIFICATION_EMAIL") - if not recipient: - raise click.ClickException( - "No recipient specified. Set NOTIFICATION_EMAIL or pass --to." - ) - - sender = current_app.config.get("MAIL_DEFAULT_SENDER") - if not sender: - raise click.ClickException("MAIL_DEFAULT_SENDER is not configured.") - - message = Message( - subject="webmentions-ssg: test email", - sender=sender, - recipients=[recipient], - body=( - "This is a test email from webmentions-ssg.\n\n" - "If you received this message, the outgoing mail configuration works.\n" - ), - ) - - try: - MAIL.send(message) - except Exception as exc: - raise click.ClickException(f"Could not send test email: {exc}") from exc - - click.echo(f"Test email sent to {recipient}.") diff --git a/webmentions_ssg/cli/users.py b/webmentions_ssg/cli/users.py deleted file mode 100644 index a48fc71..0000000 --- a/webmentions_ssg/cli/users.py +++ /dev/null @@ -1,117 +0,0 @@ -# SPDX-FileCopyrightText: 2026 Dennis Fink -# -# SPDX-License-Identifier: BSD-3-Clause - -"""Provide commands for managing application user accounts.""" - -import click -import sqlalchemy as sa -from flask.cli import AppGroup -from sqlalchemy.exc import IntegrityError - -from .. import DATABASE -from ..security import validate_password - -USERS_CLI = AppGroup("users", help="Manage users.") - - -def _require_user(username: str): - """Return a user by username or report a CLI-friendly error.""" - from ..models import User - - user = DATABASE.session.scalar(sa.select(User).where(User.username == username)) - if user is None: - raise click.ClickException(f"User {username!r} does not exist.") - return user - - -@USERS_CLI.command("create", help="Create a user.") -@click.option("--username", prompt="Username", help="Username for the new user.") -@click.password_option(confirmation_prompt=True) -def create_user(username: str, password: str) -> None: - """ - Create a user with a securely hashed password. - - :param username: Username for the new user. - :param password: Plain-text password to hash and store. - :raises click.ClickException: If the credentials are invalid or the user exists. - """ - from ..models import User - - username = username.strip() - if not username: - raise click.ClickException("Username must not be empty.") - - try: - validate_password(password) - except ValueError as exc: - raise click.ClickException(str(exc)) from exc - - if DATABASE.session.scalar(sa.select(User).where(User.username == username)): - raise click.ClickException(f"User {username!r} already exists.") - - DATABASE.session.add(User(username=username, password=password)) - try: - DATABASE.session.commit() - except IntegrityError as exc: - DATABASE.session.rollback() - raise click.ClickException(f"User {username!r} already exists.") from exc - - click.echo(f"Created user {username!r}.") - - -@USERS_CLI.command("list", help="List registered users.") -def list_users() -> None: - """List user IDs and usernames in alphabetical order.""" - from ..models import User - - users = DATABASE.session.scalars(sa.select(User).order_by(User.username)).all() - if not users: - click.echo("No users found.") - return - - click.echo("ID\tUSERNAME") - for user in users: - click.echo(f"{user.id}\t{user.username}") - - -@USERS_CLI.command("set-password", help="Reset a user's password.") -@click.argument("username") -@click.password_option(confirmation_prompt=True) -def set_user_password(username: str, password: str) -> None: - """ - Change a user's password without displaying it. - - :param username: Username of the account to update. - :param password: New plain-text password to hash and store. - :raises click.ClickException: If the account is missing or the password is invalid. - """ - try: - validate_password(password) - except ValueError as exc: - raise click.ClickException(str(exc)) from exc - - user = _require_user(username) - user.password = password - DATABASE.session.commit() - click.echo(f"Updated password for user {username!r}.") - - -@USERS_CLI.command("delete", help="Delete a user.") -@click.argument("username") -@click.option("-y", "--yes", is_flag=True, help="Skip the deletion confirmation.") -def delete_user(username: str, yes: bool) -> None: - """ - Permanently delete a user after confirmation, unless --yes is passed. - - :param username: Username of the account to remove. - :param yes: Whether to skip the confirmation prompt. - :raises click.ClickException: If the account does not exist. - """ - user = _require_user(username) - if not yes: - click.confirm(f"Delete user {username!r}?", abort=True) - - DATABASE.session.delete(user) - DATABASE.session.commit() - click.echo(f"Deleted user {username!r}.") -- cgit v1.3.1