Commit 0223f3ff by Calen Pennington

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

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