Skip to content
Snippets Groups Projects
Unverified Commit c724b1b0 authored by Eric Fischer's avatar Eric Fischer Committed by GitHub
Browse files

Merge pull request #18052 from edx/efischer/cache_stuff

Cache get_group_id_for_user
parents bb026ff8 30476a33
No related branches found
Tags release-2020-12-15-18.35
No related merge requests found
......@@ -38,6 +38,7 @@ from openedx.core.djangoapps.content.course_structures.models import CourseStruc
from openedx.core.djangoapps.course_groups import cohorts
from openedx.core.djangoapps.course_groups.cohorts import set_course_cohorted
from openedx.core.djangoapps.course_groups.tests.helpers import CohortFactory, config_course_cohorts
from openedx.core.djangoapps.request_cache.middleware import RequestCache
from openedx.core.djangoapps.util.testing import ContentGroupTestCase
from student.roles import CourseStaffRole
from student.tests.factories import AdminFactory, CourseEnrollmentFactory, UserFactory
......@@ -1785,6 +1786,7 @@ class GroupModeratorPermissionsTestCase(ModuleStoreTestCase):
'can_vote': True,
'can_report': True
})
RequestCache.clear_request_cache()
set_discussion_division_settings(self.course.id, division_scheme=CourseDiscussionSettings.ENROLLMENT_TRACK)
content = {'user_id': self.verified_user.id, 'type': 'thread', 'username': self.verified_user.username}
......
......@@ -592,15 +592,14 @@ def get_user_group_ids(course_id, content, user=None):
content_user_group_id = None
user_group_id = None
if course_id is not None:
course_discussion_settings = get_course_discussion_settings(course_id)
if content.get('username'):
try:
content_user = get_user_by_username_or_email(content.get('username'))
content_user_group_id = get_group_id_for_user(content_user, course_discussion_settings)
content_user_group_id = get_group_id_for_user_from_cache(content_user, course_id)
except User.DoesNotExist:
content_user_group_id = None
user_group_id = get_group_id_for_user(user, course_discussion_settings) if user else None
user_group_id = get_group_id_for_user_from_cache(user, course_id) if user else None
return user_group_id, content_user_group_id
......@@ -819,7 +818,7 @@ def get_group_id_for_comments_service(request, course_key, commentable_id=None):
_verify_group_exists(group_id, course_discussion_settings)
else:
# regular users always query with their own id.
group_id = get_group_id_for_user(request.user, course_discussion_settings)
group_id = get_group_id_for_user_from_cache(request.user, course_key)
return group_id
else:
# Never pass a group_id to the comments service for a non-divided
......@@ -827,6 +826,15 @@ def get_group_id_for_comments_service(request, course_key, commentable_id=None):
return None
@request_cached
def get_group_id_for_user_from_cache(user, course_id):
"""
Caches the results of get_group_id_for_user, but serializes the course_id
instead of the course_discussions_settings object as cache keys.
"""
return get_group_id_for_user(user, get_course_discussion_settings(course_id))
def get_group_id_for_user(user, course_discussion_settings):
"""
Given a user, return the group_id for that user according to the course_discussion_settings.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment