From 9aa66e133a21370d6d95519f0e8701a734d8b002 Mon Sep 17 00:00:00 2001 From: Liana Harris <46411498+LianaHarris360@users.noreply.github.com> Date: Tue, 19 Nov 2024 09:34:59 -0600 Subject: [PATCH] Refactor NetworkLocationViewSet to return dynamic locations --- kolibri/core/discovery/api.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/kolibri/core/discovery/api.py b/kolibri/core/discovery/api.py index 8590dc1ff95..7025a60e1cc 100644 --- a/kolibri/core/discovery/api.py +++ b/kolibri/core/discovery/api.py @@ -1,4 +1,3 @@ -from django.db.models import Q from django_filters.rest_framework import DjangoFilterBackend from rest_framework import decorators from rest_framework import viewsets @@ -29,7 +28,6 @@ class NetworkLocationViewSet(viewsets.ModelViewSet): permission_classes = [NetworkLocationPermissions | NotProvisionedHasPermission] serializer_class = NetworkLocationSerializer - queryset = NetworkLocation.objects.exclude(location_type=LocationTypes.Reserved) filter_backends = [DjangoFilterBackend] filterset_fields = [ "id", @@ -39,23 +37,24 @@ class NetworkLocationViewSet(viewsets.ModelViewSet): def get_queryset(self): syncable = self.request.query_params.get("syncable", None) + base_queryset = NetworkLocation.objects.filter( + location_type__in=[LocationTypes.Static, LocationTypes.Dynamic] + ) + reserved_ids = [] if syncable == "1": # Include KDP's reserved location - queryset = NetworkLocation.objects.filter( - Q(location_type=LocationTypes.Static) - | Q(id=DATA_PORTAL_BASE_INSTANCE_ID) - ) + reserved_ids.append(DATA_PORTAL_BASE_INSTANCE_ID) elif syncable == "0": # Include Studio's reserved location - queryset = NetworkLocation.objects.filter( - Q(location_type=LocationTypes.Static) - | Q(id=CENTRAL_CONTENT_BASE_INSTANCE_ID) + reserved_ids.append(CENTRAL_CONTENT_BASE_INSTANCE_ID) + if reserved_ids: + reserved_queryset = NetworkLocation.objects.filter( + id__in=reserved_ids, ) + queryset = base_queryset | reserved_queryset else: - # Exclude both KDP and Studio - queryset = NetworkLocation.objects.filter( - location_type__in=[LocationTypes.Static, LocationTypes.Dynamic] - ) + # By default, exclude KDP/Studio reserved locations + queryset = base_queryset return queryset def get_object(self, id_filter=None):