<commit_before>import logging

import waffle
from django.contrib.auth.models import Permission
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.signals import post_save
from django.dispatch import receiver
from slumber.exceptions import SlumberBaseException

from course_discovery.apps.publisher.models import CourseRun, OrganizationExtension
from course_discovery.apps.publisher.studio_api_utils import StudioAPI

logger = logging.getLogger(__name__)


def get_related_discovery_course_run(publisher_course_run):
    try:
        discovery_course = publisher_course_run.course.discovery_counterpart
        return discovery_course.course_runs.latest('start')
    except ObjectDoesNotExist:
        return


@receiver(post_save, sender=CourseRun)
def create_course_run_in_studio_receiver(sender, instance, created, **kwargs):  # pylint: disable=unused-argument
    if created and waffle.switch_is_active('enable_publisher_create_course_run_in_studio'):
        course = instance.course
        for organization in course.organizations.all():
            try:
                if not organization.organization_extension.auto_create_in_studio:
                    logger.warning(
                        ('Course run [%d] will not be automatically created in studio.'
                         'Organization [%s] has opted out of this feature.'),
                        course.id,
                        organization.key,
                    )
                    return
            except ObjectDoesNotExist:
                logger.exception(
                    'Organization [%s] does not have an associated OrganizationExtension',
                    organization.key,
                )

        partner = course.partner

        if not partner:
            logger.error(
                'Failed to publish course run [%d] to Studio. Related course [%d] has no associated Partner.',<commit_msg>Added logs in case discovery run is not found<commit_after>import logging

import waffle
from django.contrib.auth.models import Permission
from django.core.exceptions import ObjectDoesNotExist
from django.db.models.signals import post_save
from django.dispatch import receiver
from slumber.exceptions import SlumberBaseException

from course_discovery.apps.publisher.models import CourseRun, OrganizationExtension
from course_discovery.apps.publisher.studio_api_utils import StudioAPI

logger = logging.getLogger(__name__)


def get_related_discovery_course_run(publisher_course_run):
    try:
        discovery_course = publisher_course_run.course.discovery_counterpart
        return discovery_course.course_runs.latest('start')
    except ObjectDoesNotExist:
        logger.info(
            'Related discovery course run not found for [%s] with partner [%s] ',
            publisher_course_run.course.key,
            publisher_course_run.course.partner
        )
        return


@receiver(post_save, sender=CourseRun)
def create_course_run_in_studio_receiver(sender, instance, created, **kwargs):  # pylint: disable=unused-argument
    if created and waffle.switch_is_active('enable_publisher_create_course_run_in_studio'):
        course = instance.course
        for organization in course.organizations.all():
            try:
                if not organization.organization_extension.auto_create_in_studio:
                    logger.warning(
                        ('Course run [%d] will not be automatically created in studio.'
                         'Organization [%s] has opted out of this feature.'),
                        course.id,
                        organization.key,
                    )
                    return
            except ObjectDoesNotExist:
                logger.exception(
                    'Organization [%s] does not have an associated OrganizationExtension',
                    organization.key,
                )

        partner = course.partner

        if not partner:
            logger.error(
                'Failed to publish course run [%d] to Studio. Related course [%d] has no associated Partner.',
{'fraction_non_alphanumeric': 0.0639079333986288, 'fraction_numerical': 0.0, 'mean_word_length': 2.497431506849315, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '3421916', 'n_tokens_mistral': 1042, 'n_tokens_neox': 992, 'n_words': 267}