-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserializers.py
55 lines (44 loc) · 2.47 KB
/
serializers.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from django.conf import settings
from django.utils.translation import ugettext_lazy as _
from rest_framework.exceptions import ValidationError
from rest_framework.fields import empty
from rest_framework.reverse import reverse
from drf_chunked_upload.serializers import ChunkedUploadSerializer
from rest_framework import serializers
from main.api.fields import CustomBase64File
from main.api.serializers import SerializerCreateExtraMixin, GetGroupFromRequestMixin
from record_cards.serializers import CheckFileExtensionsMixin
from support_info.models import SupportInfo, SupportChunkedFile
class SupportInfoSerializer(SerializerCreateExtraMixin, serializers.ModelSerializer):
file = CustomBase64File(required=False, allow_null=True)
class Meta:
model = SupportInfo
fields = ("id", "user_id", "created_at", "updated_at", "title", "description", "type", "category", "file",
"link")
read_only_fields = ("id", "user_id", "created_at", "updated_at", "file")
class SupportInfoShortSerializer(serializers.ModelSerializer):
class Meta:
model = SupportInfo
fields = ("id", "user_id", "created_at", "title", "type")
class SupportChunkedFileSerializer(SerializerCreateExtraMixin, GetGroupFromRequestMixin, CheckFileExtensionsMixin,
ChunkedUploadSerializer):
support_info_id = serializers.PrimaryKeyRelatedField(
source="support_info", queryset=SupportInfo.objects.all(),
error_messages={"does_not_exist": _("The selected Support Info does not exist")})
support_info = SupportInfoShortSerializer(read_only=True)
class Meta:
model = SupportChunkedFile
fields = ("id", "created_at", "status", "completed_at", "support_info_id", "support_info", "file", "filename",
"offset", "url")
def run_validation(self, data=empty):
validated_data = super().run_validation(data)
if validated_data["support_info"].type != SupportInfo.DOCUMENTATION:
raise ValidationError({"support_info_id": _("Support info must be documentation type")}, code="invalid")
return validated_data
def get_url(self, obj):
url = reverse("private_api:support_info:support_info_file_upload_chunk",
kwargs={"pk": obj.id}, request=self.context["request"])
# todo: workaround of X-FORWARDED-PROTO incorrect in enviroments
if not settings.DEBUG:
url = url.replace("http://", "https://")
return url