Commit f4beb8b8 by Don Mitchell

Reduce sql queries for groupname tests.

Conflicts:
	cms/djangoapps/auth/authz.py
parent 17b5c212
...@@ -63,7 +63,7 @@ def get_all_course_role_groupnames(location, role, use_filter=True): ...@@ -63,7 +63,7 @@ def get_all_course_role_groupnames(location, role, use_filter=True):
# filter to the ones which exist # filter to the ones which exist
default = groupnames[0] default = groupnames[0]
if use_filter: if use_filter:
groupnames = [group for group in groupnames if Group.objects.filter(name=group).exists()] groupnames = [group.name for group in Group.objects.filter(name__in=groupnames)]
return groupnames, default return groupnames, default
...@@ -203,11 +203,8 @@ def remove_user_from_course_group(caller, user, location, role): ...@@ -203,11 +203,8 @@ def remove_user_from_course_group(caller, user, location, role):
# see if the user is actually in that role, if not then we don't have to do anything # see if the user is actually in that role, if not then we don't have to do anything
groupnames, _ = get_all_course_role_groupnames(location, role) groupnames, _ = get_all_course_role_groupnames(location, role)
for groupname in groupnames: for group in user.groups.filter(name__in=groupnames):
groups = user.groups.filter(name=groupname) user.groups.remove(group)
if groups:
# will only be one with that name
user.groups.remove(groups[0])
user.save() user.save()
...@@ -243,7 +240,7 @@ def is_user_in_course_group_role(user, location, role, check_staff=True): ...@@ -243,7 +240,7 @@ def is_user_in_course_group_role(user, location, role, check_staff=True):
if check_staff and user.is_staff: if check_staff and user.is_staff:
return True return True
groupnames, _ = get_all_course_role_groupnames(location, role) groupnames, _ = get_all_course_role_groupnames(location, role)
return any(user.groups.filter(name=groupname).exists() for groupname in groupnames) return user.groups.filter(name__in=groupnames).exists()
return False return False
...@@ -266,7 +263,7 @@ def is_user_in_creator_group(user): ...@@ -266,7 +263,7 @@ def is_user_in_creator_group(user):
# Feature flag for using the creator group setting. Will be removed once the feature is complete. # Feature flag for using the creator group setting. Will be removed once the feature is complete.
if settings.FEATURES.get('ENABLE_CREATOR_GROUP', False): if settings.FEATURES.get('ENABLE_CREATOR_GROUP', False):
return user.groups.filter(name=COURSE_CREATOR_GROUP_NAME).count() > 0 return user.groups.filter(name=COURSE_CREATOR_GROUP_NAME).exists()
return True return True
......
...@@ -4,7 +4,6 @@ adding users, removing users, and listing members ...@@ -4,7 +4,6 @@ adding users, removing users, and listing members
""" """
from abc import ABCMeta, abstractmethod from abc import ABCMeta, abstractmethod
from functools import partial
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment