You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
1.8 KiB
43 lines
1.8 KiB
import logging
|
|
from django.db.models.signals import pre_save, post_save
|
|
from django.dispatch import receiver
|
|
from apps.certificate.models import Certificate
|
|
from apps.account.notification_service import create_and_send_notification
|
|
from apps.course.models.course import extract_text_from_json, get_localized_field
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
@receiver(pre_save, sender=Certificate)
|
|
def store_certificate_previous_status(sender, instance, **kwargs):
|
|
if instance.pk:
|
|
try:
|
|
old = Certificate.objects.get(pk=instance.pk)
|
|
instance._previous_status = old.status
|
|
except Certificate.DoesNotExist:
|
|
instance._previous_status = None
|
|
else:
|
|
instance._previous_status = None
|
|
|
|
|
|
@receiver(post_save, sender=Certificate)
|
|
def notify_certificate_issued(sender, instance, created, **kwargs):
|
|
# Trigger when status becomes approved
|
|
if instance.status == 'approved':
|
|
was_approved = False
|
|
if created:
|
|
was_approved = True
|
|
elif hasattr(instance, '_previous_status') and instance._previous_status != 'approved':
|
|
was_approved = True
|
|
|
|
if was_approved:
|
|
course_title_en = get_localized_field('en', instance.course.title)
|
|
course_title_fa = get_localized_field('fa', instance.course.title)
|
|
create_and_send_notification(
|
|
user=instance.student,
|
|
title_en="Certificate Issued",
|
|
body_en=f"Your certificate for '{course_title_en}' has been issued.",
|
|
title_fa="گواهی صادر شد",
|
|
body_fa=f"گواهی پایان دوره شما برای «{course_title_fa}» صادر شد.",
|
|
service='imam-javad',
|
|
data={'type': 'certificate_issued', 'certificate_id': instance.id}
|
|
)
|