From b7edb2782e14650daaaa490fb9f77ffe4b97acb7 Mon Sep 17 00:00:00 2001 From: william Date: Tue, 1 Oct 2024 12:13:17 +1000 Subject: [PATCH] fix orcabus_id prefix filter --- .../stacks/metadata-manager/app/viewsets/base.py | 12 ++++++++++++ .../stacks/metadata-manager/app/viewsets/contact.py | 3 ++- .../metadata-manager/app/viewsets/individual.py | 3 ++- .../stacks/metadata-manager/app/viewsets/library.py | 2 +- .../stacks/metadata-manager/app/viewsets/project.py | 3 ++- .../stacks/metadata-manager/app/viewsets/sample.py | 3 ++- .../stacks/metadata-manager/app/viewsets/subject.py | 2 +- 7 files changed, 22 insertions(+), 6 deletions(-) diff --git a/lib/workload/stateless/stacks/metadata-manager/app/viewsets/base.py b/lib/workload/stateless/stacks/metadata-manager/app/viewsets/base.py index eb45124a4..259bb6227 100644 --- a/lib/workload/stateless/stacks/metadata-manager/app/viewsets/base.py +++ b/lib/workload/stateless/stacks/metadata-manager/app/viewsets/base.py @@ -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"] @@ -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 diff --git a/lib/workload/stateless/stacks/metadata-manager/app/viewsets/contact.py b/lib/workload/stateless/stacks/metadata-manager/app/viewsets/contact.py index 11d57f5ed..aace47451 100644 --- a/lib/workload/stateless/stacks/metadata-manager/app/viewsets/contact.py +++ b/lib/workload/stateless/stacks/metadata-manager/app/viewsets/contact.py @@ -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) diff --git a/lib/workload/stateless/stacks/metadata-manager/app/viewsets/individual.py b/lib/workload/stateless/stacks/metadata-manager/app/viewsets/individual.py index 8087c9791..c214efe84 100644 --- a/lib/workload/stateless/stacks/metadata-manager/app/viewsets/individual.py +++ b/lib/workload/stateless/stacks/metadata-manager/app/viewsets/individual.py @@ -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) diff --git a/lib/workload/stateless/stacks/metadata-manager/app/viewsets/library.py b/lib/workload/stateless/stacks/metadata-manager/app/viewsets/library.py index bfb430e8f..5bc831af5 100644 --- a/lib/workload/stateless/stacks/metadata-manager/app/viewsets/library.py +++ b/lib/workload/stateless/stacks/metadata-manager/app/viewsets/library.py @@ -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: diff --git a/lib/workload/stateless/stacks/metadata-manager/app/viewsets/project.py b/lib/workload/stateless/stacks/metadata-manager/app/viewsets/project.py index 9e48caa51..747cb765f 100644 --- a/lib/workload/stateless/stacks/metadata-manager/app/viewsets/project.py +++ b/lib/workload/stateless/stacks/metadata-manager/app/viewsets/project.py @@ -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) diff --git a/lib/workload/stateless/stacks/metadata-manager/app/viewsets/sample.py b/lib/workload/stateless/stacks/metadata-manager/app/viewsets/sample.py index d3952cbf2..6d133258c 100644 --- a/lib/workload/stateless/stacks/metadata-manager/app/viewsets/sample.py +++ b/lib/workload/stateless/stacks/metadata-manager/app/viewsets/sample.py @@ -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) diff --git a/lib/workload/stateless/stacks/metadata-manager/app/viewsets/subject.py b/lib/workload/stateless/stacks/metadata-manager/app/viewsets/subject.py index 37a7aa8ca..fa252a42e 100644 --- a/lib/workload/stateless/stacks/metadata-manager/app/viewsets/subject.py +++ b/lib/workload/stateless/stacks/metadata-manager/app/viewsets/subject.py @@ -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: