Skip to content

Commit

Permalink
[fix] Change tasks get serializer (HumanSignal#1399)
Browse files Browse the repository at this point in the history
* [fix] change tasks get serializer

* Add undefined resolver

* Rename TasksListAPI => ProjectTaskListAPI

Co-authored-by: nik <[email protected]>
Co-authored-by: makseq-ubnt <[email protected]>
  • Loading branch information
3 people authored Sep 3, 2021
1 parent b509ad4 commit 076e257
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 8 deletions.
19 changes: 12 additions & 7 deletions label_studio/projects/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
ProjectSerializer, ProjectLabelConfigSerializer, ProjectSummarySerializer
)
from tasks.models import Task, Annotation, Prediction, TaskLock
from tasks.serializers import TaskSerializer, TaskWithAnnotationsAndPredictionsAndDraftsSerializer
from tasks.serializers import TaskSerializer, TaskSimpleSerializer, TaskWithAnnotationsAndPredictionsAndDraftsSerializer
from webhooks.utils import api_webhook, api_webhook_for_delete, emit_webhooks_for_instance
from webhooks.models import WebhookAction

Expand Down Expand Up @@ -558,7 +558,6 @@ def get(self, *args, **kwargs):
return super(ProjectSummaryAPI, self).get(*args, **kwargs)



@method_decorator(name='delete', decorator=swagger_auto_schema(
tags=['Projects'],
operation_summary='Delete all tasks',
Expand All @@ -574,8 +573,8 @@ def get(self, *args, **kwargs):
```
""".format(settings.HOSTNAME or 'https://localhost:8080')
))
class TasksListAPI(generics.ListCreateAPIView,
generics.DestroyAPIView):
class ProjectTaskListAPI(generics.ListCreateAPIView,
generics.DestroyAPIView):

parser_classes = (JSONParser, FormParser)
queryset = Task.objects.all()
Expand All @@ -588,6 +587,12 @@ class TasksListAPI(generics.ListCreateAPIView,
redirect_route = 'projects:project-settings'
redirect_kwarg = 'pk'

def get_serializer_class(self):
if self.request.method == 'GET':
return TaskSimpleSerializer
else:
return TaskSerializer

def filter_queryset(self, queryset):
project = generics.get_object_or_404(Project.objects.for_user(self.request.user), pk=self.kwargs.get('pk', 0))
tasks = Task.objects.filter(project=project)
Expand All @@ -601,14 +606,14 @@ def delete(self, request, *args, **kwargs):
return Response(data={'tasks': task_ids}, status=204)

def get(self, *args, **kwargs):
return super(TasksListAPI, self).get(*args, **kwargs)
return super(ProjectTaskListAPI, self).get(*args, **kwargs)

@swagger_auto_schema(auto_schema=None)
def post(self, *args, **kwargs):
return super(TasksListAPI, self).post(*args, **kwargs)
return super(ProjectTaskListAPI, self).post(*args, **kwargs)

def get_serializer_context(self):
context = super(TasksListAPI, self).get_serializer_context()
context = super(ProjectTaskListAPI, self).get_serializer_context()
context['project'] = get_object_with_check_and_log(self.request, Project, pk=self.kwargs['pk'])
return context

Expand Down
2 changes: 1 addition & 1 deletion label_studio/projects/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
path('<int:pk>/summary/', api.ProjectSummaryAPI.as_view(), name='project-summary'),

# Tasks list for the project: get and destroy
path('<int:pk>/tasks/', api.TasksListAPI.as_view(), name='project-tasks-list'),
path('<int:pk>/tasks/', api.ProjectTaskListAPI.as_view(), name='project-tasks-list'),

# Generate sample task for this project
path('<int:pk>/sample-task/', api.ProjectSampleTask.as_view(), name='project-sample-task'),
Expand Down
9 changes: 9 additions & 0 deletions label_studio/tasks/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,15 @@ def __init__(self, *args, **kwargs):
self.fields['annotations'] = AnnotationSerializer(many=True, default=[], context=self.context, read_only=True)
self.fields['predictions'] = PredictionSerializer(many=True, default=[], context=self.context, read_only=True)

def to_representation(self, instance):
project = instance.project
if project:
# resolve $undefined$ key in task data
data = instance.data
replace_task_data_undefined_with_config_field(data, project)

return super().to_representation(instance)

class Meta:
model = Task
fields = '__all__'
Expand Down

0 comments on commit 076e257

Please sign in to comment.