def _retrieve(self, *args, **kwargs): url = self.url(action='get', params=self.attributes) retrieve_params = self.default_retrieve_params.copy() retrieve_params.update(kwargs) if self.attributes.get('course_id'): retrieve_params['course_id'] = text_type(self.course_id) if self.attributes.get('group_id'): retrieve_params['group_id'] = self.group_id try: response = utils.perform_request( 'get', url, retrieve_params, metric_action='model.retrieve', metric_tags=self._metric_tags, ) except utils.CommentClientRequestError as e: if e.status_code == 404: # attempt to gracefully recover from a previous failure # to sync this user to the comments service. self.save() response = utils.perform_request( 'get', url, retrieve_params, metric_action='model.retrieve', metric_tags=self._metric_tags, ) else: raise self._update_from_response(response)
def retire(self, retired_username): url = _url_for_retire(self.id) params = {'retired_username': retired_username} utils.perform_request('post', url, params, raw=True, metric_action='user.retire', metric_tags=self._metric_tags)
def replace_username(self, new_username): url = _url_for_username_replacement(self.id) params = {"new_username": new_username} utils.perform_request( 'post', url, params, raw=True, )
def read(self, source): """ Calls cs_comments_service to mark thread as read for the user """ params = {'source_type': source.type, 'source_id': source.id} utils.perform_request( 'post', _url_for_read(self.id), params, metric_action='user.read', metric_tags=self._metric_tags + ['target.type:{}'.format(source.type)], )
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))
def unfollow(self, source): params = {'source_type': source.type, 'source_id': source.id} response = utils.perform_request( 'delete', _url_for_subscription(self.id), params, metric_action='user.unfollow', metric_tags=self._metric_tags + ['target.type:{}'.format(source.type)], )
def un_pin(self, user, thread_id): url = _url_for_un_pin_thread(thread_id) params = {'user_id': user.id} response = utils.perform_request('put', url, params, metric_tags=self._metric_tags, metric_action='thread.unpin') self._update_from_response(response)
def flagAbuse(self, user, voteable): if voteable.type == 'thread': url = _url_for_flag_abuse_thread(voteable.id) elif voteable.type == 'comment': url = _url_for_flag_comment(voteable.id) else: raise utils.CommentClientRequestError( "Can only flag/unflag threads or comments") params = {'user_id': user.id} response = utils.perform_request('put', url, params, metric_action='thread.abuse.flagged', metric_tags=self._metric_tags) voteable._update_from_response(response)
def active_threads(self, query_params={}): if not self.course_id: raise utils.CommentClientRequestError("Must provide course_id when retrieving active threads for the user") url = _url_for_user_active_threads(self.id) params = {'course_id': text_type(self.course_id)} params = utils.merge_dict(params, query_params) response = utils.perform_request( 'get', url, params, metric_action='user.active_threads', metric_tags=self._metric_tags, paged_results=True, ) return response.get('collection', []), response.get('page', 1), response.get('num_pages', 1)
def active_threads(self, query_params={}): if not self.course_id: raise utils.CommentClientRequestError("Must provide course_id when retrieving active threads for the user") url = _url_for_user_active_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.active_threads', metric_tags=self._metric_tags, paged_results=True, ) return response.get('collection', []), response.get('page', 1), response.get('num_pages', 1)
def unvote(self, voteable): if voteable.type == 'thread': url = _url_for_vote_thread(voteable.id) elif voteable.type == 'comment': url = _url_for_vote_comment(voteable.id) else: raise utils.CommentClientRequestError("Can only vote / unvote for threads or comments") params = {'user_id': self.id} response = utils.perform_request( 'delete', url, params, metric_action='user.unvote', metric_tags=self._metric_tags + ['target.type:{}'.format(voteable.type)], ) voteable._update_from_response(response)
def _retrieve(self, *args, **kwargs): url = self.url(action='get', params=self.attributes) request_params = { 'recursive': kwargs.get('recursive'), 'with_responses': kwargs.get('with_responses', False), 'user_id': kwargs.get('user_id'), 'mark_as_read': kwargs.get('mark_as_read', True), 'resp_skip': kwargs.get('response_skip'), 'resp_limit': kwargs.get('response_limit'), } request_params = utils.strip_none(request_params) response = utils.perform_request('get', url, request_params, metric_action='model.retrieve', metric_tags=self._metric_tags) self._update_from_response(response)
def unFlagAbuse(self, user, voteable, removeAll): if voteable.type == 'thread': url = _url_for_unflag_abuse_thread(voteable.id) elif voteable.type == 'comment': url = _url_for_unflag_comment(voteable.id) else: raise utils.CommentClientRequestError( "Can only flag/unflag for threads or comments") params = {'user_id': user.id} #if you're an admin, when you unflag, remove ALL flags if removeAll: params['all'] = True response = utils.perform_request( 'put', url, params, metric_tags=self._metric_tags, metric_action='thread.abuse.unflagged') voteable._update_from_response(response)
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 = utils.merge_dict(params, 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) )
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))