示例#1
0
    def search(cls, query_params):

        # NOTE: Params 'recursive' and 'with_responses' are currently not used by
        # either the 'search' or 'get_all' actions below.  Both already use
        # with_responses=False internally in the comment service, so no additional
        # optimization is required.
        params = {
            'page': 1,
            'per_page': 20,
            'course_id': query_params['course_id'],
        }
        params.update(utils.strip_blank(utils.strip_none(query_params)))

        if query_params.get('text'):
            url = cls.url(action='search')
        else:
            url = cls.url(action='get_all',
                          params=utils.extract(params, 'commentable_id'))
            if params.get('commentable_id'):
                del params['commentable_id']
        response = utils.perform_request(
            'get',
            url,
            params,
            metric_tags=[u'course_id:{}'.format(query_params['course_id'])],
            metric_action='thread.search',
            paged_results=True)
        if query_params.get('text'):
            search_query = query_params['text']
            course_id = query_params['course_id']
            group_id = query_params[
                'group_id'] if 'group_id' in query_params else None
            requested_page = params['page']
            total_results = response.get('total_results')
            corrected_text = response.get('corrected_text')
            # Record search result metric to allow search quality analysis.
            # course_id is already included in the context for the event tracker
            tracker.emit(
                'edx.forum.searched', {
                    'query': search_query,
                    'corrected_text': corrected_text,
                    'group_id': group_id,
                    'page': requested_page,
                    'total_results': total_results,
                })
            log.info(
                u'forum_text_search query="{search_query}" corrected_text="{corrected_text}" course_id={course_id} group_id={group_id} page={requested_page} total_results={total_results}'
                .format(search_query=search_query,
                        corrected_text=corrected_text,
                        course_id=course_id,
                        group_id=group_id,
                        requested_page=requested_page,
                        total_results=total_results))

        return utils.CommentClientPaginatedResult(
            collection=response.get('collection', []),
            page=response.get('page', 1),
            num_pages=response.get('num_pages', 1),
            thread_count=response.get('thread_count', 0),
            corrected_text=response.get('corrected_text', None))
示例#2
0
 def subscribed_threads(self, query_params={}):
     if not self.course_id:
         raise utils.CommentClientRequestError(
             "Must provide course_id when retrieving subscribed threads for the user"
         )
     url = _url_for_user_subscribed_threads(self.id)
     params = {'course_id': text_type(self.course_id)}
     params.update(query_params)
     response = utils.perform_request(
         'get',
         url,
         params,
         metric_action='user.subscribed_threads',
         metric_tags=self._metric_tags,
         paged_results=True)
     return utils.CommentClientPaginatedResult(
         collection=response.get('collection', []),
         page=response.get('page', 1),
         num_pages=response.get('num_pages', 1),
         thread_count=response.get('thread_count', 0))