views.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. from rest_framework import viewsets, exceptions, mixins
  2. from rest_framework.response import Response
  3. from rest_framework.decorators import action
  4. from organizations_ext.models import Organization
  5. from projects.models import Project
  6. from .models import Release, ReleaseFile
  7. from .serializers import (
  8. ReleaseSerializer,
  9. ReleaseUpdateSerializer,
  10. ReleaseFileSerializer,
  11. AssembleSerializer,
  12. )
  13. from .permissions import ReleasePermission, ReleaseFilePermission
  14. class ReleaseViewSet(viewsets.ModelViewSet):
  15. queryset = Release.objects.all()
  16. serializer_class = ReleaseSerializer
  17. permission_classes = [ReleasePermission]
  18. lookup_field = "version"
  19. lookup_value_regex = "[^/]+"
  20. def get_serializer_class(self):
  21. serializer_class = self.serializer_class
  22. if self.request.method == "PUT":
  23. serializer_class = ReleaseUpdateSerializer
  24. return serializer_class
  25. def get_queryset(self):
  26. if not self.request.user.is_authenticated:
  27. return self.queryset.none()
  28. queryset = self.queryset.filter(organization__users=self.request.user)
  29. organization_slug = self.kwargs.get("organization_slug")
  30. if organization_slug:
  31. queryset = queryset.filter(organization__slug=organization_slug)
  32. return queryset
  33. def perform_create(self, serializer):
  34. try:
  35. organization = Organization.objects.get(
  36. slug=self.kwargs.get("organization_slug"), users=self.request.user,
  37. )
  38. except Organization.DoesNotExist:
  39. raise exceptions.ValidationError("Organization does not exist")
  40. try:
  41. project = Project.objects.get(
  42. slug=self.kwargs.get("project_slug"), organization=organization,
  43. )
  44. except Project.DoesNotExist:
  45. raise exceptions.ValidationError("Project does not exist")
  46. release = serializer.save(organization=organization)
  47. release.projects.add(project)
  48. @action(detail=True, methods=["post"])
  49. def assemble(self, request, organization_slug: str, version: str):
  50. release = self.get_object()
  51. serializer = AssembleSerializer(data=request.data)
  52. serializer.is_valid(raise_exception=True)
  53. return Response({"state": "ok", "missingChunks": []})
  54. class ReleaseFileViewSet(
  55. mixins.CreateModelMixin,
  56. mixins.ListModelMixin,
  57. mixins.DestroyModelMixin,
  58. mixins.RetrieveModelMixin,
  59. viewsets.GenericViewSet,
  60. ):
  61. queryset = ReleaseFile.objects.all()
  62. serializer_class = ReleaseFileSerializer
  63. permission_classes = [ReleaseFilePermission]
  64. def get_queryset(self):
  65. queryset = self.queryset
  66. if not self.request.user.is_authenticated:
  67. return queryset.none()
  68. queryset = queryset.filter(
  69. release__organization__users=self.request.user,
  70. release__organization__slug=self.kwargs.get("organization_slug"),
  71. release__projects__slug=self.kwargs.get("project_slug"),
  72. release__version=self.kwargs.get("release_version"),
  73. )
  74. queryset = queryset.select_related("file")
  75. return queryset
  76. def perform_create(self, serializer):
  77. try:
  78. release = Release.objects.get(
  79. version=self.kwargs.get("release_version"),
  80. organization__slug=self.kwargs.get("organization_slug"),
  81. )
  82. except Release.DoesNotExist:
  83. raise exceptions.ValidationError("Release does not exist")
  84. serializer.save(release=release)