<commit_before>"""
Signal handler for enabling/disabling self-generated certificates based on the course-pacing.
"""

import logging

import six
from django.db.models.signals import post_save
from django.dispatch import receiver

from common.djangoapps.course_modes.models import CourseMode
from common.djangoapps.student.models import CourseEnrollment
from lms.djangoapps.certificates.generation_handler import (
    generate_allowlist_certificate_task,
    is_using_certificate_allowlist_and_is_on_allowlist
)
from lms.djangoapps.certificates.models import (
    CertificateGenerationCourseSetting,
    CertificateStatuses,
    CertificateWhitelist,
    GeneratedCertificate
)
from lms.djangoapps.certificates.tasks import CERTIFICATE_DELAY_SECONDS, generate_certificate
from lms.djangoapps.grades.api import CourseGradeFactory
from lms.djangoapps.verify_student.services import IDVerificationService
from openedx.core.djangoapps.certificates.api import auto_certificate_generation_enabled
from openedx.core.djangoapps.content.course_overviews.signals import COURSE_PACING_CHANGED
from openedx.core.djangoapps.signals.signals import (
    COURSE_GRADE_NOW_FAILED,
    COURSE_GRADE_NOW_PASSED,
    LEARNER_NOW_VERIFIED
)

log = logging.getLogger(__name__)


@receiver(COURSE_PACING_CHANGED, dispatch_uid="update_cert_settings_on_pacing_change")
def _update_cert_settings_on_pacing_change(sender, updated_course_overview, **kwargs):  # pylint: disable=unused-argument
    """
    Catches the signal that course pacing has changed and enable/disable
    the self-generated certificates according to course-pacing.
    """
    CertificateGenerationCourseSetting.set_self_generation_enabled_for_course(
        updated_course_overview.id,
        updated_course_overview.self_paced,
    )
    log.info(u'Certificate Generation Setting Toggled for {course_id} via pacing change'.format(
        course_id=updated_course_overview.id
    ))


@receiver(post_save, sender=CertificateWhitelist, dispatch_uid="append_certificate_whitelist")
def _listen_for_certificate_whitelist_append(sender, instance, **kwargs):  # lint-amnesty, pylint: disable=missing-function-docstring, unused-argument
    course = CourseOverview.get_from_id(instance.course_id)  # lint-amnesty, pylint: disable=unused-variable

    if not auto_certificate_generation_enabled():
        return

    if fire_ungenerated_certificate_task(instance.user, instance.course_id):
        log.info(u'Certificate generation task initiated for {user} : {course} via whitelist'.format(
            user=instance.user.id,
            course=instance.course_id
        ))


@receiver(COURSE_GRADE_NOW_PASSED, dispatch_uid="new_passing_learner")
def listen_for_passing_grade(sender, user, course_id, **kwargs):  # pylint: disable=unused-argument
    """
    Listen for a learner passing a course, send cert generation task,
    downstream signal from COURSE_GRADE_CHANGED
    """
<<<<<<< HEAD
=======
    course = CourseOverview.get_from_id(course_id)  # lint-amnesty, pylint: disable=unused-variable
>>>>>>> Applied pylint-amnesty to certificates
    if not auto_certificate_generation_enabled():
        return
<commit_msg>Applied pylint-amnesty to certificates<commit_after>"""
Signal handler for enabling/disabling self-generated certificates based on the course-pacing.
"""

import logging

import six
from django.db.models.signals import post_save
from django.dispatch import receiver

from common.djangoapps.course_modes.models import CourseMode
from common.djangoapps.student.models import CourseEnrollment
from lms.djangoapps.certificates.generation_handler import (
    generate_allowlist_certificate_task,
    is_using_certificate_allowlist_and_is_on_allowlist
)
from lms.djangoapps.certificates.models import (
    CertificateGenerationCourseSetting,
    CertificateStatuses,
    CertificateWhitelist,
    GeneratedCertificate
)
from lms.djangoapps.certificates.tasks import CERTIFICATE_DELAY_SECONDS, generate_certificate
from lms.djangoapps.grades.api import CourseGradeFactory
from lms.djangoapps.verify_student.services import IDVerificationService
from openedx.core.djangoapps.certificates.api import auto_certificate_generation_enabled
from openedx.core.djangoapps.content.course_overviews.signals import COURSE_PACING_CHANGED
from openedx.core.djangoapps.signals.signals import (
    COURSE_GRADE_NOW_FAILED,
    COURSE_GRADE_NOW_PASSED,
    LEARNER_NOW_VERIFIED
)

log = logging.getLogger(__name__)


@receiver(COURSE_PACING_CHANGED, dispatch_uid="update_cert_settings_on_pacing_change")
def _update_cert_settings_on_pacing_change(sender, updated_course_overview, **kwargs):  # pylint: disable=unused-argument
    """
    Catches the signal that course pacing has changed and enable/disable
    the self-generated certificates according to course-pacing.
    """
    CertificateGenerationCourseSetting.set_self_generation_enabled_for_course(
        updated_course_overview.id,
        updated_course_overview.self_paced,
    )
    log.info(u'Certificate Generation Setting Toggled for {course_id} via pacing change'.format(
        course_id=updated_course_overview.id
    ))


@receiver(post_save, sender=CertificateWhitelist, dispatch_uid="append_certificate_whitelist")
def _listen_for_certificate_whitelist_append(sender, instance, **kwargs):  # lint-amnesty, pylint: disable=missing-function-docstring, unused-argument
    course = CourseOverview.get_from_id(instance.course_id)  # lint-amnesty, pylint: disable=unused-variable
    if not auto_certificate_generation_enabled():
        return

    if fire_ungenerated_certificate_task(instance.user, instance.course_id):
        log.info(u'Certificate generation task initiated for {user} : {course} via whitelist'.format(
            user=instance.user.id,
            course=instance.course_id
        ))


@receiver(COURSE_GRADE_NOW_PASSED, dispatch_uid="new_passing_learner")
def listen_for_passing_grade(sender, user, course_id, **kwargs):  # pylint: disable=unused-argument
    """
    Listen for a learner passing a course, send cert generation task,
    downstream signal from COURSE_GRADE_CHANGED
    """
<<<<<<< HEAD
<<<<<<< HEAD
=======
    course = CourseOverview.get_from_id(course_id)  # lint-amnesty, pylint: disable=unused-variable
>>>>>>> Applied pylint-amnesty to certificates
=======
    course = CourseOverview.get_from_id(course_id)  # lint-amnesty, pylint: disable=unused-variable
>>>>>>> Applied pylint-amnesty to certificates
    if not auto_certificate_generation_enabled():
        return

{'fraction_non_alphanumeric': 0.07874617737003058, 'fraction_numerical': 0.0, 'mean_word_length': 5.59375, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7376399', 'n_tokens_mistral': 2036, 'n_tokens_neox': 1916, 'n_words': 375}