Sep 16 - Optimize code, part 1
This commit is contained in:
@@ -48,6 +48,14 @@ def create_app() -> Flask:
|
||||
cfg = get_config()
|
||||
app.config.from_object(cfg)
|
||||
|
||||
# Real client IP behind Nginx: ProxyFix takes it from the X-Forwarded-For
|
||||
# entry appended by our own proxy (TRUSTED_PROXY_COUNT hops), never from the
|
||||
# first, client-supplied entry. The login rate limiter depends on it.
|
||||
_trusted_proxies = app.config.get('TRUSTED_PROXY_COUNT', 1)
|
||||
if _trusted_proxies > 0:
|
||||
from werkzeug.middleware.proxy_fix import ProxyFix
|
||||
app.wsgi_app = ProxyFix(app.wsgi_app, x_for=_trusted_proxies)
|
||||
|
||||
# Guard against deployment with the insecure default SECRET_KEY
|
||||
import sys
|
||||
if not app.debug and app.config.get('SECRET_KEY') == 'change-me-in-production':
|
||||
@@ -324,6 +332,20 @@ def create_app() -> Flask:
|
||||
"""Handle internal server errors with user-friendly page"""
|
||||
return render_template('errors/500.html'), 500
|
||||
|
||||
@app.errorhandler(413)
|
||||
def request_too_large(error):
|
||||
"""Request over MAX_CONTENT_LENGTH, or a form field over MAX_FORM_MEMORY_SIZE"""
|
||||
from flask import jsonify
|
||||
message = 'The upload is too large. / El archivo es demasiado grande.'
|
||||
if (request.path.startswith(('/qr/', '/api/'))
|
||||
or request.headers.get('X-Requested-With') == 'XMLHttpRequest'):
|
||||
return jsonify({'success': False, 'message': message}), 413
|
||||
flash(message, 'error')
|
||||
referrer = request.referrer or ''
|
||||
if not referrer.startswith(request.host_url):
|
||||
referrer = url_for('dashboard.dashboard')
|
||||
return redirect(referrer)
|
||||
|
||||
# ------------------------------------------------------------------
|
||||
# Startup initialization (runs under gunicorn and flask run alike)
|
||||
# ------------------------------------------------------------------
|
||||
@@ -395,22 +417,24 @@ 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}/"
|
||||
# Public base URL for QR images generated here. There is no request at
|
||||
# startup, so it can only come from QR_BASE_URL (e.g.
|
||||
# https://qr.govservicesinc.com). Without it no image is generated: the
|
||||
# old FLASK_HOST/FLASK_PORT fallback printed http://localhost:5000/...
|
||||
# into QR codes that no phone can open. Routes still build images from
|
||||
# the request when a QR code is created or its styling is edited.
|
||||
base_url = (_Cfg.QR_BASE_URL or '').rstrip('/')
|
||||
base_url = f"{base_url}/" if base_url else ''
|
||||
if not base_url:
|
||||
lh.logger.info("Startup: QR_BASE_URL not set — missing QR images are not generated at startup")
|
||||
|
||||
updated_count = 0
|
||||
for qr_code in qr_codes:
|
||||
if not qr_code.qr_url or not qr_code.qr_code_image:
|
||||
if not qr_code.qr_url or (not qr_code.qr_code_image and base_url):
|
||||
try:
|
||||
if not qr_code.qr_url:
|
||||
qr_code.qr_url = generate_qr_url(qr_code.name, qr_code.id)
|
||||
if not qr_code.qr_code_image:
|
||||
if not qr_code.qr_code_image and base_url:
|
||||
qr_data = f"{base_url}qr/{qr_code.qr_url}"
|
||||
styling = get_qr_styling(qr_code)
|
||||
qr_code.qr_code_image = generate_qr_code(
|
||||
|
||||
Reference in New Issue
Block a user