Source code for labfrog.form_feedback

# SPDX-FileCopyrightText: 2026 Helmholtz-Zentrum Dresden-Rossendorf e.V (HZDR)
# SPDX-License-Identifier: Apache-2.0

"""Shared UI feedback helpers for form-based routes."""

from __future__ import annotations

import logging

from flask import flash, session
from flask_login import current_user

DEMO_LIMIT_MESSAGE = (
    "Demo limit reached. Install a full LabFrog instance to save more entries."
)


[docs] def flash_demo_limit_reached() -> None: """Show the shared demo-limit warning and discard stale form flashes.""" session.pop("_flashes", None) flash(DEMO_LIMIT_MESSAGE, "warning")
[docs] def current_username(*, default: str = "anonymous") -> str: """Return the active username for persistence/audit fields.""" if current_user and current_user.is_authenticated: return str(current_user.username) return default
[docs] def flash_form_errors(form, *, logger: logging.Logger | None = None) -> None: """Log and flash WTForms validation errors consistently.""" route_logger = logger or logging.getLogger(__name__) for field, errors in form.errors.items(): for error in errors: route_logger.error("Error in field '%s': %s", field, error) flash(f"Error in field '{field}': {error}", "error")