From 056e4c497d63c6bd790c04daf533ef8d1c7e6f38 Mon Sep 17 00:00:00 2001 From: Nikita Manovich Date: Thu, 24 Jan 2019 19:26:31 +0300 Subject: [PATCH] Continued, added urls, labels and attributes for tasks. --- cvat/apps/engine/serializers.py | 50 ++++++++++++++++++++++++++++----- cvat/apps/engine/urls.py | 6 ++-- cvat/apps/engine/views.py | 3 +- 3 files changed, 48 insertions(+), 11 deletions(-) diff --git a/cvat/apps/engine/serializers.py b/cvat/apps/engine/serializers.py index 81534827c9f8..60317113ee25 100644 --- a/cvat/apps/engine/serializers.py +++ b/cvat/apps/engine/serializers.py @@ -1,28 +1,64 @@ from rest_framework import serializers -from cvat.apps.engine.models import Task, Job +from cvat.apps.engine.models import Task, Job, Label, AttributeSpec from django.contrib.auth.models import User, Group +class AttributeSerializer(serializers.PrimaryKeyRelatedField, serializers.ModelSerializer): + class Meta: + model = AttributeSpec + fields = ('id', 'text') + +class LabelSerializer(serializers.PrimaryKeyRelatedField, serializers.ModelSerializer): + attributes = AttributeSerializer(many=True, source='attributespec_set') + class Meta: + model = Label + fields = ('id', 'name', 'attributes') + + def create(self, validated_data): + attributes = validated_data.pop('attributes') + label = Label.objects.create(**validated_data) + for attr in attributes: + AttributeSpec.objects.create(label=label, **attr) + + return label + + class TaskSerializer(serializers.ModelSerializer): + labels = LabelSerializer(many=True, source='label_set') class Meta: model = Task fields = ('id', 'name', 'size', 'mode', 'owner', 'assignee', 'bug_tracker', 'created_date', 'updated_date', 'overlap', - 'z_order', 'flipped', 'source', 'status') + 'z_order', 'flipped', 'status', 'labels') read_only_fields = ('size', 'mode', 'created_date', 'updated_date', - 'overlap', 'source', 'status') + 'overlap', 'status') + + def create(self, validated_data): + labels = validated_data.pop('labels') + task = Task.objects.create(**validated_data) + for label in labels: + Label.objects.create(task=task, **label) + + return task + + +class JobSerializer(serializers.HyperlinkedModelSerializer): + task_id = serializers.ReadOnlyField(source="segment.task.id") + start_frame = serializers.ReadOnlyField(source="segment.start_frame") + stop_frame = serializers.ReadOnlyField(source="segment.stop_frame") -class JobSerializer(serializers.ModelSerializer): class Meta: model = Job - fields = ('id', 'assignee', 'status') + fields = ('url', 'id', 'assignee', 'status', 'start_frame', + 'stop_frame', 'max_shape_id', 'task_id') + read_only_fields = ('max_shape_id',) -class UserSerializer(serializers.ModelSerializer): +class UserSerializer(serializers.HyperlinkedModelSerializer): groups = serializers.SlugRelatedField(many=True, slug_field='name', queryset=Group.objects.all()) class Meta: model = User - fields = ('id', 'username', 'first_name', 'last_name', 'email', + fields = ('url', 'id', 'username', 'first_name', 'last_name', 'email', 'groups', 'is_staff', 'is_superuser', 'is_active', 'last_login', 'date_joined', 'groups') read_only_fields = ('last_login', 'date_joined') diff --git a/cvat/apps/engine/urls.py b/cvat/apps/engine/urls.py index bb5674373b67..00055f26f7c5 100644 --- a/cvat/apps/engine/urls.py +++ b/cvat/apps/engine/urls.py @@ -11,12 +11,12 @@ urlpatterns = [ # entry point for API path(REST_API_PREFIX, views.api_root, name='root'), - # GET current active user - path(REST_API_PREFIX + 'users/self', views.UserSelf.as_view(), - name='user-self'), # GET list of users, POST a new user path(REST_API_PREFIX + 'users/', views.UserList.as_view(), name='user-list'), + # GET current active user + path(REST_API_PREFIX + 'users/self', views.UserSelf.as_view(), + name='user-self'), # GET, DELETE, PATCH the user path(REST_API_PREFIX + 'users/', views.UserDetail.as_view(), name='user-detail'), diff --git a/cvat/apps/engine/views.py b/cvat/apps/engine/views.py index b6948f1103ea..0b3628b9326c 100644 --- a/cvat/apps/engine/views.py +++ b/cvat/apps/engine/views.py @@ -60,7 +60,8 @@ class JobList(generics.ListAPIView): def list(self, request, pk, version=None): queryset = self.queryset.filter(segment__task_id=pk) - serializer = JobSerializer(queryset, many=True) + serializer = JobSerializer(queryset, many=True, + context={"request": request}) return Response(serializer.data)