HH/apps/social/signals.py
2026-02-12 15:09:48 +03:00

41 lines
1.5 KiB
Python

"""
Django signals for automatic AI analysis of comments.
Triggers background analysis when new comments are created.
"""
import logging
from django.db.models.signals import post_save
from django.dispatch import receiver
from apps.social.models import SocialComment
from apps.social.tasks.ai import analyze_comment_task
logger = logging.getLogger(__name__)
@receiver(post_save, sender=SocialComment)
def analyze_comment_on_creation(sender, instance, created, **kwargs):
"""
Automatically trigger AI analysis when a new comment is created.
This signal fires immediately after a SocialComment is saved to the database.
It checks if it's a new comment (created=True) and triggers
background analysis via Celery.
Args:
sender: The model class that sent the signal
instance: The actual comment instance that was saved
created: Boolean - True if this is a new record, False if it's an update
**kwargs: Additional keyword arguments
"""
if created:
logger.info(f"New comment created (ID: {instance.id}), triggering AI analysis...")
# Trigger background analysis task
try:
analyze_comment_task.delay(instance.id)
logger.info(f"AI analysis task queued for comment {instance.id}")
except Exception as e:
logger.error(f"Failed to queue AI analysis for comment {instance.id}: {e}")
# Don't raise exception to avoid preventing comment from being saved