lms_result_processor.py 2.71 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
"""
This file contains implementation override of SearchResultProcessor which will allow
    * Blends in "location" property
    * Confirms user access to object
"""
from django.core.urlresolvers import reverse

from opaque_keys.edx.locations import SlashSeparatedCourseKey
from search.result_processor import SearchResultProcessor
from xmodule.modulestore.django import modulestore
Davorin Sego committed
11 12
from lms.djangoapps.course_blocks.api import get_course_blocks
from lms.djangoapps.courseware.access import has_access
13 14 15 16 17 18 19


class LmsSearchResultProcessor(SearchResultProcessor):
    """ SearchResultProcessor for LMS Search """
    _course_key = None
    _usage_key = None
    _module_store = None
Davorin Sego committed
20
    _course_blocks = {}
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39

    def get_course_key(self):
        """ fetch course key object from string representation - retain result for subsequent uses """
        if self._course_key is None:
            self._course_key = SlashSeparatedCourseKey.from_deprecated_string(self._results_fields["course"])
        return self._course_key

    def get_usage_key(self):
        """ fetch usage key for component from string representation - retain result for subsequent uses """
        if self._usage_key is None:
            self._usage_key = self.get_course_key().make_usage_key_from_deprecated_string(self._results_fields["id"])
        return self._usage_key

    def get_module_store(self):
        """ module store accessor - retain result for subsequent uses """
        if self._module_store is None:
            self._module_store = modulestore()
        return self._module_store

Davorin Sego committed
40 41 42 43 44 45 46
    def get_course_blocks(self, user):
        """ fetch cached blocks for course - retain for subsequent use """
        course_key = self.get_course_key()
        if course_key not in self._course_blocks:
            root_block_usage_key = self.get_module_store().make_course_usage_key(course_key)
            self._course_blocks[course_key] = get_course_blocks(user, root_block_usage_key)
        return self._course_blocks[course_key]
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

    @property
    def url(self):
        """
        Property to display the url for the given location, useful for allowing navigation
        """
        if "course" not in self._results_fields or "id" not in self._results_fields:
            raise ValueError("Must have course and id in order to build url")

        return reverse(
            "jump_to",
            kwargs={"course_id": self._results_fields["course"], "location": self._results_fields["id"]}
        )

    def should_remove(self, user):
        """ Test to see if this result should be removed due to access restriction """
Davorin Sego committed
63 64 65
        if has_access(user, 'staff', self.get_course_key()):
            return False
        return self.get_usage_key() not in self.get_course_blocks(user).get_block_keys()