views.py 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 files.tasks import assemble_artifacts_task
  7. from .models import Release, ReleaseFile
  8. from .serializers import (
  9. ReleaseSerializer,
  10. ReleaseUpdateSerializer,
  11. ReleaseFileSerializer,
  12. AssembleSerializer,
  13. )
  14. from .permissions import ReleasePermission, ReleaseFilePermission
  15. class ReleaseViewSet(viewsets.ModelViewSet):
  16. queryset = Release.objects.all()
  17. serializer_class = ReleaseSerializer
  18. permission_classes = [ReleasePermission]
  19. lookup_field = "version"
  20. lookup_value_regex = "[^/]+"
  21. def get_serializer_class(self):
  22. serializer_class = self.serializer_class
  23. if self.request.method == "PUT":
  24. serializer_class = ReleaseUpdateSerializer
  25. return serializer_class
  26. def get_queryset(self):
  27. if not self.request.user.is_authenticated:
  28. return self.queryset.none()
  29. queryset = self.queryset.filter(organization__users=self.request.user)
  30. organization_slug = self.kwargs.get("organization_slug")
  31. if organization_slug:
  32. queryset = queryset.filter(organization__slug=organization_slug)
  33. project_slug = self.kwargs.get("project_slug")
  34. if project_slug:
  35. queryset = queryset.filter(projects__slug=project_slug)
  36. return queryset
  37. def get_organization(self):
  38. try:
  39. return Organization.objects.get(
  40. slug=self.kwargs.get("organization_slug"),
  41. users=self.request.user,
  42. )
  43. except Organization.DoesNotExist:
  44. raise exceptions.ValidationError("Organization does not exist")
  45. def perform_create(self, serializer):
  46. organization = self.get_organization()
  47. try:
  48. project = Project.objects.get(
  49. slug=self.kwargs.get("project_slug"),
  50. organization=organization,
  51. )
  52. except Project.DoesNotExist:
  53. raise exceptions.ValidationError("Project does not exist")
  54. release = serializer.save(organization=organization)
  55. release.projects.add(project)
  56. @action(detail=True, methods=["post"])
  57. def assemble(self, request, organization_slug: str, version: str):
  58. organization = self.get_organization()
  59. release = self.get_object()
  60. serializer = AssembleSerializer(data=request.data)
  61. serializer.is_valid(raise_exception=True)
  62. checksum = serializer.validated_data.get("checksum", None)
  63. chunks = serializer.validated_data.get("chunks", [])
  64. assemble_artifacts_task.delay(
  65. org_id=organization.id,
  66. version=version,
  67. checksum=checksum,
  68. chunks=chunks,
  69. )
  70. # TODO should return more state's
  71. return Response({"state": "ok", "missingChunks": []})
  72. class ReleaseFileViewSet(
  73. mixins.CreateModelMixin,
  74. mixins.ListModelMixin,
  75. mixins.DestroyModelMixin,
  76. mixins.RetrieveModelMixin,
  77. viewsets.GenericViewSet,
  78. ):
  79. queryset = ReleaseFile.objects.all()
  80. serializer_class = ReleaseFileSerializer
  81. permission_classes = [ReleaseFilePermission]
  82. def get_queryset(self):
  83. queryset = self.queryset
  84. if not self.request.user.is_authenticated:
  85. return queryset.none()
  86. queryset = queryset.filter(
  87. release__organization__users=self.request.user,
  88. release__organization__slug=self.kwargs.get("organization_slug"),
  89. release__version=self.kwargs.get("release_version"),
  90. )
  91. if self.kwargs.get("project_slug"):
  92. queryset = queryset.filter(
  93. release__projects__slug=self.kwargs.get("project_slug")
  94. )
  95. queryset = queryset.select_related("file")
  96. return queryset
  97. def perform_create(self, serializer):
  98. try:
  99. release = Release.objects.get(
  100. version=self.kwargs.get("release_version"),
  101. organization__slug=self.kwargs.get("organization_slug"),
  102. )
  103. except Release.DoesNotExist:
  104. raise exceptions.ValidationError("Release does not exist")
  105. serializer.save(release=release)