Ejemplo n.º 1
0
    def get_items(self, location, course_id=None, depth=0, qualifiers=None):
        """
        Returns a list of XModuleDescriptor instances for the items
        that match location. Any element of location that is None is treated
        as a wildcard that matches any value. NOTE: don't use this to look for courses
        as the course_id is required. Use get_courses.

        location: either a Location possibly w/ None as wildcards for category or name or
        a Locator with at least a package_id and branch but possibly no block_id.

        depth: An argument that some module stores may use to prefetch
            descendents of the queried modules for more efficient results later
            in the request. The depth is counted in the number of calls to
            get_children() to cache. None indicates to cache all descendents
        """
        if not (course_id or hasattr(location, 'package_id')):
            raise Exception(
                "Must pass in a course_id when calling get_items()")

        store = self._get_modulestore_for_courseid(
            course_id or getattr(location, 'package_id'))
        # translate won't work w/ missing fields so work around it
        if store.reference_type == Location:
            if not self.use_locations:
                if getattr(location, 'block_id', False):
                    location = self._incoming_reference_adaptor(
                        store, course_id, location)
                else:
                    # get the course's location
                    location = loc_mapper().translate_locator_to_location(
                        location, get_course=True)
                    # now remove the unknowns
                    location = location.replace(category=qualifiers.get(
                        'category', None),
                                                name=None)
        else:
            if self.use_locations:
                if not isinstance(location, Location):
                    location = Location(location)
                try:
                    location.ensure_fully_specified()
                    location = loc_mapper().translate_location(
                        course_id, location, location.revision == 'published',
                        True)
                except InsufficientSpecificationError:
                    # construct the Locator by hand
                    if location.category is not None and qualifiers.get(
                            'category', False):
                        qualifiers['category'] = location.category
                    location = loc_mapper(
                    ).translate_location_to_course_locator(
                        course_id, location, location.revision == 'published')
        xblocks = store.get_items(location, course_id, depth, qualifiers)
        xblocks = [
            self._outgoing_xblock_adaptor(store, course_id, xblock)
            for xblock in xblocks
        ]
        return xblocks
Ejemplo n.º 2
0
    def get_items(self, location, course_id=None, depth=0, qualifiers=None):
        """
        Returns a list of XModuleDescriptor instances for the items
        that match location. Any element of location that is None is treated
        as a wildcard that matches any value. NOTE: don't use this to look for courses
        as the course_id is required. Use get_courses.

        location: either a Location possibly w/ None as wildcards for category or name or
        a Locator with at least a package_id and branch but possibly no block_id.

        depth: An argument that some module stores may use to prefetch
            descendents of the queried modules for more efficient results later
            in the request. The depth is counted in the number of calls to
            get_children() to cache. None indicates to cache all descendents
        """
        if not (course_id or hasattr(location, 'package_id')):
            raise Exception("Must pass in a course_id when calling get_items()")

        store = self._get_modulestore_for_courseid(course_id or getattr(location, 'package_id'))
        # translate won't work w/ missing fields so work around it
        if store.reference_type == Location:
            if not self.use_locations:
                if getattr(location, 'block_id', False):
                    location = self._incoming_reference_adaptor(store, course_id, location)
                else:
                    # get the course's location
                    location = loc_mapper().translate_locator_to_location(location, get_course=True)
                    # now remove the unknowns
                    location = location.replace(
                        category=qualifiers.get('category', None),
                        name=None
                    )
        else:
            if self.use_locations:
                if not isinstance(location, Location):
                    location = Location(location)
                try:
                    location.ensure_fully_specified()
                    location = loc_mapper().translate_location(
                        course_id, location, location.revision == 'published', True
                    )
                except InsufficientSpecificationError:
                    # construct the Locator by hand
                    if location.category is not None and qualifiers.get('category', False):
                        qualifiers['category'] = location.category
                    location = loc_mapper().translate_location_to_course_locator(
                        course_id, location, location.revision == 'published'
                    )
        xblocks = store.get_items(location, course_id, depth, qualifiers)
        xblocks = [self._outgoing_xblock_adaptor(store, course_id, xblock) for xblock in xblocks]
        return xblocks
Ejemplo n.º 3
0
 def get_parent_locations(self, location, course_id):
     """Find all locations that are the parents of this location in this
     course.  Needed for path_to_location().
     """
     location = Location.ensure_fully_specified(location)
     items = self.collection.find({"definition.children": location.url()}, {"_id": True})
     return [i["_id"] for i in items]
