Files

89 lines
3.1 KiB
Python
Raw Permalink Normal View History

2025-08-02 20:08:33 +02:00
# -*- coding: utf-8 -*-
"""
Django Translatable Fields - App Configuration
This module contains the Django app configuration for the translatable fields plugin.
It includes startup checks to ensure PostgreSQL is being used as the database backend.
Author: Holger Sielaff <holger@backender.de>
Version: 0.1.0
"""
from django.apps import AppConfig
from django.core.exceptions import ImproperlyConfigured
from django.db import connection
import logging
logger = logging.getLogger(__name__)
class TranslatableFieldsConfig(AppConfig):
"""
Django app configuration for translatable fields.
This app configuration performs startup checks to ensure the database
backend is compatible with the translatable fields functionality.
"""
default_auto_field = 'django.db.models.BigAutoField'
name = 'django_translatable_fields'
verbose_name = 'Django Translatable Fields'
def ready(self):
"""
Perform app initialization and compatibility checks.
This method is called when Django starts up and the app is ready.
It checks for PostgreSQL database backend and warns about suboptimal
performance with other database backends.
Raises:
ImproperlyConfigured: If database backend is not supported
"""
super().ready()
# Check database backend
self._check_database_backend()
# Import signal handlers (if any)
try:
from . import signals # noqa
except ImportError:
pass
def _check_database_backend(self):
"""
Check if the database backend is compatible with translatable fields.
PostgreSQL is strongly recommended for optimal performance with JSON
field operations. Other backends will work but with reduced performance.
Raises:
ImproperlyConfigured: If database backend is not supported
"""
try:
vendor = connection.vendor
if vendor == 'postgresql':
logger.info("Django Translatable Fields: Using PostgreSQL - optimal performance enabled")
return
elif vendor in ['mysql', 'sqlite']:
logger.warning(
f"Django Translatable Fields: Using {vendor.title()} database. "
f"Performance may be suboptimal. PostgreSQL is strongly recommended "
f"for production use with translatable fields."
)
return
else:
raise ImproperlyConfigured(
f"Django Translatable Fields: Unsupported database backend '{vendor}'. "
f"Supported backends: PostgreSQL (recommended), MySQL, SQLite. "
f"PostgreSQL is strongly recommended for production use."
)
except Exception as e:
logger.warning(
f"Django Translatable Fields: Could not check database backend: {e}. "
f"Ensure you're using PostgreSQL for optimal performance."
)