tasks.py 1.53 KB
Newer Older
1 2 3 4 5 6
"""
Asynchronous tasks for the CCX app.
"""

import logging

7 8
from ccx_keys.locator import CCXLocator
from django.dispatch import receiver
9 10 11
from opaque_keys import InvalidKeyError
from opaque_keys.edx.locator import CourseLocator

12
from lms import CELERY_APP
13
from lms.djangoapps.ccx.models import CustomCourseForEdX
14
from xmodule.modulestore.django import SignalHandler
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

log = logging.getLogger("edx.ccx")


@receiver(SignalHandler.course_published)
def course_published_handler(sender, course_key, **kwargs):  # pylint: disable=unused-argument
    """
    Consume signals that indicate course published. If course already a CCX, do nothing.
    """
    if not isinstance(course_key, CCXLocator):
        send_ccx_course_published.delay(unicode(course_key))


@CELERY_APP.task
def send_ccx_course_published(course_key):
    """
    Find all CCX derived from this course, and send course published event for them.
    """
    course_key = CourseLocator.from_string(course_key)
    for ccx in CustomCourseForEdX.objects.filter(course_id=course_key):
        try:
36
            ccx_key = CCXLocator.from_course_locator(course_key, unicode(ccx.id))
37 38 39 40 41 42 43 44 45
        except InvalidKeyError:
            log.info('Attempt to publish course with deprecated id. Course: %s. CCX: %s', course_key, ccx.id)
            continue
        responses = SignalHandler.course_published.send(
            sender=ccx,
            course_key=ccx_key
        )
        for rec, response in responses:
            log.info('Signal fired when course is published. Receiver: %s. Response: %s', rec, response)