Merge pull request #1580 from edx/christina/item

Change delete_item to RESTful URL.
This commit is contained in:
Christina Roberts
2013-11-06 10:50:56 -08:00
21 changed files with 172 additions and 106 deletions

View File

@@ -17,7 +17,7 @@ def expect_json(view_function):
def parse_json_into_request(request, *args, **kwargs):
# cdodge: fix postback errors in CMS. The POST 'content-type' header can include additional information
# e.g. 'charset', so we can't do a direct string compare
if "application/json" in request.META.get('CONTENT_TYPE', ''):
if "application/json" in request.META.get('CONTENT_TYPE', '') and request.body:
request.json = json.loads(request.body)
else:
request.json = {}

View File

@@ -167,7 +167,7 @@ class LocMapperStore(object):
return BlockUsageLocator(course_id=entry['course_id'], branch=branch, usage_id=usage_id)
def translate_locator_to_location(self, locator):
def translate_locator_to_location(self, locator, get_course=False):
"""
Returns an old style Location for the given Locator if there's an appropriate entry in the
mapping collection. Note, it requires that the course was previously mapped (a side effect of
@@ -175,6 +175,9 @@ class LocMapperStore(object):
the block's usage_id was previously stored in the
map (a side effect of translate_location or via add|update_block_location).
If get_course, then rather than finding the map for this locator, it finds the 'course' root
for the mapped course.
If there are no matches, it returns None.
If there's more than one location to locator mapping to the same course_id, it looks for the first
@@ -191,7 +194,16 @@ class LocMapperStore(object):
for candidate in maps:
for old_name, cat_to_usage in candidate['block_map'].iteritems():
for category, usage_id in cat_to_usage.iteritems():
if usage_id == locator.usage_id:
if get_course:
if category == 'course':
return Location(
'i4x',
candidate['_id']['org'],
candidate['_id']['course'],
'course',
self._decode_from_mongo(old_name),
None)
elif usage_id == locator.usage_id:
# figure out revision
# enforce the draft only if category in [..] logic
if category in draft.DIRECT_ONLY_CATEGORIES:

View File

@@ -263,7 +263,7 @@ class CourseLocator(Locator):
version_guid=self.version_guid,
branch=self.branch)
def url_reverse(self, prefix, postfix):
def url_reverse(self, prefix, postfix=''):
"""
Do what reverse is supposed to do but seems unable to do. Generate a url using prefix unicode(self) postfix
:param prefix: the beginning of the url (will be forced to begin and end with / if non-empty)

View File

@@ -81,7 +81,7 @@ class DraftModuleStore(MongoModuleStore):
try:
return wrap_draft(super(DraftModuleStore, self).get_item(as_draft(location), depth=depth))
except ItemNotFoundError:
return wrap_draft(super(DraftModuleStore, self).get_item(location, depth=depth))
return wrap_draft(super(DraftModuleStore, self).get_item(as_published(location), depth=depth))
def get_instance(self, course_id, location, depth=0):
"""
@@ -169,7 +169,7 @@ class DraftModuleStore(MongoModuleStore):
try:
draft_item = self.get_item(location)
if not getattr(draft_item, 'is_draft', False):
self.convert_to_draft(location)
self.convert_to_draft(as_published(location))
except ItemNotFoundError, e:
if not allow_not_found:
raise e
@@ -187,7 +187,7 @@ class DraftModuleStore(MongoModuleStore):
draft_loc = as_draft(location)
draft_item = self.get_item(location)
if not getattr(draft_item, 'is_draft', False):
self.convert_to_draft(location)
self.convert_to_draft(as_published(location))
return super(DraftModuleStore, self).update_children(draft_loc, children)
@@ -203,7 +203,7 @@ class DraftModuleStore(MongoModuleStore):
draft_item = self.get_item(location)
if not getattr(draft_item, 'is_draft', False):
self.convert_to_draft(location)
self.convert_to_draft(as_published(location))
if 'is_draft' in metadata:
del metadata['is_draft']
@@ -262,7 +262,7 @@ class DraftModuleStore(MongoModuleStore):
"""
Turn the published version into a draft, removing the published version
"""
self.convert_to_draft(location)
self.convert_to_draft(as_published(location))
super(DraftModuleStore, self).delete_item(location)
def _query_children_for_cache_children(self, items):

View File

@@ -258,13 +258,17 @@ class TestLocationMapper(unittest.TestCase):
new_style_course_id,
block_map={
'abc123': {'problem': 'problem2'},
'48f23a10395384929234': {'chapter': 'chapter48f'}
'48f23a10395384929234': {'chapter': 'chapter48f'},
'baz_run': {'course': 'root'},
}
)
# only one course matches
prob_location = loc_mapper().translate_locator_to_location(prob_locator)
# default branch
self.assertEqual(prob_location, Location('i4x', org, course, 'problem', 'abc123', None))
# test get_course keyword
prob_location = loc_mapper().translate_locator_to_location(prob_locator, get_course=True)
self.assertEqual(prob_location, Location('i4x', org, course, 'course', 'baz_run', None))
# explicit branch
prob_locator = BlockUsageLocator(
course_id=prob_locator.course_id, branch='draft', usage_id=prob_locator.usage_id

View File

@@ -97,15 +97,16 @@ def export_to_xml(modulestore, contentstore, course_location, root_dir, course_d
if len(draft_verticals) > 0:
draft_course_dir = export_fs.makeopendir('drafts')
for draft_vertical in draft_verticals:
parent_locs = draft_modulestore.get_parent_locations(draft_vertical.location, course.location.course_id)
# Don't try to export orphaned items.
if len(parent_locs) > 0:
logging.debug('parent_locs = {0}'.format(parent_locs))
draft_vertical.xml_attributes['parent_sequential_url'] = Location(parent_locs[0]).url()
sequential = modulestore.get_item(Location(parent_locs[0]))
index = sequential.children.index(draft_vertical.location.url())
draft_vertical.xml_attributes['index_in_children_list'] = str(index)
draft_vertical.export_to_xml(draft_course_dir)
if getattr(draft_vertical, 'is_draft', False):
parent_locs = draft_modulestore.get_parent_locations(draft_vertical.location, course.location.course_id)
# Don't try to export orphaned items.
if len(parent_locs) > 0:
logging.debug('parent_locs = {0}'.format(parent_locs))
draft_vertical.xml_attributes['parent_sequential_url'] = Location(parent_locs[0]).url()
sequential = modulestore.get_item(Location(parent_locs[0]))
index = sequential.children.index(draft_vertical.location.url())
draft_vertical.xml_attributes['index_in_children_list'] = str(index)
draft_vertical.export_to_xml(draft_course_dir)
def export_extra_content(export_fs, modulestore, course_id, course_location, category_type, dirname, file_suffix=''):