remove course location from loc_mapper on delete

This commit is contained in:
zubiar-arbi
2014-03-19 17:09:21 +05:00
parent 982f24aed1
commit 9d04a9270b
5 changed files with 145 additions and 18 deletions

View File

@@ -500,3 +500,34 @@ class LocMapperStore(object):
setmany[u'{}+{}'.format(old_course_id, location.url())] = (published_usage, draft_usage)
setmany[old_course_id] = (published_usage, draft_usage)
self.cache.set_many(setmany)
def delete_course_mapping(self, course_location):
"""
Remove provided course location from loc_mapper and cache.
:param course_location: a Location whose category is 'course'.
"""
course_locator = self.translate_location(course_location.course_id, course_location)
course_locator_draft = self.translate_location(
course_location.course_id, course_location, published=False
)
self.location_map.remove({'course_id': course_locator.package_id})
self._delete_cache_location_map_entry(
course_location.course_id, course_location, course_locator, course_locator_draft
)
def _delete_cache_location_map_entry(self, old_course_id, location, published_usage, draft_usage):
"""
Remove the location of course (draft and published) from cache
"""
delete_keys = []
if location.category == 'course':
delete_keys.append(u'courseId+{}'.format(published_usage.package_id))
delete_keys.append(u'courseIdLower+{}'.format(published_usage.package_id.lower()))
delete_keys.append(unicode(published_usage))
delete_keys.append(unicode(draft_usage))
delete_keys.append(u'{}+{}'.format(old_course_id, location.url()))
delete_keys.append(old_course_id)
self.cache.delete_many(delete_keys)

View File

@@ -1,6 +1,7 @@
import re
from xmodule.contentstore.content import StaticContent
from xmodule.modulestore import Location
from xmodule.modulestore.django import loc_mapper
import logging
@@ -266,4 +267,7 @@ def delete_course(modulestore, contentstore, source_location, commit=False):
if commit:
modulestore.delete_item(source_location)
# remove location of this course from loc_mapper and cache
loc_mapper().delete_course_mapping(source_location)
return True

View File

@@ -80,6 +80,38 @@ class TestLocationMapper(LocMapperSetupSansDjango):
self.assertEqual(entry['prod_branch'], 'live')
self.assertEqual(entry['block_map'], block_map)
def test_delete_course_map(self):
"""
Test that course location is properly remove from loc_mapper and cache when course is deleted
"""
org = u'foo_org'
course = u'bar_course'
run = u'baz_run'
course_location = Location('i4x', org, course, 'course', run)
course_locator = loc_mapper().translate_location(course_location.course_id, course_location)
loc_mapper().create_map_entry(course_location)
# pylint: disable=protected-access
entry = loc_mapper().location_map.find_one({
'_id': loc_mapper()._construct_location_son(org, course, run)
})
self.assertIsNotNone(entry, 'Entry not found in loc_mapper')
self.assertEqual(entry['course_id'], u'{0}.{1}.{2}'.format(org, course, run))
# now delete course location from loc_mapper and cache and test that course location no longer
# exists in loca_mapper and cache
loc_mapper().delete_course_mapping(course_location)
# pylint: disable=protected-access
entry = loc_mapper().location_map.find_one({
'_id': loc_mapper()._construct_location_son(org, course, run)
})
self.assertIsNone(entry, 'Entry found in loc_mapper')
# pylint: disable=protected-access
cached_value = loc_mapper()._get_location_from_cache(course_locator)
self.assertIsNone(cached_value, 'course_locator found in cache')
# pylint: disable=protected-access
cached_value = loc_mapper()._get_course_location_from_cache(course_locator.package_id)
self.assertIsNone(cached_value, 'Entry found in cache')
def translate_n_check(self, location, old_style_course_id, new_style_package_id, block_id, branch, add_entry=False):
"""
Request translation, check package_id, block_id, and branch
@@ -427,3 +459,10 @@ class TrivialCache(object):
mock set
"""
self.cache[key] = entry
def delete_many(self, entries):
"""
mock delete_many
"""
for entry in entries:
del self.cache[entry]