parent
b5efc57655
commit
391d70d9ec
18 changed files with 496 additions and 224 deletions
|
@ -191,29 +191,47 @@ def get_teams_within_org(organization):
|
|||
""" Returns a AttrDict of team info (id, name, description), its role under the org,
|
||||
the number of repositories on which it has permission, and the number of members.
|
||||
"""
|
||||
query = (Team.select(Team.id, Team.name, Team.description, TeamRole.name,
|
||||
fn.Count(RepositoryPermission.id), fn.Count(TeamMember.id))
|
||||
query = (Team.select()
|
||||
.where(Team.organization == organization)
|
||||
.join(TeamRole)
|
||||
.switch(Team)
|
||||
.join(RepositoryPermission, JOIN_LEFT_OUTER)
|
||||
.switch(Team)
|
||||
.join(TeamMember, JOIN_LEFT_OUTER)
|
||||
.group_by(Team.id)
|
||||
.tuples())
|
||||
.join(TeamRole))
|
||||
|
||||
def _team_view(team_tuple):
|
||||
return AttrDict({
|
||||
'id': team_tuple[0],
|
||||
'name': team_tuple[1],
|
||||
'description': team_tuple[2],
|
||||
'role_name': team_tuple[3],
|
||||
def _team_view(team):
|
||||
return {
|
||||
'id': team.id,
|
||||
'name': team.name,
|
||||
'description': team.description,
|
||||
'role_name': team.role.name,
|
||||
|
||||
'repo_count': team_tuple[4],
|
||||
'member_count': team_tuple[5],
|
||||
})
|
||||
'repo_count': 0,
|
||||
'member_count': 0,
|
||||
}
|
||||
|
||||
return [_team_view(team_tuple) for team_tuple in query]
|
||||
teams = {team.id: _team_view(team) for team in query}
|
||||
if not teams:
|
||||
# Just in case. Should ideally never happen.
|
||||
return []
|
||||
|
||||
# Add repository permissions count.
|
||||
permission_tuples = (RepositoryPermission.select(RepositoryPermission.team,
|
||||
fn.Count(RepositoryPermission.id))
|
||||
.where(RepositoryPermission.team << teams.keys())
|
||||
.group_by(RepositoryPermission.team)
|
||||
.tuples())
|
||||
|
||||
for perm_tuple in permission_tuples:
|
||||
teams[perm_tuple[0]]['repo_count'] = perm_tuple[1]
|
||||
|
||||
# Add the member count.
|
||||
members_tuples = (TeamMember.select(TeamMember.team,
|
||||
fn.Count(TeamMember.id))
|
||||
.where(TeamMember.team << teams.keys())
|
||||
.group_by(TeamMember.team)
|
||||
.tuples())
|
||||
|
||||
for member_tuple in members_tuples:
|
||||
teams[member_tuple[0]]['member_count'] = member_tuple[1]
|
||||
|
||||
return [AttrDict(team_info) for team_info in teams.values()]
|
||||
|
||||
|
||||
def get_user_teams_within_org(username, organization):
|
||||
|
|
Reference in a new issue