Skip to content

Commit

Permalink
fix orcabus_id prefix filter
Browse files Browse the repository at this point in the history
  • Loading branch information
williamputraintan committed Oct 1, 2024
1 parent 61e50e6 commit b7edb27
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@


class BaseViewSet(ReadOnlyModelViewSet, ABC):
lookup_value_regex = "[^/]+" # This is to allow for special characters in the URL
orcabus_id_prefix = ''
ordering_fields = "__all__"
ordering = ["-orcabus_id"]
Expand All @@ -24,3 +25,14 @@ def retrieve(self, request, *args, **kwargs):
obj = get_object_or_404(self.queryset, pk=pk)
serializer = self.serializer_class(obj)
return Response(serializer.data)

def get_query_params(self):
"""
Sanitize query params if needed
e.g. remove prefixes for each orcabus_id
"""
query_params = self.request.query_params.copy()
orcabus_id = query_params.get("orcabus_id", None)
if orcabus_id and orcabus_id.startswith(self.orcabus_id_prefix):
query_params['orcabus_id'] = orcabus_id[len(self.orcabus_id_prefix):]
return query_params
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ def list(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs)

def get_queryset(self):
return Contact.objects.get_by_keyword(**self.request.query_params)
query_params = self.get_query_params()
return Contact.objects.get_by_keyword(**query_params)
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ def list(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs)

def get_queryset(self):
return Individual.objects.get_by_keyword(self.queryset, **self.request.query_params)
query_params = self.get_query_params()
return Individual.objects.get_by_keyword(self.queryset, **query_params)
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class LibraryViewSet(BaseViewSet):

def get_queryset(self):
qs = self.queryset
query_params = self.request.query_params.copy()
query_params = self.get_query_params()

coverage__lte = query_params.get("coverage__lte", None)
if coverage__lte:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ def list(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs)

def get_queryset(self):
return Project.objects.get_by_keyword(self.queryset, **self.request.query_params)
query_params = self.get_query_params()
return Project.objects.get_by_keyword(self.queryset, **query_params)
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,5 @@ def list(self, request, *args, **kwargs):
return super().list(request, *args, **kwargs)

def get_queryset(self):
return Sample.objects.get_by_keyword(**self.request.query_params)
query_params = self.get_query_params()
return Sample.objects.get_by_keyword(**query_params)
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SubjectViewSet(BaseViewSet):
def get_queryset(self):

qs = self.queryset
query_params = self.request.query_params.copy()
query_params = self.get_query_params()

library_id = query_params.get("library_id", None)
if library_id:
Expand Down

0 comments on commit b7edb27

Please sign in to comment.