From 68c182c055861ee53efc7a559f724ae547830aae Mon Sep 17 00:00:00 2001 From: Calen Pennington Date: Fri, 1 Nov 2013 14:58:27 -0400 Subject: [PATCH] Optimize and cache the query to find the list of groups a user is in --- lms/djangoapps/courseware/access.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/lms/djangoapps/courseware/access.py b/lms/djangoapps/courseware/access.py index 7836ab8bbc..7126c6f881 100644 --- a/lms/djangoapps/courseware/access.py +++ b/lms/djangoapps/courseware/access.py @@ -503,6 +503,14 @@ def _has_global_staff_access(user): return False +def group_names(user): + """Return the list of names of the groups that a user is in""" + if not hasattr(user, '_groups'): + user._groups = user.groups.values_list('name', flat=True) + + return user._groups + + def _adjust_start_date_for_beta_testers(user, descriptor): """ If user is in a beta test group, adjust the start date by the appropriate number of @@ -530,10 +538,8 @@ def _adjust_start_date_for_beta_testers(user, descriptor): # bail early if no beta testing is set up return descriptor.start - user_groups = [g.name for g in user.groups.all()] - beta_group = course_beta_test_group_name(descriptor.location) - if beta_group in user_groups: + if beta_group in group_names(user): debug("Adjust start time: user in group %s", beta_group) delta = timedelta(descriptor.days_early_for_beta) effective = descriptor.start - delta @@ -577,7 +583,7 @@ def _has_access_to_location(user, location, access_level, course_context): return True # If not global staff, is the user in the Auth group for this class? - user_groups = [g.name for g in user.groups.all()] + user_groups = group_names(user) if access_level == 'staff': staff_groups = group_names_for_staff(location, course_context) + \