04/16 Updated with domain settings in .env file

This commit is contained in:
2026-04-16 13:11:28 -04:00
parent 7bee3bfedc
commit 03c9ca088c
3 changed files with 45 additions and 15 deletions
+17 -10
View File
@@ -294,8 +294,10 @@ def update_existing_qr_codes():
"""Update existing QR codes with missing URLs or images at startup.
Regenerates qr_url slugs without needing a request context.
For qr_code_image, constructs the base URL from FLASK_HOST/FLASK_PORT
config so this can run safely outside any HTTP request.
For qr_code_image, uses QR_BASE_URL (from .env) as the authoritative base
URL so that generated links always match the public-facing domain.
Falls back to FLASK_HOST/FLASK_PORT construction only when QR_BASE_URL is
not configured (development environments without a reverse proxy).
"""
from extensions import db as _db, logger_handler as lh
from utils.helpers import generate_qr_code, get_qr_styling, generate_qr_url
@@ -306,14 +308,19 @@ def update_existing_qr_codes():
if not qr_codes:
return
# Build a base URL that does not require an active request context.
host = os.environ.get('FLASK_HOST', '0.0.0.0')
# 0.0.0.0 is a bind address, not a reachable hostname — default to localhost
if host in ('0.0.0.0', ''):
host = 'localhost'
port = os.environ.get('FLASK_PORT', '5000')
scheme = 'https' if _Cfg.SESSION_COOKIE_SECURE else 'http'
base_url = f"{scheme}://{host}:{port}/"
# Prefer the explicit QR_BASE_URL env var (required behind a reverse proxy).
# Fall back to FLASK_HOST/PORT for local/dev environments.
qr_base_url = os.environ.get('QR_BASE_URL', '').rstrip('/')
if qr_base_url:
base_url = qr_base_url + '/'
else:
host = os.environ.get('FLASK_HOST', '0.0.0.0')
# 0.0.0.0 is a bind address, not a reachable hostname — default to localhost
if host in ('0.0.0.0', ''):
host = 'localhost'
port = os.environ.get('FLASK_PORT', '5000')
scheme = 'https' if _Cfg.SESSION_COOKIE_SECURE else 'http'
base_url = f"{scheme}://{host}:{port}/"
updated_count = 0
for qr_code in qr_codes: