Skip to content

Commit

Permalink
feat: DEV-2575: Add filter by ids for project list (HumanSignal#2565)
Browse files Browse the repository at this point in the history
* feat: DEV-2575: Add filter by ids for project list

* Add test (DEV-2575)
  • Loading branch information
triklozoid authored Jun 27, 2022
1 parent 27eb3e2 commit 0d76e69
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 2 deletions.
10 changes: 10 additions & 0 deletions label_studio/core/filters.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from django_filters import Filter
from django_filters.constants import EMPTY_VALUES

class ListFilter(Filter):
def filter(self, qs, value):
if value in EMPTY_VALUES:
return qs
value_list = value.split(",")
qs = super().filter(qs, value_list)
return qs
11 changes: 9 additions & 2 deletions label_studio/projects/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@
"""
import drf_yasg.openapi as openapi
import logging
import numpy as np
import pathlib
import os

from django.db import IntegrityError
from django.conf import settings
from drf_yasg.utils import swagger_auto_schema
from django.utils.decorators import method_decorator
from django_filters.rest_framework import DjangoFilterBackend
from django_filters import FilterSet
from rest_framework import generics, status, filters
from rest_framework.exceptions import NotFound, ValidationError as RestValidationError
from rest_framework.parsers import FormParser, JSONParser, MultiPartParser
Expand Down Expand Up @@ -38,6 +39,7 @@
get_object_with_check_and_log, paginator, paginator_help)
from core.utils.exceptions import ProjectExistException, LabelStudioDatabaseException
from core.utils.io import find_dir, find_file, read_yaml
from core.filters import ListFilter

from data_manager.functions import get_prepared_queryset, filters_ordering_selected_items_exist
from data_manager.models import View
Expand Down Expand Up @@ -91,6 +93,10 @@ class ProjectListPagination(PageNumberPagination):
page_size_query_param = 'page_size'


class ProjectFilterSet(FilterSet):
ids = ListFilter(field_name="id", lookup_expr="in")


@method_decorator(name='get', decorator=swagger_auto_schema(
tags=['Projects'],
operation_summary='List your projects',
Expand Down Expand Up @@ -120,7 +126,8 @@ class ProjectListPagination(PageNumberPagination):
class ProjectListAPI(generics.ListCreateAPIView):
parser_classes = (JSONParser, FormParser, MultiPartParser)
serializer_class = ProjectSerializer
filter_backends = [filters.OrderingFilter]
filter_backends = [filters.OrderingFilter, DjangoFilterBackend]
filterset_class = ProjectFilterSet
permission_required = ViewClassPermission(
GET=all_permissions.projects_view,
POST=all_permissions.projects_create,
Expand Down
66 changes: 66 additions & 0 deletions label_studio/tests/test_projects.tavern.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,69 @@ stages:
status_code: 200
json:
id: !int "{project_pk}"
---

test_name: test_project_ids_filter

strict: False

marks:
- usefixtures:
- django_live_url

stages:
- id: signup
type: ref
- id: get_user_token
type: ref
- id: create_project1
name: Create project
request:
url: "{django_live_url}/api/projects"
json:
title: create_batch_tasks_assignments
label_config: <View><Text name="text" value="$text"/><Choices name="label" toName="text"><Choice value="pos"/><Choice value="neg"/></Choices></View>
is_published: true
method: POST
headers:
content-type: application/json
response:
status_code: 201
save:
json:
project_pk1: id
- id: create_project2
name: Create project
request:
url: "{django_live_url}/api/projects"
json:
title: create_batch_tasks_assignments
label_config: <View><Text name="text" value="$text"/><Choices name="label" toName="text"><Choice value="pos"/><Choice value="neg"/></Choices></View>
is_published: true
method: POST
headers:
content-type: application/json
response:
status_code: 201
save:
json:
project_pk2: id
- name: get_first
request:
method: GET
url: '{django_live_url}/api/projects?ids={project_pk1}'
response:
status_code: 200
json:
results:
- id: !int "{project_pk1}"
- name: get_both
request:
method: GET
url: '{django_live_url}/api/projects?ids={project_pk1},{project_pk2}&ordering=created_at'
response:
status_code: 200
json:
results:
- id: !int "{project_pk1}"
- id: !int "{project_pk2}"

0 comments on commit 0d76e69

Please sign in to comment.