Commit d2b4a6ea by Sarina Canelake

Merge pull request #5874 from stvstnfrd/up/course/about

Create helper function to remove duplicate code
parents 50152abb 04b97a3a
...@@ -11,6 +11,16 @@ from models.settings import course_grading ...@@ -11,6 +11,16 @@ from models.settings import course_grading
from xmodule.fields import Date from xmodule.fields import Date
from xmodule.modulestore.django import modulestore from xmodule.modulestore.django import modulestore
# This list represents the attribute keys for a course's 'about' info.
# Note: The 'video' attribute is intentionally excluded as it must be
# handled separately; its value maps to an alternate key name.
ABOUT_ATTRIBUTES = [
'syllabus',
'short_description',
'overview',
'effort',
]
class CourseDetails(object): class CourseDetails(object):
def __init__(self, org, course_id, run): def __init__(self, org, course_id, run):
...@@ -31,6 +41,18 @@ class CourseDetails(object): ...@@ -31,6 +41,18 @@ class CourseDetails(object):
self.course_image_asset_path = "" # URL of the course image self.course_image_asset_path = "" # URL of the course image
@classmethod @classmethod
def _fetch_about_attribute(cls, course_key, attribute):
"""
Retrieve an attribute from a course's "about" info
"""
usage_key = course_key.make_usage_key('about', attribute)
try:
value = modulestore().get_item(usage_key).data
except ItemNotFoundError:
value = None
return value
@classmethod
def fetch(cls, course_key): def fetch(cls, course_key):
""" """
Fetch the course details for the given course from persistence and return a CourseDetails model. Fetch the course details for the given course from persistence and return a CourseDetails model.
...@@ -45,36 +67,14 @@ class CourseDetails(object): ...@@ -45,36 +67,14 @@ class CourseDetails(object):
course_details.course_image_name = descriptor.course_image course_details.course_image_name = descriptor.course_image
course_details.course_image_asset_path = course_image_url(descriptor) course_details.course_image_asset_path = course_image_url(descriptor)
temploc = course_key.make_usage_key('about', 'syllabus') for attribute in ABOUT_ATTRIBUTES:
try: value = cls._fetch_about_attribute(course_key, attribute)
course_details.syllabus = modulestore().get_item(temploc).data if value is not None:
except ItemNotFoundError: setattr(course_details, attribute, value)
pass
temploc = course_key.make_usage_key('about', 'short_description')
try:
course_details.short_description = modulestore().get_item(temploc).data
except ItemNotFoundError:
pass
temploc = course_key.make_usage_key('about', 'overview')
try:
course_details.overview = modulestore().get_item(temploc).data
except ItemNotFoundError:
pass
temploc = course_key.make_usage_key('about', 'effort') raw_video = cls._fetch_about_attribute(course_key, 'video')
try: if raw_video:
course_details.effort = modulestore().get_item(temploc).data
except ItemNotFoundError:
pass
temploc = course_key.make_usage_key('about', 'video')
try:
raw_video = modulestore().get_item(temploc).data
course_details.intro_video = CourseDetails.parse_video_tag(raw_video) course_details.intro_video = CourseDetails.parse_video_tag(raw_video)
except ItemNotFoundError:
pass
return course_details return course_details
...@@ -160,8 +160,8 @@ class CourseDetails(object): ...@@ -160,8 +160,8 @@ class CourseDetails(object):
# NOTE: below auto writes to the db w/o verifying that any of the fields actually changed # NOTE: below auto writes to the db w/o verifying that any of the fields actually changed
# to make faster, could compare against db or could have client send over a list of which fields changed. # to make faster, could compare against db or could have client send over a list of which fields changed.
for about_type in ['syllabus', 'overview', 'effort', 'short_description']: for attribute in ABOUT_ATTRIBUTES:
cls.update_about_item(course_key, about_type, jsondict[about_type], descriptor, user) cls.update_about_item(course_key, attribute, jsondict[attribute], descriptor, user)
recomposed_video_tag = CourseDetails.recompose_video_tag(jsondict['intro_video']) recomposed_video_tag = CourseDetails.recompose_video_tag(jsondict['intro_video'])
cls.update_about_item(course_key, 'video', recomposed_video_tag, descriptor, user) cls.update_about_item(course_key, 'video', recomposed_video_tag, descriptor, user)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment