38086-vm/config/wsgi.py
2026-02-11 14:40:48 +00:00

75 lines
2.0 KiB
Python

"""
WSGI config for config project.
It exposes the WSGI callable as a module-level variable named ``application``.
For more information on this file, see
https://docs.djangoproject.com/en/5.0/howto/deployment/wsgi/
"""
import os
from pathlib import Path
import ctypes
import ctypes.util
try:
from dotenv import load_dotenv
env_path = Path(__file__).resolve().parent.parent.parent / '.env'
if env_path.exists():
load_dotenv(env_path)
except ImportError:
pass
# --- FIX: Preload libraries for WeasyPrint/Pango ---
# Manually load libraries using absolute paths
import ctypes
import ctypes.util
import traceback
import sys
lib_paths = [
'/usr/lib/x86_64-linux-gnu/libglib-2.0.so.0',
'/usr/lib/x86_64-linux-gnu/libgobject-2.0.so.0',
'/usr/lib/x86_64-linux-gnu/libfontconfig.so.1',
'/usr/lib/x86_64-linux-gnu/libcairo.so.2',
'/usr/lib/x86_64-linux-gnu/libpango-1.0.so.0',
'/usr/lib/x86_64-linux-gnu/libpangoft2-1.0.so.0',
]
for path in lib_paths:
try:
ctypes.CDLL(path)
except OSError:
pass
# Try to import weasyprint
try:
import weasyprint
except Exception as e:
# Log error to file and stderr
error_msg = f"WeasyPrint Import Error: {str(e)}\n{traceback.format_exc()}"
sys.stderr.write(error_msg)
try:
with open("weasyprint_wsgi_error.log", "w") as f:
f.write(error_msg)
except Exception:
pass
# Fallback to mock
import types
class MockHTML:
def __init__(self, string=None, base_url=None):
pass
def write_pdf(self, target=None):
raise OSError("WeasyPrint system dependencies are missing. PDF generation is disabled.")
mock_wp = types.ModuleType("weasyprint")
mock_wp.HTML = MockHTML
sys.modules["weasyprint"] = mock_wp
# ---------------------------------------------------
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'config.settings')
from django.core.wsgi import get_wsgi_application
application = get_wsgi_application()