views.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. from rest_framework import viewsets, exceptions, status
  2. from rest_framework.decorators import action
  3. from rest_framework.response import Response
  4. from django.shortcuts import get_object_or_404
  5. from teams.models import Team
  6. from teams.views import NestedTeamViewSet
  7. from organizations_ext.models import Organization, OrganizationUserRole
  8. from .models import Project, ProjectKey
  9. from .serializers.serializers import ProjectSerializer, ProjectKeySerializer
  10. from .permissions import ProjectPermission, ProjectKeyPermission
  11. class NestedProjectViewSet(viewsets.ModelViewSet):
  12. """
  13. Detail view is under /api/0/projects/{organization_slug}/{project_slug}/
  14. Project keys/DSN's are available at /api/0/projects/{organization_slug}/{project_slug}/keys/
  15. """
  16. queryset = Project.objects.all()
  17. serializer_class = ProjectSerializer
  18. lookup_field = "slug"
  19. permission_classes = [ProjectPermission]
  20. def get_object(self):
  21. queryset = self.filter_queryset(self.get_queryset())
  22. slug = self.kwargs.get("project_slug", self.kwargs.get("slug"))
  23. obj = get_object_or_404(
  24. queryset, slug=slug, organization__slug=self.kwargs["organization_slug"],
  25. )
  26. self.check_object_permissions(self.request, obj)
  27. return obj
  28. def get_queryset(self):
  29. if self.request.user.is_authenticated:
  30. queryset = self.queryset.filter(organization__users=self.request.user)
  31. organization_slug = self.kwargs.get("organization_slug")
  32. if organization_slug:
  33. queryset = queryset.filter(organization__slug=organization_slug)
  34. team_slug = self.kwargs.get("team_slug")
  35. if team_slug:
  36. queryset = queryset.filter(team__slug=team_slug)
  37. return queryset
  38. return self.queryset.none()
  39. def perform_create(self, serializer):
  40. team = None
  41. if self.kwargs.get("team_slug"):
  42. try:
  43. team = Team.objects.get(
  44. slug=self.kwargs.get("team_slug"),
  45. organization__slug=self.kwargs.get("organization_slug"),
  46. organization__users=self.request.user,
  47. organization__organization_users__role__gte=OrganizationUserRole.ADMIN,
  48. )
  49. except Team.DoesNotExist:
  50. raise exceptions.ValidationError("Team not found")
  51. try:
  52. organization = Organization.objects.get(
  53. slug=self.kwargs.get("organization_slug"),
  54. users=self.request.user,
  55. organization_users__role__gte=OrganizationUserRole.ADMIN,
  56. )
  57. except Organization.DoesNotExist:
  58. raise exceptions.ValidationError("Organization not found")
  59. new_project = serializer.save(organization=organization)
  60. if new_project and team:
  61. new_project.team_set.add(team)
  62. class ProjectViewSet(NestedProjectViewSet):
  63. lookup_field = "pk"
  64. lookup_value_regex = r"(?P<organization_slug>[^/.]+)/(?P<project_slug>[-\w]+)"
  65. def create(self, request, *args, **kwargs):
  66. raise exceptions.MethodNotAllowed(
  67. request.method,
  68. "Create project not allowed. Use team-projects view instead.",
  69. )
  70. class ProjectKeyViewSet(viewsets.ModelViewSet):
  71. queryset = ProjectKey.objects.all()
  72. serializer_class = ProjectKeySerializer
  73. lookup_field = "public_key"
  74. permission_classes = [ProjectKeyPermission]
  75. def get_queryset(self):
  76. if not self.request.user.is_authenticated:
  77. return self.queryset.none()
  78. return (
  79. super()
  80. .get_queryset()
  81. .filter(
  82. project__slug=self.kwargs["project_slug"],
  83. project__organization__slug=self.kwargs["organization_slug"],
  84. project__organization__users=self.request.user,
  85. )
  86. )
  87. def perform_create(self, serializer):
  88. project = get_object_or_404(
  89. Project,
  90. slug=self.kwargs.get("project_slug"),
  91. organization__slug=self.kwargs["organization_slug"],
  92. organization__users=self.request.user,
  93. )
  94. serializer.save(project=project)
  95. class ProjectTeamViewSet(NestedTeamViewSet):
  96. @action(
  97. methods=["post", "delete"], detail=False, url_path=("(?P<team_slug>[-\w]+)")
  98. )
  99. def add_remove_project(
  100. self,
  101. request,
  102. project_pk=None,
  103. project_slug=None,
  104. organization_slug=None,
  105. team_slug=None,
  106. ):
  107. """ Add/remove team to a project """
  108. team = get_object_or_404(self.get_queryset(), slug=team_slug)
  109. project = get_object_or_404(
  110. Project,
  111. slug=project_slug,
  112. organization__slug=organization_slug,
  113. organization__users=self.request.user,
  114. organization__organization_users__role__gte=OrganizationUserRole.MANAGER,
  115. )
  116. serializer = ProjectSerializer(instance=project, context={"request": request})
  117. if request.method == "POST":
  118. project.team_set.add(team)
  119. return Response(serializer.data, status=status.HTTP_201_CREATED)
  120. project.team_set.remove(team)
  121. return Response(serializer.data)