notifciations issues fixed final

This commit is contained in:
IGNY8 VPS (Salman)
2025-12-28 00:52:14 +00:00
parent 28a60f8141
commit 0605f650b1
12 changed files with 1384 additions and 18 deletions

View File

@@ -481,6 +481,9 @@ class AIEngine:
# Log to database
self._log_to_database(fn, payload, parsed, save_result)
# Create notification for successful completion
self._create_success_notification(function_name, save_result, payload)
return {
'success': True,
**save_result,
@@ -524,6 +527,9 @@ class AIEngine:
self._log_to_database(fn, None, None, None, error=error)
# Create notification for failure
self._create_failure_notification(function_name, error)
return {
'success': False,
'error': error,
@@ -651,4 +657,104 @@ class AIEngine:
'generate_site_structure': 'site_blueprint',
}
return mapping.get(function_name, 'unknown')
def _create_success_notification(self, function_name: str, save_result: dict, payload: dict):
"""Create notification for successful AI task completion"""
if not self.account:
return
# Lazy import to avoid circular dependency and Django app loading issues
from igny8_core.business.notifications.services import NotificationService
# Get site from payload if available
site = None
site_id = payload.get('site_id')
if site_id:
try:
from igny8_core.auth.models import Site
site = Site.objects.get(id=site_id, account=self.account)
except:
pass
try:
# Map function to appropriate notification method
if function_name == 'auto_cluster':
NotificationService.notify_clustering_complete(
account=self.account,
site=site,
cluster_count=save_result.get('clusters_created', 0),
keyword_count=save_result.get('keywords_updated', 0)
)
elif function_name == 'generate_ideas':
NotificationService.notify_ideas_complete(
account=self.account,
site=site,
idea_count=save_result.get('count', 0),
cluster_count=len(payload.get('ids', []))
)
elif function_name == 'generate_content':
NotificationService.notify_content_complete(
account=self.account,
site=site,
article_count=save_result.get('count', 0),
word_count=save_result.get('word_count', 0)
)
elif function_name == 'generate_image_prompts':
NotificationService.notify_prompts_complete(
account=self.account,
site=site,
prompt_count=save_result.get('count', 0)
)
elif function_name == 'generate_images':
NotificationService.notify_images_complete(
account=self.account,
site=site,
image_count=save_result.get('count', 0)
)
logger.info(f"[AIEngine] Created success notification for {function_name}")
except Exception as e:
# Don't fail the task if notification creation fails
logger.warning(f"[AIEngine] Failed to create success notification: {e}", exc_info=True)
def _create_failure_notification(self, function_name: str, error: str):
"""Create notification for failed AI task"""
if not self.account:
return
# Lazy import to avoid circular dependency and Django app loading issues
from igny8_core.business.notifications.services import NotificationService
try:
# Map function to appropriate failure notification method
if function_name == 'auto_cluster':
NotificationService.notify_clustering_failed(
account=self.account,
error=error
)
elif function_name == 'generate_ideas':
NotificationService.notify_ideas_failed(
account=self.account,
error=error
)
elif function_name == 'generate_content':
NotificationService.notify_content_failed(
account=self.account,
error=error
)
elif function_name == 'generate_image_prompts':
NotificationService.notify_prompts_failed(
account=self.account,
error=error
)
elif function_name == 'generate_images':
NotificationService.notify_images_failed(
account=self.account,
error=error
)
logger.info(f"[AIEngine] Created failure notification for {function_name}")
except Exception as e:
# Don't fail the task if notification creation fails
logger.warning(f"[AIEngine] Failed to create failure notification: {e}", exc_info=True)

View File

@@ -426,6 +426,21 @@ class KeywordViewSet(SiteSectorModelViewSet):
errors.append(f"Error adding '{seed_keyword.keyword}': {str(e)}")
skipped_count += 1
# Create notification if keywords were added
if created_count > 0:
try:
from igny8_core.business.notifications.services import NotificationService
NotificationService.notify_keywords_imported(
account=account,
site=site,
count=created_count
)
except Exception as e:
# Don't fail the request if notification fails
import logging
logger = logging.getLogger(__name__)
logger.warning(f"Failed to create notification for keywords import: {e}")
return success_response(
data={
'created': created_count,