Commit 0223f3ff by Calen Pennington

Return a custom error for duplicate elements, and ignore it when loading into mongo

parent f5cf87f8
class InvalidDefinitionError(Exception):
pass
class NotFoundError(Exception):
pass
......@@ -14,5 +14,10 @@ class InsufficientSpecificationError(Exception):
class InvalidLocationError(Exception):
pass
class NoPathToItem(Exception):
pass
class DuplicateItemError(Exception):
pass
import pymongo
from bson.objectid import ObjectId
from bson.son import SON
from fs.osfs import OSFS
from itertools import repeat
......@@ -14,14 +13,13 @@ from mitxmako.shortcuts import render_to_string
from . import ModuleStore, Location
from .exceptions import (ItemNotFoundError, InsufficientSpecificationError,
NoPathToItem)
NoPathToItem, DuplicateItemError)
# TODO (cpennington): This code currently operates under the assumption that
# there is only one revision for each item. Once we start versioning inside the CMS,
# that assumption will have to change
class CachingDescriptorSystem(MakoDescriptorSystem):
"""
A system that has a cache of module json that it will use to load modules
......@@ -215,15 +213,22 @@ class MongoModuleStore(ModuleStore):
return self._load_items(list(items), depth)
# TODO (cpennington): This needs to be replaced by clone_item as soon as we allow
# creation of items from the cms
def create_item(self, location):
"""
Create an empty item at the specified location with the supplied editor
Create an empty item at the specified location.
If that location already exists, raises a DuplicateItemError
location: Something that can be passed to Location
"""
try:
self.collection.insert({
'_id': Location(location).dict(),
})
except pymongo.errors.DuplicateKeyError:
raise DuplicateItemError(location)
def update_item(self, location, data):
"""
......@@ -286,8 +291,6 @@ class MongoModuleStore(ModuleStore):
{'_id': True})
return [i['_id'] for i in items]
def path_to_location(self, location, course_name=None):
'''
Try to find a course_id/chapter/section[/position] path to this location.
......@@ -361,7 +364,6 @@ class MongoModuleStore(ModuleStore):
if path is None:
raise(NoPathToItem(location))
n = len(path)
course_id = CourseDescriptor.location_to_id(path[0])
chapter = path[1].name if n > 1 else None
......
import logging
from .xml import XMLModuleStore
from .exceptions import DuplicateItemError
log = logging.getLogger(__name__)
......@@ -27,7 +28,7 @@ def import_from_xml(store, data_dir, course_dirs=None, eager=True,
# This should in the future create new revisions of the items on import
try:
store.create_item(module.location)
except:
except DuplicateItemError:
log.exception('Item already exists at %s' % module.location.url())
pass
if 'data' in module.definition:
......
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