From 1b565672689e8a4d5d2b2e95738480b900c05655 Mon Sep 17 00:00:00 2001 From: Joseph Schorr Date: Tue, 7 Apr 2015 13:45:49 -0400 Subject: [PATCH] Make sure also include teams from organizations that the user admins --- data/model/legacy.py | 19 +++++++++++++++++++ endpoints/api/search.py | 27 ++++++++++++++++++++++++++- 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/data/model/legacy.py b/data/model/legacy.py index 1cce22bfc..8e4ab9ee7 100644 --- a/data/model/legacy.py +++ b/data/model/legacy.py @@ -696,7 +696,26 @@ def get_matching_user_teams(team_prefix, user): .switch(Team) .join(TeamMember) .where(TeamMember.user == user, Team.name ** (team_prefix + '%')) + .distinct(Team.id) .limit(10)) + + return query + + +def get_matching_admined_teams(team_prefix, user): + admined_orgs = (get_user_organizations(user.username) + .switch(Team) + .join(TeamRole) + .where(TeamRole.name == 'admin')) + + query = (Team.select() + .join(User) + .switch(Team) + .join(TeamMember) + .where(Team.name ** (team_prefix + '%'), Team.organization << (admined_orgs)) + .distinct(Team.id) + .limit(10)) + return query diff --git a/endpoints/api/search.py b/endpoints/api/search.py index 4e6288875..995a8e414 100644 --- a/endpoints/api/search.py +++ b/endpoints/api/search.py @@ -142,9 +142,16 @@ class ConductSearch(ApiResource): if get_authenticated_user(): username = get_authenticated_user().username - # Find the matching teams. + # Find the matching teams where the user is a member. + encountered_teams = set() + matching_teams = model.get_matching_user_teams(prefix, get_authenticated_user()) for team in matching_teams: + if team.id in encountered_teams: + continue + + encountered_teams.add(team.id) + results.append({ 'kind': 'team', 'name': team.name, @@ -154,6 +161,24 @@ class ConductSearch(ApiResource): 'href': '/organization/' + team.organization.username + '/teams/' + team.name }) + # Find matching teams in orgs admined by the user. + matching_teams = model.get_matching_admined_teams(prefix, get_authenticated_user()) + for team in matching_teams: + if team.id in encountered_teams: + continue + + encountered_teams.add(team.id) + + results.append({ + 'kind': 'team', + 'name': team.name, + 'organization': entity_view(team.organization), + 'avatar': avatar.get_data_for_team(team), + 'score': 2, + 'href': '/organization/' + team.organization.username + '/teams/' + team.name + }) + + # Find the matching repositories. matching_repos = model.get_matching_repositories(prefix, username) matching_repo_counts = {t[0]: t[1] for t in model.get_repository_pull_counts(matching_repos)}