Optimize lookup of org membership on prototype and perms APIs

Fixes a major slowdown when working with permissions under organizations with a lot of members

Fixes https://www.pivotaltracker.com/story/show/144076113
This commit is contained in:
Joseph Schorr 2017-05-08 13:31:26 -04:00
parent f5e4380a57
commit db767b3610
3 changed files with 37 additions and 17 deletions

View file

@ -106,15 +106,28 @@ def remove_organization_member(org, user_obj):
TeamMember.delete().where(TeamMember.id << members).execute()
def get_organization_member_set(orgname):
def get_organization_member_set(org, include_robots=False, users_filter=None):
""" Returns the set of all member usernames under the given organization, with optional
filtering by robots and/or by a specific set of User objects.
"""
Org = User.alias()
org_users = (User
.select(User.username)
.join(TeamMember)
.join(Team)
.join(Org, on=(Org.id == Team.organization))
.where(Org.username == orgname)
.where(Team.organization == org)
.distinct())
if not include_robots:
org_users = org_users.where(User.robot == False)
if users_filter is not None:
ids_list = [u.id for u in users_filter if u is not None]
if not ids_list:
return set()
org_users = org_users.where(User.id << ids_list)
return {user.username for user in org_users}