-
Notifications
You must be signed in to change notification settings - Fork 2.7k
/
Copy pathserializers.py
495 lines (406 loc) · 18.7 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
"""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
import ujson as json
from data_manager.models import Filter, FilterGroup, View
from django.conf import settings
from django.db import transaction
from drf_yasg import openapi
from projects.models import Project
from rest_framework import serializers
from tasks.models import Task
from tasks.serializers import (
AnnotationDraftSerializer,
AnnotationSerializer,
PredictionSerializer,
TaskSerializer,
)
from users.models import User
from label_studio.core.utils.common import round_floats
class FilterSerializer(serializers.ModelSerializer):
class Meta:
model = Filter
fields = '__all__'
def validate_column(self, column: str) -> str:
"""
Ensure that the passed filter expression starts with 'filter:tasks:' and contains
no foreign key traversals. This means either the filter expression contains no '__'
substrings, or that it's the task.data json field that's accessed.
Users depending on foreign key traversals in views can allowlist them via the
DATA_MANAGER_FILTER_ALLOWLIST setting in the env.
Edit with care. The validations below are critical for security.
"""
column_copy = column
# We may support 'filter:annotations:' in the future, but we don't as of yet.
required_prefix = 'filter:tasks:'
optional_prefix = '-'
if not column_copy.startswith(required_prefix):
raise serializers.ValidationError(f'Filter "{column}" should start with "{required_prefix}"')
column_copy = column_copy[len(required_prefix) :]
if column_copy.startswith(optional_prefix):
column_copy = column_copy[len(optional_prefix) :]
if column_copy.startswith('data.'):
# Allow underscores if the filter is based on the `task.data` JSONField, because these don't leverage foreign keys.
return column
# Specific filters relying on foreign keys can be allowlisted
if column_copy in settings.DATA_MANAGER_FILTER_ALLOWLIST:
return column
# But in general, we don't allow foreign keys
if '__' in column_copy:
raise serializers.ValidationError(
f'"__" is not generally allowed in filters. Consider asking your administrator to add "{column_copy}" '
'to DATA_MANAGER_FILTER_ALLOWLIST, but note that some filter expressions may pose a security risk'
)
return column
class FilterGroupSerializer(serializers.ModelSerializer):
filters = FilterSerializer(many=True)
class Meta:
model = FilterGroup
fields = '__all__'
class ViewSerializer(serializers.ModelSerializer):
filter_group = FilterGroupSerializer(required=False)
class Meta:
model = View
fields = '__all__'
def to_internal_value(self, data):
"""
map old filters structure to models
"filters": { ===> FilterGroup model
"conjunction": "or",
"items":[ ===> "filters" in FilterGroup
{ ==> Filter model
"filter":"filter:tasks:data.image", ==> column
"operator":"contains",
"type":"Image",
"value": <string: "XXX" | int: 123 | dict | list>
},
{
"filter":"filter:tasks:data.image",
"operator":"equal",
"type":"Image",
"value": <string: "XXX" | int: 123 | dict | list>
}
]
}
}
"""
_data = data.get('data', {})
filters = _data.pop('filters', {})
conjunction = filters.get('conjunction')
if 'filter_group' not in data and conjunction:
data['filter_group'] = {'conjunction': conjunction, 'filters': []}
if 'items' in filters:
for f in filters['items']:
data['filter_group']['filters'].append(
{
'column': f.get('filter', ''),
'operator': f.get('operator', ''),
'type': f.get('type', ''),
'value': f.get('value', {}),
}
)
ordering = _data.pop('ordering', {})
data['ordering'] = ordering
return super().to_internal_value(data)
def to_representation(self, instance):
result = super().to_representation(instance)
filters = result.pop('filter_group', {})
if filters:
filters['items'] = []
filters.pop('filters', [])
filters.pop('id', None)
for f in instance.filter_group.filters.order_by('index'):
filters['items'].append(
{
'filter': f.column,
'operator': f.operator,
'type': f.type,
'value': f.value,
}
)
result['data']['filters'] = filters
selected_items = result.pop('selected_items', {})
if selected_items:
result['data']['selectedItems'] = selected_items
ordering = result.pop('ordering', {})
if ordering:
result['data']['ordering'] = ordering
return result
@staticmethod
def _create_filters(filter_group, filters_data):
filter_index = 0
for filter_data in filters_data:
filter_data['index'] = filter_index
filter_group.filters.add(Filter.objects.create(**filter_data))
filter_index += 1
def create(self, validated_data):
with transaction.atomic():
filter_group_data = validated_data.pop('filter_group', None)
if filter_group_data:
filters_data = filter_group_data.pop('filters', [])
filter_group = FilterGroup.objects.create(**filter_group_data)
self._create_filters(filter_group=filter_group, filters_data=filters_data)
validated_data['filter_group_id'] = filter_group.id
view = self.Meta.model.objects.create(**validated_data)
return view
def update(self, instance, validated_data):
with transaction.atomic():
filter_group_data = validated_data.pop('filter_group', None)
if filter_group_data:
filters_data = filter_group_data.pop('filters', [])
filter_group = instance.filter_group
if filter_group is None:
filter_group = FilterGroup.objects.create(**filter_group_data)
conjunction = filter_group_data.get('conjunction')
if conjunction and filter_group.conjunction != conjunction:
filter_group.conjunction = conjunction
filter_group.save()
filter_group.filters.clear()
self._create_filters(filter_group=filter_group, filters_data=filters_data)
ordering = validated_data.pop('ordering', None)
if ordering and ordering != instance.ordering:
instance.ordering = ordering
instance.save()
if validated_data['data'] != instance.data:
instance.data = validated_data['data']
instance.save()
return instance
class UpdatedByDMFieldSerializer(serializers.SerializerMethodField):
# TODO: get_updated_by implementation is weird, but we need to adhere schema to it
class Meta:
swagger_schema_fields = {
'type': openapi.TYPE_ARRAY,
'title': 'User IDs',
'description': 'User IDs who updated this task',
'items': {'type': openapi.TYPE_OBJECT, 'title': 'User IDs'},
}
class AnnotatorsDMFieldSerializer(serializers.SerializerMethodField):
# TODO: get_updated_by implementation is weird, but we need to adhere schema to it
class Meta:
swagger_schema_fields = {
'type': openapi.TYPE_ARRAY,
'title': 'Annotators IDs',
'description': 'Annotators IDs who annotated this task',
'items': {'type': openapi.TYPE_INTEGER, 'title': 'User IDs'},
}
class CompletedByDMSerializerWithGenericSchema(serializers.PrimaryKeyRelatedField):
# TODO: likely we need to remove full user details from GET /api/tasks/{id} as it non-secure and currently controlled by the export toggle
class Meta:
swagger_schema_fields = {
'type': openapi.TYPE_OBJECT,
'title': 'User details',
'description': 'User details who completed this annotation.',
}
class AnnotationsDMFieldSerializer(AnnotationSerializer):
completed_by = CompletedByDMSerializerWithGenericSchema(required=False, queryset=User.objects.all())
class AnnotationDraftDMFieldSerializer(serializers.SerializerMethodField):
class Meta:
swagger_schema_fields = {
'type': openapi.TYPE_ARRAY,
'title': 'Annotation drafts',
'description': 'Drafts for this task',
'items': {
'type': openapi.TYPE_OBJECT,
'title': 'Draft object',
'properties': {
'result': {
'type': openapi.TYPE_ARRAY,
'title': 'Draft result',
'items': {
'type': openapi.TYPE_OBJECT,
'title': 'Draft result item',
},
},
'created_at': {
'type': openapi.TYPE_STRING,
'format': 'date-time',
'title': 'Creation time',
},
'updated_at': {
'type': openapi.TYPE_STRING,
'format': 'date-time',
'title': 'Last update time',
},
},
},
}
class PredictionsDMFieldSerializer(serializers.SerializerMethodField):
class Meta:
swagger_schema_fields = {
'type': openapi.TYPE_ARRAY,
'title': 'Predictions',
'description': 'Predictions for this task',
'items': {
'type': openapi.TYPE_OBJECT,
'title': 'Prediction object',
'properties': {
'result': {
'type': openapi.TYPE_ARRAY,
'title': 'Prediction result',
'items': {
'type': openapi.TYPE_OBJECT,
'title': 'Prediction result item',
},
},
'score': {
'type': openapi.TYPE_NUMBER,
'title': 'Prediction score',
},
'model_version': {
'type': openapi.TYPE_STRING,
'title': 'Model version',
},
'model': {
'type': openapi.TYPE_OBJECT,
'title': 'ML Backend instance',
},
'model_run': {
'type': openapi.TYPE_OBJECT,
'title': 'Model Run instance',
},
'task': {
'type': openapi.TYPE_INTEGER,
'title': 'Task ID related to the prediction',
},
'project': {
'type': openapi.TYPE_NUMBER,
'title': 'Project ID related to the prediction',
},
'created_at': {
'type': openapi.TYPE_STRING,
'format': 'date-time',
'title': 'Creation time',
},
'updated_at': {
'type': openapi.TYPE_STRING,
'format': 'date-time',
'title': 'Last update time',
},
},
},
}
class DataManagerTaskSerializer(TaskSerializer):
predictions = PredictionsDMFieldSerializer(required=False, read_only=True)
annotations = AnnotationsDMFieldSerializer(required=False, many=True, default=[], read_only=True)
drafts = AnnotationDraftDMFieldSerializer(required=False, read_only=True)
annotators = AnnotatorsDMFieldSerializer(required=False, read_only=True)
inner_id = serializers.IntegerField(required=False)
cancelled_annotations = serializers.IntegerField(required=False)
total_annotations = serializers.IntegerField(required=False)
total_predictions = serializers.IntegerField(required=False)
completed_at = serializers.DateTimeField(required=False)
annotations_results = serializers.SerializerMethodField(required=False)
predictions_results = serializers.SerializerMethodField(required=False)
predictions_score = serializers.FloatField(required=False)
file_upload = serializers.SerializerMethodField(required=False)
storage_filename = serializers.SerializerMethodField(required=False)
annotations_ids = serializers.SerializerMethodField(required=False)
predictions_model_versions = serializers.SerializerMethodField(required=False)
avg_lead_time = serializers.FloatField(required=False)
draft_exists = serializers.BooleanField(required=False)
updated_by = UpdatedByDMFieldSerializer(required=False, read_only=True)
CHAR_LIMITS = 500
class Meta:
model = Task
ref_name = 'data_manager_task_serializer'
fields = '__all__'
expandable_fields = {'annotations': (AnnotationSerializer, {'many': True})}
def to_representation(self, obj):
"""Dynamically manage including of some fields in the API result"""
ret = super(DataManagerTaskSerializer, self).to_representation(obj)
if not self.context.get('annotations'):
ret.pop('annotations', None)
if not self.context.get('predictions'):
ret.pop('predictions', None)
return ret
def _pretty_results(self, task, field, unique=False):
if not hasattr(task, field) or getattr(task, field) is None:
return ''
result = getattr(task, field)
if isinstance(result, str):
output = result
if unique:
output = list(set(output.split(',')))
output = ','.join(output)
elif isinstance(result, int):
output = str(result)
else:
result = [r for r in result if r is not None]
if unique:
result = list(set(result))
result = round_floats(result)
output = json.dumps(result, ensure_ascii=False)[1:-1] # remove brackets [ ]
return output[: self.CHAR_LIMITS].replace(',"', ', "').replace('],[', '] [').replace('"', '')
def get_annotations_results(self, task):
return self._pretty_results(task, 'annotations_results')
def get_predictions_results(self, task):
return self._pretty_results(task, 'predictions_results')
def get_predictions(self, task):
return PredictionSerializer(task.predictions, many=True, default=[], read_only=True).data
@staticmethod
def get_file_upload(task):
if hasattr(task, 'file_upload_field'):
file_upload = task.file_upload_field
return os.path.basename(task.file_upload_field) if file_upload else None
return None
@staticmethod
def get_storage_filename(task):
return task.get_storage_filename()
@staticmethod
def get_updated_by(obj):
return [{'user_id': obj.updated_by_id}] if obj.updated_by_id else []
@staticmethod
def get_annotators(obj):
if not hasattr(obj, 'annotators'):
return []
annotators = obj.annotators
if not annotators:
return []
if isinstance(annotators, str):
annotators = [int(v) for v in annotators.split(',')]
annotators = list(set(annotators))
annotators = [a for a in annotators if a is not None]
return annotators if hasattr(obj, 'annotators') and annotators else []
def get_annotations_ids(self, task):
return self._pretty_results(task, 'annotations_ids', unique=True)
def get_predictions_model_versions(self, task):
return self._pretty_results(task, 'predictions_model_versions', unique=True)
def get_drafts_serializer(self):
return AnnotationDraftSerializer
def get_drafts_queryset(self, user, drafts):
"""Get all user's draft"""
return drafts.filter(user=user)
def get_drafts(self, task):
"""Return drafts only for the current user"""
# it's for swagger documentation
if not isinstance(task, Task) or not self.context.get('drafts'):
return []
drafts = task.drafts
if 'request' in self.context and hasattr(self.context['request'], 'user'):
user = self.context['request'].user
drafts = self.get_drafts_queryset(user, drafts)
serializer_class = self.get_drafts_serializer()
return serializer_class(drafts, many=True, read_only=True, default=True, context=self.context).data
class SelectedItemsSerializer(serializers.Serializer):
all = serializers.BooleanField()
included = serializers.ListField(child=serializers.IntegerField(), required=False)
excluded = serializers.ListField(child=serializers.IntegerField(), required=False)
def validate(self, data):
if data['all'] is True and data.get('included'):
raise serializers.ValidationError('included not allowed with all==true')
if data['all'] is False and data.get('excluded'):
raise serializers.ValidationError('excluded not allowed with all==false')
view = self.context.get('view')
request = self.context.get('request')
if view and request and request.method in ('PATCH', 'DELETE'):
all_value = view.selected_items.get('all')
if all_value and all_value != data['all']:
raise serializers.ValidationError('changing all value possible only with POST method')
return data
class ViewResetSerializer(serializers.Serializer):
project = serializers.PrimaryKeyRelatedField(queryset=Project.objects.all())
class ViewOrderSerializer(serializers.Serializer):
project = serializers.IntegerField()
ids = serializers.ListField(
child=serializers.IntegerField(), allow_empty=False, help_text='A list of view IDs in the desired order.'
)