Ejemplo n.º 4
0
 def get_parent_locations(self, location, course_id):
     '''Find all locations that are the parents of this location in this
     course.  Needed for path_to_location().
     '''
     location = Location.ensure_fully_specified(location)
     items = self.collection.find({'definition.children': location.url()},
                                  {'_id': True})
     return [Location(i['_id']) for i in items]
Ejemplo n.º 5
0
 def get_parent_locations(self, location, course_id):
     '''Find all locations that are the parents of this location in this
     course.  Needed for path_to_location().
     '''
     location = Location.ensure_fully_specified(location)
     items = self.collection.find({'definition.children': location.url()},
                                  {'_id': True})
     return [i['_id'] for i in items]
Ejemplo n.º 6
0
 def has_item(self, course_id, location):
     """
     Returns True if location exists in this ModuleStore.
     """
     location = Location.ensure_fully_specified(location)
     try:
         self._find_one(location)
         return True
     except ItemNotFoundError:
         return False
Ejemplo n.º 7
0
 def has_item(self, course_id, location):
     """
     Returns True if location exists in this ModuleStore.
     """
     location = Location.ensure_fully_specified(location)
     try:
         self._find_one(location)
         return True
     except ItemNotFoundError:
         return False
Ejemplo n.º 8
0
    def set_attrs(self, location, attr_dict):
        """
        Like set_attr but sets multiple key value pairs.

        Returns nothing.

        Raises NotFoundError if no such item exists
        Raises AttributeError is attr_dict has any attrs which are one of the build in attrs.

        :param location:  a c4x asset location
        """
        # raises exception if location is not fully specified
        Location.ensure_fully_specified(location)
        for attr in attr_dict.iterkeys():
            if attr in ['_id', 'md5', 'uploadDate', 'length']:
                raise AttributeError("{} is a protected attribute.".format(attr))
        item = self.fs_files.find_one(location_to_query(location))
        if item is None:
            raise NotFoundError()
        self.fs_files.update({"_id": item["_id"]}, {"$set": attr_dict})
Ejemplo n.º 9
0
    def set_attrs(self, location, attr_dict):
        """
        Like set_attr but sets multiple key value pairs.

        Returns nothing.

        Raises NotFoundError if no such item exists
        Raises AttributeError is attr_dict has any attrs which are one of the build in attrs.

        :param location:  a c4x asset location
        """
        # raises exception if location is not fully specified
        Location.ensure_fully_specified(location)
        for attr in attr_dict.iterkeys():
            if attr in ['_id', 'md5', 'uploadDate', 'length']:
                raise AttributeError(
                    "{} is a protected attribute.".format(attr))
        item = self.fs_files.find_one(location_to_query(location))
        if item is None:
            raise NotFoundError()
        self.fs_files.update({"_id": item["_id"]}, {"$set": attr_dict})
Ejemplo n.º 10
0
 def get_instance_items(self, course_id, location, category, depth=0):
     """
     Return an array all of the locations for orphans in the course.
     """
     '''
     all_items = self.collection.find({
         '_id.org': location.org,
         '_id.course': location.course,
         #'_id.category': {'$nin': detached_categories}
     })
     '''
     location = Location.ensure_fully_specified(location)
     item = self._find_one(location)
     module = self._load_items_category([item], category, course_id, depth)[1]
     return module
Ejemplo n.º 11
0
    def get_item(self, location, depth=0):
        """
        Returns an XModuleDescriptor instance for the item at location.

        If any segment of the location is None except revision, raises
            xmodule.modulestore.exceptions.InsufficientSpecificationError
        If no object is found at that location, raises
            xmodule.modulestore.exceptions.ItemNotFoundError

        location: a Location object
        depth (int): An argument that some module stores may use to prefetch
            descendents of the queried modules for more efficient results later
            in the request. The depth is counted in the number of
            calls to get_children() to cache. None indicates to cache all descendents.
        """
        location = Location.ensure_fully_specified(location)
        item = self._find_one(location)
        module = self._load_items([item], depth)[0]
        return module
Ejemplo n.º 12
0
    def get_item(self, location, depth=0):
        """
        Returns an XModuleDescriptor instance for the item at location.

        If any segment of the location is None except revision, raises
            xmodule.modulestore.exceptions.InsufficientSpecificationError
        If no object is found at that location, raises
            xmodule.modulestore.exceptions.ItemNotFoundError

        location: a Location object
        depth (int): An argument that some module stores may use to prefetch
            descendents of the queried modules for more efficient results later
            in the request. The depth is counted in the number of
            calls to get_children() to cache. None indicates to cache all descendents.
        """
        location = Location.ensure_fully_specified(location)
        item = self._find_one(location)
        module = self._load_items([item], depth)[0]
        return module