from __future__ import absolute_import

from rest_framework.response import Response

from sentry.api.bases.organization import OrganizationEndpoint
from sentry.api.permissions import assert_perm
from sentry.api.serializers import serialize
from sentry.models import Project, Team


class OrganizationProjectsEndpoint(OrganizationEndpoint):
    def get(self, request, organization):
        assert_perm(organization, request.user, request.auth)

        team_list = Team.objects.get_for_user(
            organization=organization,
            user=request.user,
        )

        project_list = []
        for team in team_list:
            project_list.extend(Project.objects.get_for_user(
                team=team,
                user=request.user,
            ))
        project_list.sort(key=lambda x: x.name)

        team_map = dict(
            (t.id, c) for (t, c) in zip(team_list, serialize(team_list, request.user)),
        )

        context = []
        for project, pdata in zip(project_list, serialize(project_list, request.user)):
            pdata['team'] = team_map[project.team_id]
            context.append(pdata)

        return Response(context)

@@ -79,16 +79,55 @@
 sponse%0A%0A
+from sentry.api.base import DocSection%0A
 from sen
@@ -181,16 +181,16 @@
 ndpoint%0A
-
 from sen
@@ -382,44 +382,242 @@
    d
-ef get(self, request, organization):
+oc_section = DocSection.ORGANIZATIONS%0A%0A    def get(self, request, organization):%0A        %22%22%22%0A        List an organization's projects%0A%0A        Return a list of projects bound to a organization.%0A%0A            %7Bmethod%7D %7Bpath%7D%0A%0A        %22%22%22
 %0A   

Add organization projects to docs