예제 #1
0
    def create(cls, course_overview, course=None):
        """
        Create thumbnail images for this CourseOverview.

        This will save the CourseOverviewImageSet before it returns.
        """
        from openedx.core.lib.courses import create_course_image_thumbnail

        # If image thumbnails are not enabled, do nothing.
        config = CourseOverviewImageConfig.current()
        if not config.enabled:
            return

        # If a course object was provided, use that. Otherwise, pull it from
        # CourseOverview's course_id. This happens because sometimes we are
        # generated as part of the CourseOverview creation (course is available
        # and passed in), and sometimes the CourseOverview already exists.
        if not course:
            course = modulestore().get_course(course_overview.id)

        image_set = cls(course_overview=course_overview)

        if course.course_image:
            # Try to create a thumbnails of the course image. If this fails for any
            # reason (weird format, non-standard URL, etc.), the URLs will default
            # to being blank. No matter what happens, we don't want to bubble up
            # a 500 -- an image_set is always optional.
            try:
                image_set.small_url = create_course_image_thumbnail(course, config.small)
                image_set.large_url = create_course_image_thumbnail(course, config.large)
            except Exception:  # pylint: disable=broad-except
                log.exception(
                    u"Could not create thumbnail for course %s with image %s (small=%s), (large=%s)",
                    course.id,
                    course.course_image,
                    config.small,
                    config.large
                )

        # Regardless of whether we created thumbnails or not, we need to save
        # this record before returning. If no thumbnails were created (there was
        # an error or the course has no source course_image), our url fields
        # just keep their blank defaults.
        try:
            with transaction.atomic():
                image_set.save()
                course_overview.image_set = image_set
        except (IntegrityError, ValueError):
            # In the event of a race condition that tries to save two image sets
            # to the same CourseOverview, we'll just silently pass on the one
            # that fails. They should be the same data anyway.
            #
            # The ValueError above is to catch the following error that can
            # happen in Django 1.8.4+ if the CourseOverview object fails to save
            # (again, due to race condition).
            #
            # Example: ValueError: save() prohibited to prevent data loss due
            #          to unsaved related object 'course_overview'.")
            pass
예제 #2
0
    def create(cls, course_overview, course=None):
        """
        Create thumbnail images for this CourseOverview.

        This will save the CourseOverviewImageSet before it returns.
        """
        from openedx.core.lib.courses import create_course_image_thumbnail

        # If image thumbnails are not enabled, do nothing.
        config = CourseOverviewImageConfig.current()
        if not config.enabled:
            return

        # If a course object was provided, use that. Otherwise, pull it from
        # CourseOverview's course_id. This happens because sometimes we are
        # generated as part of the CourseOverview creation (course is available
        # and passed in), and sometimes the CourseOverview already exists.
        if not course:
            course = modulestore().get_course(course_overview.id)

        image_set = cls(course_overview=course_overview)

        if course.course_image:
            # Try to create a thumbnails of the course image. If this fails for any
            # reason (weird format, non-standard URL, etc.), the URLs will default
            # to being blank. No matter what happens, we don't want to bubble up
            # a 500 -- an image_set is always optional.
            try:
                image_set.small_url = create_course_image_thumbnail(course, config.small)
                image_set.large_url = create_course_image_thumbnail(course, config.large)
            except Exception:  # pylint: disable=broad-except
                log.exception(
                    "Could not create thumbnail for course %s with image %s (small=%s), (large=%s)",
                    course.id,
                    course.course_image,
                    config.small,
                    config.large
                )

        # Regardless of whether we created thumbnails or not, we need to save
        # this record before returning. If no thumbnails were created (there was
        # an error or the course has no source course_image), our url fields
        # just keep their blank defaults.
        try:
            with transaction.atomic():
                image_set.save()
                course_overview.image_set = image_set
        except (IntegrityError, ValueError):
            # In the event of a race condition that tries to save two image sets
            # to the same CourseOverview, we'll just silently pass on the one
            # that fails. They should be the same data anyway.
            #
            # The ValueError above is to catch the following error that can
            # happen in Django 1.8.4+ if the CourseOverview object fails to save
            # (again, due to race condition).
            #
            # Example: ValueError: save() prohibited to prevent data loss due
            #          to unsaved related object 'course_overview'.")
            pass