112 lines
4.2 KiB
Python
112 lines
4.2 KiB
Python
"""
|
|
Django signals for psychology app.
|
|
Handles automated workflows and notifications.
|
|
"""
|
|
|
|
from django.db.models.signals import post_save, pre_save
|
|
from django.dispatch import receiver
|
|
from django.utils import timezone
|
|
|
|
from .models import (
|
|
PsychologyConsultation,
|
|
PsychologySession,
|
|
PsychologyGoal,
|
|
)
|
|
from .services import (
|
|
PsychologyRiskAssessmentService,
|
|
PsychologyNotificationService,
|
|
)
|
|
from core.documentation_tracking import DocumentationDelayTracker
|
|
|
|
|
|
@receiver(post_save, sender=PsychologyConsultation)
|
|
def handle_consultation_created(sender, instance, created, **kwargs):
|
|
"""
|
|
Handle post-save signal for PsychologyConsultation.
|
|
Creates risk alerts and documentation trackers.
|
|
"""
|
|
if created:
|
|
# Check for high risk and create alerts
|
|
risk_assessment = PsychologyRiskAssessmentService.assess_risk_level(instance)
|
|
|
|
if risk_assessment['requires_immediate_attention']:
|
|
PsychologyRiskAssessmentService.create_safety_alert(instance)
|
|
|
|
# Create documentation delay tracker
|
|
if instance.provider:
|
|
DocumentationDelayTracker.objects.create(
|
|
document_type='SESSION_NOTE', # Using SESSION_NOTE as closest match
|
|
document_id=instance.id,
|
|
patient=instance.patient,
|
|
assigned_to=instance.provider,
|
|
senior_therapist=instance.provider, # Should be actual senior
|
|
due_date=instance.consultation_date + timezone.timedelta(days=2),
|
|
tenant=instance.tenant,
|
|
)
|
|
|
|
|
|
@receiver(post_save, sender=PsychologySession)
|
|
def handle_session_created(sender, instance, created, **kwargs):
|
|
"""
|
|
Handle post-save signal for PsychologySession.
|
|
Creates documentation trackers and risk alerts.
|
|
"""
|
|
if created:
|
|
# Check for high risk in session
|
|
if instance.current_risk_level in ['MODERATE', 'HIGH']:
|
|
from notifications.models import Notification
|
|
Notification.objects.create(
|
|
user=instance.provider,
|
|
notification_type='SAFETY_ALERT',
|
|
title=f"Session Risk Alert: {instance.patient.get_full_name()}",
|
|
message=f"Session #{instance.session_number} shows {instance.get_current_risk_level_display()} risk level.",
|
|
)
|
|
|
|
# Create documentation delay tracker
|
|
if instance.provider:
|
|
DocumentationDelayTracker.objects.create(
|
|
document_type='SESSION_NOTE',
|
|
document_id=instance.id,
|
|
patient=instance.patient,
|
|
assigned_to=instance.provider,
|
|
senior_therapist=instance.provider, # Should be actual senior
|
|
due_date=instance.session_date + timezone.timedelta(days=2),
|
|
tenant=instance.tenant,
|
|
)
|
|
|
|
|
|
@receiver(pre_save, sender=PsychologyGoal)
|
|
def handle_goal_status_change(sender, instance, **kwargs):
|
|
"""
|
|
Handle pre-save signal for PsychologyGoal.
|
|
Auto-set achieved_date when goal is marked as achieved.
|
|
"""
|
|
if instance.pk:
|
|
try:
|
|
old_instance = PsychologyGoal.objects.get(pk=instance.pk)
|
|
# If status changed to ACHIEVED and achieved_date not set
|
|
if (old_instance.status != 'ACHIEVED' and
|
|
instance.status == 'ACHIEVED' and
|
|
not instance.achieved_date):
|
|
instance.achieved_date = timezone.now().date()
|
|
instance.progress_percentage = 100
|
|
except PsychologyGoal.DoesNotExist:
|
|
pass
|
|
|
|
|
|
@receiver(post_save, sender=PsychologyGoal)
|
|
def handle_goal_achieved(sender, instance, created, **kwargs):
|
|
"""
|
|
Handle post-save signal for PsychologyGoal.
|
|
Send notification when goal is achieved.
|
|
"""
|
|
if not created and instance.status == 'ACHIEVED':
|
|
from notifications.models import Notification
|
|
if instance.consultation and instance.consultation.provider:
|
|
Notification.objects.create(
|
|
user=instance.consultation.provider,
|
|
notification_type='SUCCESS',
|
|
title=f"Goal Achieved: {instance.patient.get_full_name()}",
|
|
message=f"Treatment goal achieved: {instance.goal_description[:100]}",
|
|
)
|