serializers.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from django.db import IntegrityError
  2. from rest_framework import serializers
  3. from projects.serializers.base_serializers import ProjectReferenceSerializer
  4. from files.models import File
  5. from glitchtip.exceptions import ConflictException
  6. from .models import Release, ReleaseFile
  7. class ReleaseSerializer(serializers.ModelSerializer):
  8. dateCreated = serializers.DateTimeField(source="created", read_only=True)
  9. dateReleased = serializers.DateTimeField(source="released", required=False)
  10. shortVersion = serializers.CharField(source="version", read_only=True)
  11. deployCount = serializers.IntegerField(source="deploy_count", read_only=True)
  12. projects = ProjectReferenceSerializer(many=True, read_only=True)
  13. class Meta:
  14. model = Release
  15. fields = (
  16. "url",
  17. "data",
  18. "deployCount",
  19. "dateCreated",
  20. "dateReleased",
  21. "version",
  22. "shortVersion",
  23. "projects",
  24. )
  25. lookup_field = "version"
  26. def create(self, validated_data):
  27. version = validated_data.pop("version")
  28. organization = validated_data.pop("organization")
  29. instance, _ = Release.objects.get_or_create(
  30. version=version, organization=organization, defaults=validated_data
  31. )
  32. return instance
  33. class ReleaseUpdateSerializer(ReleaseSerializer):
  34. version = serializers.CharField(read_only=True)
  35. class ReleaseFileSerializer(serializers.ModelSerializer):
  36. file = serializers.FileField(write_only=True, allow_empty_file=True)
  37. sha1 = serializers.CharField(source="file.checksum", read_only=True)
  38. dateCreated = serializers.DateTimeField(source="created", read_only=True)
  39. headers = serializers.JSONField(source="file.headers", read_only=True)
  40. size = serializers.IntegerField(source="file.size", read_only=True)
  41. id = serializers.CharField(read_only=True)
  42. class Meta:
  43. model = ReleaseFile
  44. fields = ("sha1", "name", "dateCreated", "headers", "file", "id", "size")
  45. read_only_fields = ("headers",)
  46. extra_kwargs = {"file": {"write_only": True}}
  47. def create(self, validated_data):
  48. fileobj = validated_data.pop("file")
  49. release = validated_data.pop("release")
  50. full_name = validated_data.get("name", fileobj.name)
  51. name = full_name.rsplit("/", 1)[-1]
  52. headers = {"Content-Type": fileobj.content_type}
  53. validated_data["name"] = name
  54. validated_data["headers"] = headers
  55. file = File.objects.create(name=name, headers=headers)
  56. file.putfile(fileobj)
  57. try:
  58. release_file = ReleaseFile.objects.create(
  59. release=release, file=file, name=full_name,
  60. )
  61. except IntegrityError:
  62. file.delete()
  63. raise ConflictException("File already present!")
  64. return release_file
  65. class AssembleSerializer(serializers.Serializer):
  66. checksum = serializers.RegexField("^[a-fA-F0-9]+$", max_length=40, min_length=40)
  67. chunks = serializers.ListField(
  68. child=serializers.RegexField("^[a-fA-F0-9]+$", max_length=40, min_length=40)
  69. )