chenzhaoyang
2025-12-17 063da0bf961e1d35e25dc107f883f7492f4c5a7c
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
56
57
58
59
"""This file and its contents are licensed under the Apache License 2.0. Please see the included NOTICE for copyright information and LICENSE for a copy of the license.
"""
import os
 
from django.conf import settings
from io_storages.base_models import ExportStorage, ImportStorage
from rest_framework import serializers
from tasks.models import Task
from tasks.serializers import AnnotationSerializer, TaskSerializer
from users.models import User
 
from label_studio.core.utils.common import load_func
 
 
class ImportStorageSerializer(serializers.ModelSerializer):
    type = serializers.ReadOnlyField(default=os.path.basename(os.path.dirname(__file__)))
    synchronizable = serializers.BooleanField(required=False, default=True)
 
    def validate(self, data):
        data = super(ImportStorageSerializer, self).validate(data)
        if settings.IMPORT_STORAGE_SERIALIZER_VALIDATE:
            validate_func = load_func(settings.IMPORT_STORAGE_SERIALIZER_VALIDATE)
            data = validate_func(self, data)
        return data
 
    class Meta:
        model = ImportStorage
        fields = '__all__'
 
 
class ExportStorageSerializer(serializers.ModelSerializer):
    type = serializers.ReadOnlyField(default=os.path.basename(os.path.dirname(__file__)))
    synchronizable = serializers.BooleanField(required=False, default=True)
 
    class Meta:
        model = ExportStorage
        fields = '__all__'
 
 
class StorageTaskSerializer(TaskSerializer):
    def __init__(self, *args, **kwargs):
        # task is nested into the annotation, we don't need annotations in the task again
        kwargs['context'] = {'resolve_uri': False}
        super().__init__(*args, **kwargs)
 
    class Meta:
        model = Task
        fields = '__all__'
 
 
class StorageCompletedBySerializer(serializers.ModelSerializer):
    class Meta:
        model = User
        fields = ('id', 'first_name', 'last_name', 'email')
 
 
class StorageAnnotationSerializer(AnnotationSerializer):
    task = StorageTaskSerializer(read_only=True, omit=['annotations'])
    completed_by = StorageCompletedBySerializer(read_only=True)