Skip to content

Commit

Permalink
Fix: Metadata Filter && Sortable Endpoint (#523)
Browse files Browse the repository at this point in the history
  • Loading branch information
williamputraintan authored Aug 30, 2024
1 parent 42a8d07 commit 4a9ebbf
Showing 1 changed file with 24 additions and 9 deletions.
33 changes: 24 additions & 9 deletions lib/workload/stateless/stacks/metadata-manager/app/viewsets/lab.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class SubjectViewSet(ReadOnlyModelViewSet):
pagination_class = StandardResultsSetPagination
filter_backends = [filters.OrderingFilter, filters.SearchFilter]
ordering_fields = "__all__"
ordering = ["-subject_id"]
ordering = ["-orcabus_id"]
search_fields = Subject.get_base_fields()
queryset = Subject.objects.none()

Expand All @@ -33,7 +33,7 @@ def get_queryset(self):
responses={200: SubjectFullSerializer(many=True)},
parameters=[
SubjectSerializer,
OpenApiParameter(name='libraryId',
OpenApiParameter(name='library_id',
description='Filter the subjects that has the given library_id in '
'the Library model.',
required=False,
Expand All @@ -43,10 +43,12 @@ def get_queryset(self):
@action(detail=False, methods=['get'], url_path='full')
def get_full_model_set(self, request):
query_params = self.request.query_params.copy()
qs = Subject.objects.prefetch_related("specimen_set__library_set").all().order_by("-subject_id")

# Allow filtering by libraryId
library_id = query_params.get("libraryId", None)
ordering = query_params.get("ordering", '-orcabus_id')
qs = Subject.objects.prefetch_related("specimen_set__library_set").all().order_by(ordering)

# Allow filtering by library_id
library_id = query_params.get("library_id", None)
if library_id:
query_params.pop("library_id")
qs = qs.filter(specimen__library__library_id=library_id)
Expand Down Expand Up @@ -74,7 +76,7 @@ class SpecimenViewSet(ReadOnlyModelViewSet):
pagination_class = StandardResultsSetPagination
filter_backends = [filters.OrderingFilter, filters.SearchFilter]
ordering_fields = "__all__"
ordering = ["-specimen_id"]
ordering = ["-orcabus_id"]
search_fields = Specimen.get_base_fields()
queryset = Specimen.objects.none()

Expand All @@ -94,7 +96,7 @@ class LibraryViewSet(ReadOnlyModelViewSet):
pagination_class = StandardResultsSetPagination
filter_backends = [filters.OrderingFilter, filters.SearchFilter]
ordering_fields = "__all__"
ordering = ["-library_id"]
ordering = ["-orcabus_id"]
search_fields = Library.get_base_fields()
queryset = Library.objects.none()

Expand All @@ -113,10 +115,23 @@ def get_queryset(self):
)
@action(detail=False, methods=['get'], url_path='full')
def get_full_model_set(self, request):
qs = Library.objects.select_related("specimen__subject").all().order_by("-library_id")
query_params = self.request.query_params.copy()

ordering = query_params.get("ordering", '-orcabus_id')
qs = Library.objects.select_related("specimen__subject").all().order_by(ordering)

coverage__lte = query_params.get("coverage__lte", None)
if coverage__lte:
query_params.pop("coverage__lte")
qs = qs.filter(coverage__lte=coverage__lte)

coverage__gte = query_params.get("coverage__gte", None)
if coverage__gte:
query_params.pop("coverage__gte")
qs = qs.filter(coverage__gte=coverage__gte)

# Allow filtering by the keys inside the library model
qs = Library.objects.get_model_fields_query(qs, **self.request.query_params)
qs = Library.objects.get_model_fields_query(qs, **query_params)

page = self.paginate_queryset(qs)
serializer = LibraryFullSerializer(page, many=True)
Expand Down

0 comments on commit 4a9ebbf

Please sign in to comment.