From 6ae1f30283aa113da82e9987398ad357260cc314 Mon Sep 17 00:00:00 2001 From: signebedi Date: Thu, 28 Mar 2024 15:50:35 -0500 Subject: [PATCH] Added: ui context callable (#30) --- libreforms_fastapi/app/__init__.py | 17 +++++++++++++---- libreforms_fastapi/utils/config.py | 4 ++-- 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/libreforms_fastapi/app/__init__.py b/libreforms_fastapi/app/__init__.py index fb3bbe7..f3d9ee1 100644 --- a/libreforms_fastapi/app/__init__.py +++ b/libreforms_fastapi/app/__init__.py @@ -1615,6 +1615,17 @@ async def api_auth_login(form_data: Annotated[OAuth2PasswordRequestForm, Depends ### UI Routes - Auth ########################## +# This is a standard callable that will generate the context +# for the UI routes and Jinja templates. +def build_ui_context(): + + kwargs = {} + + kwargs["config"] = config.model_dump() + kwargs["version"] = __version__ + + return kwargs + # Homepage @app.get("/ui/home", response_class=HTMLResponse) async def ui_homepage(request: Request): @@ -1625,8 +1636,7 @@ async def ui_homepage(request: Request): request=request, name="about.html.jinja", context={ - "config": config.model_dump(), - "version": __version__, + **build_ui_context(), } ) @@ -1640,8 +1650,7 @@ async def ui_privacy(request: Request): request=request, name="privacy.html.jinja", context={ - "config": config.model_dump(), - "version": __version__, + **build_ui_context(), } ) diff --git a/libreforms_fastapi/utils/config.py b/libreforms_fastapi/utils/config.py index d80aa82..0dce459 100644 --- a/libreforms_fastapi/utils/config.py +++ b/libreforms_fastapi/utils/config.py @@ -68,7 +68,7 @@ class Config(BaseSettings): SITE_SOURCE_URL:str = os.getenv('SITE_SOURCE_URL', 'https://github.com/signebedi/libreforms-fastapi') - HOMEPAGE_CONTENT:str = os.getenv('HOMEPAGE_CONTENT', '

Welcome to libreforms-fastapi, an open-source form management application based on the libreForms API and built using FastAPI.

') + HOMEPAGE_CONTENT:str | Markup = os.getenv('HOMEPAGE_CONTENT', '

Welcome to libreforms-fastapi, an open-source form management application based on the libreForms API and built using FastAPI.

') @field_validator('HOMEPAGE_CONTENT') @@ -81,7 +81,7 @@ def validate_homepage_content(cls, v): raise ValueError(f'Issue converting to markup: {v}') return m - PRIVACY_MESSAGE:str = os.getenv('PRIVACY_MESSAGE', '') + PRIVACY_MESSAGE:str | Markup = os.getenv('PRIVACY_MESSAGE', '') @field_validator('PRIVACY_MESSAGE') def validate_privacy_message(cls, v):