from .models import SystemSetting from django.db.utils import OperationalError from django.core.management import call_command import os import time import logging logger = logging.getLogger(__name__) STARTUP_TIMESTAMP = int(time.time()) def project_context(request): return { "project_description": os.getenv("PROJECT_DESCRIPTION", ""), "project_image_url": os.getenv("PROJECT_IMAGE_URL", ""), "deployment_timestamp": STARTUP_TIMESTAMP, } def global_settings(request): settings = None try: # Use a quick query to avoid hangs if DB is locked settings = SystemSetting.objects.first() if not settings: # Only attempt creation if we are absolutely sure it's missing # and wrap it in a try-except to avoid crashes on read-only DBs try: settings = SystemSetting.objects.create(business_name="Meezan Accounting") except Exception as e: logger.error(f"Failed to create SystemSetting: {e}") settings = None except Exception as e: logger.warning(f"Database error in global_settings: {e}") pass return { 'site_settings': settings, 'global_settings': settings, 'decimal_places': settings.decimal_places if settings else 3 }