Commit 04b97a3a by stv

Create helper function to remove duplicate code

This repetitive code is especially pronounced in Stanford's fork, where
we have extended the About page to include several additional fields.

This pays down some technical debt by refactoring into a shared helper.
parent 1c6d197b
......@@ -11,6 +11,16 @@ from models.settings import course_grading
from xmodule.fields import Date
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):
def __init__(self, org, course_id, run):
......@@ -31,6 +41,18 @@ class CourseDetails(object):
self.course_image_asset_path = "" # URL of the course image
@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):
"""
Fetch the course details for the given course from persistence and return a CourseDetails model.
......@@ -45,36 +67,14 @@ class CourseDetails(object):
course_details.course_image_name = descriptor.course_image
course_details.course_image_asset_path = course_image_url(descriptor)
temploc = course_key.make_usage_key('about', 'syllabus')
try:
course_details.syllabus = modulestore().get_item(temploc).data
except ItemNotFoundError:
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
for attribute in ABOUT_ATTRIBUTES:
value = cls._fetch_about_attribute(course_key, attribute)
if value is not None:
setattr(course_details, attribute, value)
temploc = course_key.make_usage_key('about', 'effort')
try:
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
raw_video = cls._fetch_about_attribute(course_key, 'video')
if raw_video:
course_details.intro_video = CourseDetails.parse_video_tag(raw_video)
except ItemNotFoundError:
pass
return course_details
......@@ -160,8 +160,8 @@ class CourseDetails(object):
# 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.
for about_type in ['syllabus', 'overview', 'effort', 'short_description']:
cls.update_about_item(course_key, about_type, jsondict[about_type], descriptor, user)
for attribute in ABOUT_ATTRIBUTES:
cls.update_about_item(course_key, attribute, jsondict[attribute], descriptor, user)
recomposed_video_tag = CourseDetails.recompose_video_tag(jsondict['intro_video'])
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