Skip to content

Commit

Permalink
Continued, added urls, labels and attributes for tasks.
Browse files Browse the repository at this point in the history
  • Loading branch information
Nikita Manovich committed Jan 24, 2019
1 parent 0b8c000 commit 056e4c4
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 11 deletions.
50 changes: 43 additions & 7 deletions cvat/apps/engine/serializers.py
Original file line number Diff line number Diff line change
@@ -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')
Expand Down
6 changes: 3 additions & 3 deletions cvat/apps/engine/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/<int:pk>', views.UserDetail.as_view(),
name='user-detail'),
Expand Down
3 changes: 2 additions & 1 deletion cvat/apps/engine/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 056e4c4

Please sign in to comment.