courses.py 1.93 KB
Newer Older
1 2 3 4 5
"""
Common utility functions related to courses.
"""
from django.conf import settings

6
from xmodule.assetstore.assetmgr import AssetManager
7
from xmodule.contentstore.content import StaticContent
8
from xmodule.contentstore.django import contentstore
9 10


11
def course_image_url(course, image_key='course_image'):
12
    """Try to look up the image url for the course.  If it's not found,
13 14
    log an error and return the dead link.
    image_key can be one of the three: 'course_image', 'hero_image', 'thumbnail_image' """
15
    if course.static_asset_path:
16
        # If we are a static course with the image_key attribute
17 18 19 20
        # set different than the default, return that path so that
        # courses can use custom course image paths, otherwise just
        # return the default static path.
        url = '/static/' + (course.static_asset_path or getattr(course, 'data_dir', ''))
21 22
        if hasattr(course, image_key) and getattr(course, image_key) != course.fields[image_key].default:
            url += '/' + getattr(course, image_key)
23
        else:
24 25 26
            url += '/images/' + image_key + '.jpg'
    elif not getattr(course, image_key):
        # if image_key is empty, use the default image url from settings
27 28
        url = settings.STATIC_URL + settings.DEFAULT_COURSE_ABOUT_IMAGE_URL
    else:
29
        loc = StaticContent.compute_location(course.id, getattr(course, image_key))
30
        url = StaticContent.serialize_asset_key_with_slash(loc)
31

32
    return url
33 34 35 36 37 38 39 40 41 42 43 44 45


def create_course_image_thumbnail(course, dimensions):
    """Create a course image thumbnail and return the URL.

    - dimensions is a tuple of (width, height)
    """
    course_image_asset_key = StaticContent.compute_location(course.id, course.course_image)
    course_image = AssetManager.find(course_image_asset_key)  # a StaticContent obj

    _content, thumb_loc = contentstore().generate_thumbnail(course_image, dimensions=dimensions)

    return StaticContent.serialize_asset_key_with_slash(thumb_loc)