Make sure also include teams from organizations that the user admins

This commit is contained in:
Joseph Schorr 2015-04-07 13:45:49 -04:00
parent a34d56045f
commit 1b56567268
2 changed files with 45 additions and 1 deletions

View file

@ -696,7 +696,26 @@ def get_matching_user_teams(team_prefix, user):
.switch(Team) .switch(Team)
.join(TeamMember) .join(TeamMember)
.where(TeamMember.user == user, Team.name ** (team_prefix + '%')) .where(TeamMember.user == user, Team.name ** (team_prefix + '%'))
.distinct(Team.id)
.limit(10)) .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 return query

View file

@ -142,9 +142,16 @@ class ConductSearch(ApiResource):
if get_authenticated_user(): if get_authenticated_user():
username = get_authenticated_user().username 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()) matching_teams = model.get_matching_user_teams(prefix, get_authenticated_user())
for team in matching_teams: for team in matching_teams:
if team.id in encountered_teams:
continue
encountered_teams.add(team.id)
results.append({ results.append({
'kind': 'team', 'kind': 'team',
'name': team.name, 'name': team.name,
@ -154,6 +161,24 @@ class ConductSearch(ApiResource):
'href': '/organization/' + team.organization.username + '/teams/' + team.name '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. # Find the matching repositories.
matching_repos = model.get_matching_repositories(prefix, username) 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)} matching_repo_counts = {t[0]: t[1] for t in model.get_repository_pull_counts(matching_repos)}