Skip to content

Commit

Permalink
fix: DEV-3379: Remove unused fields from completed_by (#2918)
Browse files Browse the repository at this point in the history
* fix: DEV-3379: Remove unused fields from completed_by

* Fix tests (DEV-3379)
  • Loading branch information
triklozoid committed Sep 14, 2022
1 parent c76d96f commit c00498b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 43 deletions.
6 changes: 2 additions & 4 deletions label_studio/data_manager/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def update(self, instance, validated_data):

class DataManagerTaskSerializer(TaskSerializer):
predictions = serializers.SerializerMethodField(required=False, read_only=True)
annotations = serializers.SerializerMethodField(required=False, read_only=True)
annotations = AnnotationSerializer(required=False, many=True, default=[], read_only=True)
drafts = serializers.SerializerMethodField(required=False, read_only=True)
annotators = serializers.SerializerMethodField(required=False, read_only=True)

Expand All @@ -186,6 +186,7 @@ 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
Expand Down Expand Up @@ -225,9 +226,6 @@ def get_annotations_results(self, task):
def get_predictions_results(self, task):
return self._pretty_results(task, 'predictions_results')

def get_annotations(self, task):
return AnnotationSerializer(task.annotations, many=True, default=[], read_only=True).data

def get_predictions(self, task):
return PredictionSerializer(task.predictions, many=True, default=[], read_only=True).data

Expand Down
3 changes: 1 addition & 2 deletions label_studio/io_storages/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ 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'] = {
'include_annotations': False,
'resolve_uri': False
}
super().__init__(*args, **kwargs)
Expand All @@ -46,5 +45,5 @@ class Meta:


class StorageAnnotationSerializer(AnnotationSerializer):
task = StorageTaskSerializer(read_only=True)
task = StorageTaskSerializer(read_only=True, omit=['annotations'])
completed_by = StorageCompletedBySerializer(read_only=True)
5 changes: 2 additions & 3 deletions label_studio/tasks/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,10 @@ def prefetch(queryset):
)

def get_retrieve_serializer_context(self, request):
fields = ['completed_by_full', 'drafts', 'predictions', 'annotations']
fields = ['drafts', 'predictions', 'annotations']

return {
'resolve_uri': True,
'completed_by': 'full' if 'completed_by_full' in fields else None,
'predictions': 'predictions' in fields,
'annotations': 'annotations' in fields,
'drafts': 'drafts' in fields,
Expand All @@ -178,7 +177,7 @@ def get(self, request, pk):
and not self.task.predictions.exists():
evaluate_predictions([self.task])

serializer = self.get_serializer_class()(self.task, many=False, context=context)
serializer = self.get_serializer_class()(self.task, many=False, context=context, expand=['annotations.completed_by'])
data = serializer.data
return Response(data)

Expand Down
43 changes: 10 additions & 33 deletions label_studio/tasks/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import numbers

from django.db import transaction
from drf_dynamic_fields import DynamicFieldsMixin
from django.conf import settings

from rest_framework import serializers
Expand All @@ -15,13 +14,13 @@
from rest_framework.settings import api_settings
from rest_flex_fields import FlexFieldsModelSerializer

from core.feature_flags import flag_set
from projects.models import Project
from tasks.models import Task, Annotation, AnnotationDraft, Prediction
from tasks.validation import TaskValidator
from core.utils.common import get_object_with_check_and_log, retry_database_locked
from core.label_config import replace_task_data_undefined_with_config_field
from users.serializers import UserSerializer
from users.models import User
from core.utils.common import load_func

logger = logging.getLogger(__name__)
Expand All @@ -45,34 +44,18 @@ class ListAnnotationSerializer(serializers.ListSerializer):
pass


class AnnotationSerializer(ModelSerializer):
class CompletedByDMSerializer(UserSerializer):
class Meta:
model = User
fields = ['id', 'first_name', 'last_name', 'avatar', 'email', 'initials']


class AnnotationSerializer(FlexFieldsModelSerializer):
"""
"""
created_username = serializers.SerializerMethodField(default='', read_only=True, help_text='Username string')
created_ago = serializers.CharField(default='', read_only=True, help_text='Time delta from creation time')

@classmethod
def many_init(cls, *args, **kwargs):
kwargs['child'] = cls(*args, **kwargs)
return ListAnnotationSerializer(*args, **kwargs)

def to_representation(self, instance):
annotation = super(AnnotationSerializer, self).to_representation(instance)
if self.context.get('completed_by', '') == 'full':
annotation['completed_by'] = UserSerializer(instance.completed_by).data
return annotation

def get_fields(self):
fields = super(AnnotationSerializer, self).get_fields()
excluded = []

# serializer for export format
if self.context.get('export_mode', False):
excluded += ['created_username', 'created_ago', 'task',
'was_cancelled', 'ground_truth', 'result_count']

[fields.pop(field, None) for field in excluded]
return fields
completed_by = serializers.PrimaryKeyRelatedField(required=False, queryset=User.objects.all())

def validate_result(self, value):
data = value
Expand Down Expand Up @@ -104,6 +87,7 @@ def get_created_username(self, annotation):
class Meta:
model = Annotation
exclude = ['prediction', 'result_count']
expandable_fields = {'completed_by': (CompletedByDMSerializer,)}


class TaskSimpleSerializer(ModelSerializer):
Expand All @@ -130,13 +114,6 @@ class Meta:
class BaseTaskSerializer(FlexFieldsModelSerializer):
""" Task Serializer with project scheme configs validation
"""
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
if self.context.get('include_annotations', True) and 'annotations' not in self.fields:
self.fields['annotations'] = AnnotationSerializer(
many=True, read_only=False, required=False, context=self.context
)

def project(self, task=None):
""" Take the project from context
"""
Expand Down
3 changes: 2 additions & 1 deletion label_studio/users/serializers.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
"""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.
"""
from rest_framework import serializers
from rest_flex_fields import FlexFieldsModelSerializer
from django.conf import settings

from .models import User
from core.utils.common import load_func


class BaseUserSerializer(serializers.ModelSerializer):
class BaseUserSerializer(FlexFieldsModelSerializer):
# short form for user presentation
initials = serializers.SerializerMethodField(default='?', read_only=True)
avatar = serializers.SerializerMethodField(read_only=True)
Expand Down

0 comments on commit c00498b

Please sign in to comment.