From 2abe3f7e01d3756ea426a30014032525ea82d657 Mon Sep 17 00:00:00 2001 From: Robert Raposa Date: Tue, 13 Sep 2016 16:17:51 -0400 Subject: [PATCH] Reuse already retrieved user object. --- lms/djangoapps/discussion/views.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/lms/djangoapps/discussion/views.py b/lms/djangoapps/discussion/views.py index 2acbb8e366..ee3d6dd67e 100644 --- a/lms/djangoapps/discussion/views.py +++ b/lms/djangoapps/discussion/views.py @@ -63,10 +63,22 @@ def make_course_settings(course, user): @newrelic.agent.function_trace() -def get_threads(request, course, discussion_id=None, per_page=THREADS_PER_PAGE): +def get_threads(request, course, user_info, discussion_id=None, per_page=THREADS_PER_PAGE): """ This may raise an appropriate subclass of cc.utils.CommentClientError if something goes wrong, or ValueError if the group_id is invalid. + + Arguments: + request: The user request. + course: The course object. + user_info: The comment client User object as a dict. + discussion_id: Optional discussion id/commentable id for context. + per_page: Optional number of threads per page. + + Returns: + A tuple of the threads and the query parameters used for the search. + :return: + """ default_query_params = { 'page': 1, @@ -90,12 +102,9 @@ def get_threads(request, course, discussion_id=None, per_page=THREADS_PER_PAGE): if not request.GET.get('sort_key'): # If the user did not select a sort key, use their last used sort key - cc_user = cc.User.from_django_user(request.user) - cc_user.retrieve() - # TODO: After the comment service is updated this can just be - # user.default_sort_key because the service returns the default value - default_query_params['sort_key'] = cc_user.get('default_sort_key') or default_query_params['sort_key'] - else: + default_query_params['sort_key'] = user_info.get('default_sort_key') or default_query_params['sort_key'] + + elif request.GET.get('sort_key') != user_info.get('default_sort_key'): # If the user clicked a sort key, update their default sort key cc_user = cc.User.from_django_user(request.user) cc_user.default_sort_key = request.GET.get('sort_key') @@ -175,7 +184,7 @@ def inline_discussion(request, course_key, discussion_id): user_info = cc_user.to_dict() try: - threads, query_params = get_threads(request, course, discussion_id, per_page=INLINE_THREADS_PER_PAGE) + threads, query_params = get_threads(request, course, user_info, discussion_id, per_page=INLINE_THREADS_PER_PAGE) except ValueError: return HttpResponseBadRequest("Invalid group_id") @@ -212,7 +221,7 @@ def forum_form_discussion(request, course_key): user_info = user.to_dict() try: - unsafethreads, query_params = get_threads(request, course) # This might process a search query + unsafethreads, query_params = get_threads(request, course, user_info) # This might process a search query is_staff = has_permission(request.user, 'openclose_thread', course.id) threads = [utils.prepare_content(thread, course_key, is_staff) for thread in unsafethreads] except cc.utils.CommentClientMaintenanceError: @@ -335,7 +344,7 @@ def single_thread(request, course_key, discussion_id, thread_id): else: try: - threads, query_params = get_threads(request, course) + threads, query_params = get_threads(request, course, user_info) except ValueError: return HttpResponseBadRequest("Invalid group_id") threads.append(thread.to_dict())