Files
edx-platform/openedx/core/djangoapps/notifications/handlers.py
Ahtisham Shahid 118ea3a024 feat: added enrollment api for notification config (#32162)
* feat: added enrollment API for notification config

* feat: added apps.py in notifications

* feat: added waffle flag for notification app

* feat: added proper docs for the API
2023-05-09 11:51:31 +05:00

27 lines
996 B
Python

"""
Handlers for notifications
"""
import logging
from django.db import IntegrityError
from django.db.models.signals import post_save
from django.dispatch import receiver
from openedx.core.djangoapps.notifications.config.waffle import ENABLE_NOTIFICATIONS
from openedx.core.djangoapps.notifications.models import NotificationPreference
log = logging.getLogger(__name__)
@receiver(post_save, sender='student.CourseEnrollment')
def course_enrollment_post_save(sender, instance, created, **kwargs):
"""
Watches for post_save signal for creates on the CourseEnrollment table.
Generate a NotificationPreference if new Enrollment is created
"""
if created and ENABLE_NOTIFICATIONS.is_enabled(instance.course_id):
try:
NotificationPreference.objects.create(user=instance.user, course_id=instance.course_id)
except IntegrityError:
log.info(f'NotificationPreference already exists for user {instance.user} and course {instance.course_id}')