Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): Add option to fetch Organization details without Projects and Teams #13925

Merged
merged 6 commits into from
Jul 23, 2019
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/sentry/api/endpoints/organization_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -317,10 +317,12 @@ def get(self, request, organization):
team should be created for.
:auth: required
"""
is_light = request.GET.get('light') == '1'
serializer = org_serializers.LightDetailedOrganizationSerializer if is_light else org_serializers.DetailedOrganizationSerializer
context = serialize(
organization,
request.user,
org_serializers.DetailedOrganizationSerializer(),
serializer(),
access=request.access,
)
return self.respond(context)
Expand Down
85 changes: 49 additions & 36 deletions src/sentry/api/serializers/models/organization.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,43 +116,13 @@ def serialize(self, obj, attrs, user):
}


class DetailedOrganizationSerializer(OrganizationSerializer):
# Does not include project/teams list
billyvg marked this conversation as resolved.
Show resolved Hide resolved
class LightDetailedOrganizationSerializer(OrganizationSerializer):
billyvg marked this conversation as resolved.
Show resolved Hide resolved
def get_attrs(self, item_list, user, **kwargs):
return super(DetailedOrganizationSerializer, self).get_attrs(item_list, user)

def _project_list(self, organization, access):
member_projects = list(access.projects)
member_project_ids = [p.id for p in member_projects]
other_projects = list(Project.objects.filter(
organization=organization,
status=ProjectStatus.VISIBLE,
).exclude(id__in=member_project_ids))
project_list = sorted(other_projects + member_projects, key=lambda x: x.slug)

for project in project_list:
project._organization_cache = organization
return project_list

def _team_list(self, organization, access):
member_teams = list(access.teams)
member_team_ids = [p.id for p in member_teams]
other_teams = list(Team.objects.filter(
organization=organization,
status=TeamStatus.VISIBLE,
).exclude(id__in=member_team_ids))
team_list = sorted(other_teams + member_teams, key=lambda x: x.slug)

for team in team_list:
team._organization_cache = organization
return team_list
return super(LightDetailedOrganizationSerializer, self).get_attrs(item_list, user)

def serialize(self, obj, attrs, user, access):
from sentry import experiments
from sentry.api.serializers.models.project import ProjectSummarySerializer
from sentry.api.serializers.models.team import TeamSerializer

team_list = self._team_list(obj, access)
project_list = self._project_list(obj, access)

onboarding_tasks = list(
OrganizationOnboardingTask.objects.filter(
Expand All @@ -162,7 +132,7 @@ def serialize(self, obj, attrs, user, access):

experiment_assignments = experiments.all(org=obj, actor=user)

context = super(DetailedOrganizationSerializer, self).serialize(obj, attrs, user)
context = super(LightDetailedOrganizationSerializer, self).serialize(obj, attrs, user)
max_rate = quotas.get_maximum_quota(obj)
context['experiments'] = experiment_assignments
context['quota'] = {
Expand Down Expand Up @@ -205,11 +175,54 @@ def serialize(self, obj, attrs, user, access):
'scrapeJavaScript': bool(obj.get_option('sentry:scrape_javascript', SCRAPE_JAVASCRIPT_DEFAULT)),
'trustedRelays': obj.get_option('sentry:trusted-relays', TRUSTED_RELAYS_DEFAULT) or [],
})
context['teams'] = serialize(team_list, user, TeamSerializer())
context['projects'] = serialize(project_list, user, ProjectSummarySerializer())
context['access'] = access.scopes
context['pendingAccessRequests'] = OrganizationAccessRequest.objects.filter(
team__organization=obj,
).count()
context['onboardingTasks'] = serialize(onboarding_tasks, user, OnboardingTasksSerializer())
return context


class DetailedOrganizationSerializer(LightDetailedOrganizationSerializer):
def get_attrs(self, item_list, user, **kwargs):
return super(DetailedOrganizationSerializer, self).get_attrs(item_list, user)

def _project_list(self, organization, access):
member_projects = list(access.projects)
member_project_ids = [p.id for p in member_projects]
other_projects = list(Project.objects.filter(
organization=organization,
status=ProjectStatus.VISIBLE,
).exclude(id__in=member_project_ids))
project_list = sorted(other_projects + member_projects, key=lambda x: x.slug)

for project in project_list:
project._organization_cache = organization
return project_list

def _team_list(self, organization, access):
member_teams = list(access.teams)
member_team_ids = [p.id for p in member_teams]
other_teams = list(Team.objects.filter(
organization=organization,
status=TeamStatus.VISIBLE,
).exclude(id__in=member_team_ids))
team_list = sorted(other_teams + member_teams, key=lambda x: x.slug)

for team in team_list:
team._organization_cache = organization
return team_list

def serialize(self, obj, attrs, user, access):
from sentry.api.serializers.models.project import ProjectSummarySerializer
from sentry.api.serializers.models.team import TeamSerializer

context = super(DetailedOrganizationSerializer, self).serialize(obj, attrs, user, access)

team_list = self._team_list(obj, access)
project_list = self._project_list(obj, access)

context['teams'] = serialize(team_list, user, TeamSerializer())
context['projects'] = serialize(project_list, user, ProjectSummarySerializer())

return context
25 changes: 25 additions & 0 deletions tests/sentry/api/endpoints/test_organization_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,31 @@ def test_with_projects(self):
assert len(team_slugs) == 2
assert 'deleted' not in team_slugs

def test_light_details_no_projects_or_teams(self):
user = self.create_user('owner@example.org')
org = self.create_organization(owner=user)
team = self.create_team(
name='appy',
organization=org,
members=[user])
# Create non-member team to test response shape
self.create_team(name='no-member', organization=org)

for i in range(2):
self.create_project(organization=org, teams=[team])

url = reverse(
'sentry-api-0-organization-details', kwargs={
'organization_slug': org.slug,
}
)
self.login_as(user=user)

response = self.client.get(u'{}?light=1'.format(url), format='json')

assert 'projects' not in response.data
assert 'teams' not in response.data

def test_as_superuser(self):
self.user = self.create_user('super@example.org', is_superuser=True)
org = self.create_organization(owner=self.user)
Expand Down