Details tab works except for file references

This commit is contained in:
Don Mitchell
2012-11-27 11:32:03 -05:00
parent 0ebdbb92ca
commit 2238019539
10 changed files with 197 additions and 103 deletions

View File

@@ -4,6 +4,8 @@ from xmodule.modulestore import Location
from xmodule.modulestore.exceptions import ItemNotFoundError
import json
from json.encoder import JSONEncoder
import time
from util.converters import time_to_date, jsdate_to_time
class CourseDetails:
def __init__(self, location):
@@ -29,11 +31,6 @@ class CourseDetails:
descriptor = modulestore('direct').get_item(course_location)
## DEBUG verify that this is a ClassDescriptor object
if not isinstance(descriptor, CourseDescriptor):
print("oops, not the expected type: ", descriptor)
## FIXME convert these from time.struct_time objects to something the client wants
course.start_date = descriptor.start
course.end_date = descriptor.end
course.enrollment_start = descriptor.enrollment_start
@@ -45,19 +42,19 @@ class CourseDetails:
except ItemNotFoundError:
pass
temploc = course_location._replace(name='overview')
temploc = temploc._replace(name='overview')
try:
course.overview = modulestore('direct').get_item(temploc).definition['data']
except ItemNotFoundError:
pass
temploc = course_location._replace(name='effort')
temploc = temploc._replace(name='effort')
try:
course.effort = modulestore('direct').get_item(temploc).definition['data']
except ItemNotFoundError:
pass
temploc = course_location._replace(name='video')
temploc = temploc._replace(name='video')
try:
course.intro_video = modulestore('direct').get_item(temploc).definition['data']
except ItemNotFoundError:
@@ -66,12 +63,10 @@ class CourseDetails:
return course
@classmethod
def update_from_json(cls, jsonval):
def update_from_json(cls, jsondict):
"""
Decode the json into CourseDetails and save any changed attrs to the db
"""
jsondict = json.loads(jsonval)
## TODO make it an error for this to be undefined & for it to not be retrievable from modulestore
course_location = jsondict['course_location']
## Will probably want to cache the inflight courses because every blur generates an update
@@ -79,38 +74,57 @@ class CourseDetails:
dirty = False
## FIXME do more accurate comparison (convert to time? or convert persisted from time)
if (jsondict['start_date'] != descriptor.start):
## ??? Will this comparison work?
if 'start_date' in jsondict:
converted = jsdate_to_time(jsondict['start_date'])
else:
converted = None
if converted != descriptor.start:
dirty = True
descriptor.start = jsondict['start_date']
descriptor.start = converted
if (jsondict['end_date'] != descriptor.start):
if 'end_date' in jsondict:
converted = jsdate_to_time(jsondict['end_date'])
else:
converted = None
if converted != descriptor.end:
dirty = True
descriptor.end = jsondict['end_date']
descriptor.end = converted
if (jsondict['enrollment_start'] != descriptor.enrollment_start):
if 'enrollment_start' in jsondict:
converted = jsdate_to_time(jsondict['enrollment_start'])
else:
converted = None
if converted != descriptor.enrollment_start:
dirty = True
descriptor.enrollment_start = jsondict['enrollment_start']
descriptor.enrollment_start = converted
if (jsondict['enrollment_end'] != descriptor.enrollment_end):
if 'enrollment_end' in jsondict:
converted = jsdate_to_time(jsondict['enrollment_end'])
else:
converted = None
if converted != descriptor.enrollment_end:
dirty = True
descriptor.enrollment_end = jsondict['enrollment_end']
descriptor.enrollment_end = converted
if dirty:
modulestore('direct').update_item(course_location, descriptor.definition['data'])
modulestore('direct').update_metadata(course_location, descriptor.metadata)
# 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.
temploc = course_location._replace(category='about', name='syllabus')
temploc = Location(course_location)._replace(category='about', name='syllabus')
modulestore('direct').update_item(temploc, jsondict['syllabus'])
temploc = course_location._replace(name='overview')
temploc = temploc._replace(name='overview')
modulestore('direct').update_item(temploc, jsondict['overview'])
temploc = course_location._replace(name='effort')
temploc = temploc._replace(name='effort')
modulestore('direct').update_item(temploc, jsondict['effort'])
temploc = course_location._replace(name='video')
temploc = temploc._replace(name='video')
modulestore('direct').update_item(temploc, jsondict['intro_video'])
@@ -124,5 +138,7 @@ class CourseDetailsEncoder(json.JSONEncoder):
return obj.__dict__
elif isinstance(obj, Location):
return obj.dict()
elif isinstance(obj, time.struct_time):
return time_to_date(obj)
else:
return JSONEncoder.default(self, obj)
return JSONEncoder.default(self, obj)