views.py 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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"), users=self.request.user,
  41. )
  42. except Organization.DoesNotExist:
  43. raise exceptions.ValidationError("Organization does not exist")
  44. def perform_create(self, serializer):
  45. organization = self.get_organization()
  46. try:
  47. project = Project.objects.get(
  48. slug=self.kwargs.get("project_slug"), organization=organization,
  49. )
  50. except Project.DoesNotExist:
  51. raise exceptions.ValidationError("Project does not exist")
  52. release = serializer.save(organization=organization)
  53. release.projects.add(project)
  54. @action(detail=True, methods=["post"])
  55. def assemble(self, request, organization_slug: str, version: str):
  56. organization = self.get_organization()
  57. release = self.get_object()
  58. serializer = AssembleSerializer(data=request.data)
  59. serializer.is_valid(raise_exception=True)
  60. checksum = serializer.validated_data.get("checksum", None)
  61. chunks = serializer.validated_data.get("chunks", [])
  62. assemble_artifacts_task.delay(
  63. org_id=organization.id, version=version, checksum=checksum, chunks=chunks,
  64. )
  65. # TODO should return more state's
  66. return Response({"state": "ok", "missingChunks": []})
  67. class ReleaseFileViewSet(
  68. mixins.CreateModelMixin,
  69. mixins.ListModelMixin,
  70. mixins.DestroyModelMixin,
  71. mixins.RetrieveModelMixin,
  72. viewsets.GenericViewSet,
  73. ):
  74. queryset = ReleaseFile.objects.all()
  75. serializer_class = ReleaseFileSerializer
  76. permission_classes = [ReleaseFilePermission]
  77. def get_queryset(self):
  78. queryset = self.queryset
  79. if not self.request.user.is_authenticated:
  80. return queryset.none()
  81. queryset = queryset.filter(
  82. release__organization__users=self.request.user,
  83. release__organization__slug=self.kwargs.get("organization_slug"),
  84. release__projects__slug=self.kwargs.get("project_slug"),
  85. release__version=self.kwargs.get("release_version"),
  86. )
  87. queryset = queryset.select_related("file")
  88. return queryset
  89. def perform_create(self, serializer):
  90. try:
  91. release = Release.objects.get(
  92. version=self.kwargs.get("release_version"),
  93. organization__slug=self.kwargs.get("organization_slug"),
  94. )
  95. except Release.DoesNotExist:
  96. raise exceptions.ValidationError("Release does not exist")
  97. serializer.save(release=release)