Remove aws-admin pattern completely - use account + GlobalIntegrationSettings

ARCHITECTURE FIX:
- aws-admin IntegrationSettings will NEVER exist (it's a legacy pattern)
- Only user's own account IntegrationSettings can exist (if they override defaults)
- Otherwise GlobalIntegrationSettings is used directly
- API keys are ALWAYS from GlobalIntegrationSettings (accounts cannot override API keys)

REMOVED:
- All aws-admin Account lookups
- All aws-admin IntegrationSettings fallback attempts
- Confusing nested try/except chains

CORRECT FLOW NOW:
1. Try account's IntegrationSettings for config overrides
2. Use GlobalIntegrationSettings for missing values and ALL API keys
3. No intermediate aws-admin lookups
This commit is contained in:
IGNY8 VPS (Salman)
2025-12-25 02:11:21 +00:00
parent 504d0174f7
commit 826ad89a3e
3 changed files with 32 additions and 74 deletions

View File

@@ -220,17 +220,15 @@ class GenerateImagePromptsFunction(BaseAIFunction):
def _get_max_in_article_images(self, account) -> int:
"""
Get max_in_article_images from settings.
Tries aws-admin IntegrationSettings first, falls back to GlobalIntegrationSettings.
Uses account's IntegrationSettings override, or GlobalIntegrationSettings.
"""
from igny8_core.modules.system.models import IntegrationSettings
from igny8_core.modules.system.global_settings_models import GlobalIntegrationSettings
from igny8_core.auth.models import Account
# Try aws-admin IntegrationSettings first (legacy pattern)
# Try account-specific override first
try:
system_account = Account.objects.get(slug='aws-admin')
settings = IntegrationSettings.objects.get(
account=system_account,
account=account,
integration_type='image_generation',
is_active=True
)
@@ -238,15 +236,15 @@ class GenerateImagePromptsFunction(BaseAIFunction):
if max_images is not None:
max_images = int(max_images)
logger.info(f"Using max_in_article_images={max_images} from aws-admin IntegrationSettings")
logger.info(f"Using max_in_article_images={max_images} from account {account.id} IntegrationSettings override")
return max_images
except (Account.DoesNotExist, IntegrationSettings.DoesNotExist):
logger.debug("aws-admin IntegrationSettings not found, falling back to GlobalIntegrationSettings")
except IntegrationSettings.DoesNotExist:
logger.debug(f"No IntegrationSettings override for account {account.id}, using GlobalIntegrationSettings")
# Fall back to GlobalIntegrationSettings
# Use GlobalIntegrationSettings default
global_settings = GlobalIntegrationSettings.get_instance()
max_images = global_settings.max_in_article_images
logger.info(f"Using max_in_article_images={max_images} from GlobalIntegrationSettings")
logger.info(f"Using max_in_article_images={max_images} from GlobalIntegrationSettings (account {account.id})")
return max_images
def _extract_content_elements(self, content: Content, max_images: int) -> Dict:

View File

@@ -68,25 +68,23 @@ class GenerateImagesFunction(BaseAIFunction):
raise ValueError("No tasks found")
# Get image generation settings
# Try aws-admin IntegrationSettings first (legacy), fall back to GlobalIntegrationSettings
# Try account-specific override, otherwise use GlobalIntegrationSettings
from igny8_core.modules.system.models import IntegrationSettings
from igny8_core.modules.system.global_settings_models import GlobalIntegrationSettings
from igny8_core.auth.models import Account
image_settings = {}
try:
system_account = Account.objects.get(slug='aws-admin')
integration = IntegrationSettings.objects.get(
account=system_account,
account=account,
integration_type='image_generation',
is_active=True
)
image_settings = integration.config or {}
logger.info("Using image settings from aws-admin IntegrationSettings")
except (Account.DoesNotExist, IntegrationSettings.DoesNotExist):
logger.info("aws-admin IntegrationSettings not found, using GlobalIntegrationSettings")
logger.info(f"Using image settings from account {account.id} IntegrationSettings override")
except IntegrationSettings.DoesNotExist:
logger.info(f"No IntegrationSettings override for account {account.id}, using GlobalIntegrationSettings")
# Fall back to GlobalIntegrationSettings for missing values
# Use GlobalIntegrationSettings for missing values
global_settings = GlobalIntegrationSettings.get_instance()
# Extract settings with defaults from global settings

View File

@@ -182,28 +182,26 @@ def process_image_generation_queue(self, image_ids: list, account_id: int = None
results = []
# Get image generation settings
# Try aws-admin IntegrationSettings first (legacy), fall back to GlobalIntegrationSettings
# Try account-specific override, otherwise use GlobalIntegrationSettings
logger.info("[process_image_generation_queue] Step 1: Loading image generation settings")
from igny8_core.auth.models import Account
from igny8_core.modules.system.global_settings_models import GlobalIntegrationSettings
config = {}
try:
system_account = Account.objects.get(slug='aws-admin')
image_settings = IntegrationSettings.objects.get(
account=system_account,
account=account,
integration_type='image_generation',
is_active=True
)
logger.info(f"[process_image_generation_queue] Using system account (aws-admin) IntegrationSettings")
logger.info(f"[process_image_generation_queue] Using account {account.id} IntegrationSettings override")
config = image_settings.config or {}
except (Account.DoesNotExist, IntegrationSettings.DoesNotExist):
logger.info("[process_image_generation_queue] aws-admin IntegrationSettings not found, using GlobalIntegrationSettings")
except IntegrationSettings.DoesNotExist:
logger.info(f"[process_image_generation_queue] No IntegrationSettings override for account {account.id}, using GlobalIntegrationSettings")
except Exception as e:
logger.error(f"[process_image_generation_queue] ERROR loading image generation settings: {e}", exc_info=True)
return {'success': False, 'error': f'Error loading image generation settings: {str(e)}'}
# Fall back to GlobalIntegrationSettings for missing values
# Use GlobalIntegrationSettings for missing values
global_settings = GlobalIntegrationSettings.get_instance()
logger.info(f"[process_image_generation_queue] Image generation settings loaded. Config keys: {list(config.keys())}")
@@ -234,46 +232,12 @@ def process_image_generation_queue(self, image_ids: list, account_id: int = None
logger.info(f" - Desktop enabled: {desktop_enabled}")
logger.info(f" - Mobile enabled: {mobile_enabled}")
# Get provider API key (using same approach as test image generation)
# Note: API key is stored as 'apiKey' (camelCase) in IntegrationSettings.config
# Normal users use system account settings (aws-admin) via fallback, or GlobalIntegrationSettings
logger.info(f"[process_image_generation_queue] Step 2: Loading {provider.upper()} API key")
try:
provider_settings = IntegrationSettings.objects.get(
account=account,
integration_type=provider, # Use the provider from settings
is_active=True
)
logger.info(f"[process_image_generation_queue] {provider.upper()} integration settings found for account {account.id}")
except IntegrationSettings.DoesNotExist:
# Fallback to system account (aws-admin) settings
logger.info(f"[process_image_generation_queue] No {provider.upper()} settings for account {account.id}, falling back to system account")
try:
system_account = Account.objects.get(slug='aws-admin')
provider_settings = IntegrationSettings.objects.get(
account=system_account,
integration_type=provider,
is_active=True
)
logger.info(f"[process_image_generation_queue] Using system account (aws-admin) {provider.upper()} settings")
except (Account.DoesNotExist, IntegrationSettings.DoesNotExist):
# Final fallback: use GlobalIntegrationSettings API key
logger.info(f"[process_image_generation_queue] No {provider.upper()} IntegrationSettings found, will use GlobalIntegrationSettings API key")
provider_settings = None # Signal to use global settings below
except Exception as e:
logger.error(f"[process_image_generation_queue] ERROR getting {provider.upper()} API key from aws-admin: {e}", exc_info=True)
return {'success': False, 'error': f'Error retrieving {provider.upper()} API key: {str(e)}'}
except Exception as e:
logger.error(f"[process_image_generation_queue] ERROR getting {provider.upper()} API key: {e}", exc_info=True)
return {'success': False, 'error': f'Error retrieving {provider.upper()} API key: {str(e)}'}
# Get provider API key
# API keys are ALWAYS from GlobalIntegrationSettings (accounts cannot override API keys)
# Account IntegrationSettings only store provider preference, NOT API keys
logger.info(f"[process_image_generation_queue] Step 2: Loading {provider.upper()} API key from GlobalIntegrationSettings")
# Extract API key from provider settings or global settings
if provider_settings:
logger.info(f"[process_image_generation_queue] {provider.upper()} config keys: {list(provider_settings.config.keys()) if provider_settings.config else 'None'}")
api_key = provider_settings.config.get('apiKey') if provider_settings.config else None
else:
# Use GlobalIntegrationSettings API key
logger.info(f"[process_image_generation_queue] Using {provider.upper()} API key from GlobalIntegrationSettings")
# Get API key from GlobalIntegrationSettings
if provider == 'runware':
api_key = global_settings.runware_api_key
elif provider == 'openai':
@@ -282,10 +246,8 @@ def process_image_generation_queue(self, image_ids: list, account_id: int = None
api_key = None
if not api_key:
logger.error(f"[process_image_generation_queue] {provider.upper()} API key not found in config or GlobalIntegrationSettings")
if provider_settings:
logger.error(f"[process_image_generation_queue] {provider.upper()} config: {provider_settings.config}")
return {'success': False, 'error': f'{provider.upper()} API key not configured'}
logger.error(f"[process_image_generation_queue] {provider.upper()} API key not configured in GlobalIntegrationSettings")
return {'success': False, 'error': f'{provider.upper()} API key not configured in GlobalIntegrationSettings'}
# Log API key presence (but not the actual key for security)
api_key_preview = f"{api_key[:10]}...{api_key[-4:]}" if len(api_key) > 14 else "***"