def get_team(team_id):
    team = team_service.get_team(team_id)
    if not team:
        raise NotFoundError('Team {} does not exist'.format(team_id))

    team_member = team_member_service.find(
        is_team_lead=True,
        team_id=team_id,
        user_id=current_user.id
    ).one_or_none()

    if not team_member:
        raise UnauthorisedError('Only team leads can edit a team')

    domain = get_email_domain(current_user.email_address)

    if team['teamMembers'] is not None:
        for user_id, team_member in team['teamMembers'].iteritems():
            missing_permissions = [permission for permission in permission_types
                                   if permission not in team_member['permissions']]

            for permission in missing_permissions:
                team_member['permissions'][permission] = False

    team.update(domain=domain)

    return team
    def test_get_team_returns_basic_details(self, team):
        team = team_service.get_team(1)

        assert team['emailAddress'] == '*****@*****.**'
        assert team['id'] == 1
        assert team['name'] == 'Marketplace'
        assert team['status'] == 'completed'
    def test_get_team_returns_team_members(self, users, team, team_members):
        team = team_service.get_team(1)

        assert len(team['teamMembers'].keys()) == 1
        assert '2' in team['teamMembers']
        team_member = team['teamMembers']['2']
        assert team_member['emailAddress'] == '*****@*****.**'
        assert team_member['name'] == 'Moo Moo'
    def test_get_team_returns_team_leads(self, users, team, team_members):
        team = team_service.get_team(1)

        assert len(team['teamLeads'].keys()) == 1
        assert '1' in team['teamLeads']
        team_lead = team['teamLeads']['1']
        assert team_lead['emailAddress'] == '*****@*****.**'
        assert team_lead['name'] == 'Muu Muu'
def request_to_join(user_email_address, team_id, agency_id):
    team = team_service.get_team(team_id)
    if not team:
        raise NotFoundError('Team {} does not exist'.format(team_id))
    if not get_teams_by_agency(agency_id, team_id):
        raise UnauthorisedError('Can not request to join this team')

    claim = user_claims_service.make_claim(type='join_team',
                                           email_address=user_email_address,
                                           data={
                                               "team_id": team_id,
                                               "team_name": team['name'],
                                               "agency_id": agency_id,
                                               "user_id": current_user.id,
                                               "user_name": current_user.name,
                                               "approved": False
                                           })
    if not claim:
        raise ValidationError('Could not create join team request')

    send_request_to_join_team_leaders_email(team_id, claim.token)
    send_request_to_join_requester_email(user_email_address, team_id)
def get_team(team_id, allow_anyone=None):
    team = team_service.get_team(team_id)
    if not team:
        raise NotFoundError('Team {} does not exist'.format(team_id))

    if allow_anyone is None:
        team_member = team_member_service.find(
            is_team_lead=True, team_id=team_id,
            user_id=current_user.id).one_or_none()

        if not team_member:
            raise UnauthorisedError('Only team leads can edit a team')

    if team['teamMembers'] is not None:
        for user_id, team_member in team['teamMembers'].iteritems():
            missing_permissions = [
                permission for permission in permission_types
                if permission not in team_member['permissions']
            ]

            for permission in missing_permissions:
                team_member['permissions'][permission] = False

    return team