Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

filter SoUD devices when scanning the network to import new facilities #8383

Merged
merged 2 commits into from
Sep 3, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
70 changes: 37 additions & 33 deletions kolibri/core/assets/src/views/sync/SelectAddressModalGroup/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,26 +48,28 @@ function fetchAddressesForLOD(LocationResource = NetworkLocationResource) {
}

function fetchAddressesWithChannel(withChannelId = '', LocationResource = NetworkLocationResource) {
return LocationResource.fetchCollection({ force: true }).then(locations => {
rtibbles marked this conversation as resolved.
Show resolved Hide resolved
// If channelId is provided, then we are in an 'import-more' workflow and disable
// locations that do not have the channel we are importing from.
if (withChannelId !== '') {
const locationsWithAvailabilityPromises = locations.map(location => {
// Need to wrap in normal promise, otherwise Promise.all will cause some of these
// to resolve as undefined
return new Promise(resolve => {
return channelIsAvailableAtLocation(withChannelId, location).then(isAvailable => {
resolve({ ...location, hasContent: isAvailable });
return LocationResource.fetchCollection({ force: true, getParams: { filterSoUD: true } }).then(
locations => {
// If channelId is provided, then we are in an 'import-more' workflow and disable
// locations that do not have the channel we are importing from.
if (withChannelId !== '') {
const locationsWithAvailabilityPromises = locations.map(location => {
// Need to wrap in normal promise, otherwise Promise.all will cause some of these
// to resolve as undefined
return new Promise(resolve => {
return channelIsAvailableAtLocation(withChannelId, location).then(isAvailable => {
resolve({ ...location, hasContent: isAvailable });
});
});
});
});
return Promise.all(locationsWithAvailabilityPromises);
}
return Promise.all(locationsWithAvailabilityPromises);
}

// If channelId is not provided, then we are at top-level import workflow and do not
// disable any locations unless it is unavailable
return locations.map(location => ({ ...location, hasContent: location.available }));
});
// If channelId is not provided, then we are at top-level import workflow and do not
// disable any locations unless it is unavailable
return locations.map(location => ({ ...location, hasContent: location.available }));
}
);
}

function facilityIsAvailableAtLocation(facilityId, location) {
Expand All @@ -83,25 +85,27 @@ function facilityIsAvailableAtLocation(facilityId, location) {
}

function fetchAddressesWithFacility(facilityId = '', LocationResource = NetworkLocationResource) {
return LocationResource.fetchCollection({ force: true }).then(locations => {
if (facilityId !== '') {
const locationsWithAvailabilityPromises = locations.map(location => {
// Need to wrap in normal promise, otherwise Promise.all will cause some of these
// to resolve as undefined
return new Promise(resolve => {
return facilityIsAvailableAtLocation(facilityId, location).then(isAvailable => {
// NOTE: we're reusing 'hasContent' for both the facility/content cases for now
resolve({ ...location, hasContent: isAvailable });
return LocationResource.fetchCollection({ force: true, getParams: { filterSoUD: true } }).then(
locations => {
if (facilityId !== '') {
const locationsWithAvailabilityPromises = locations.map(location => {
// Need to wrap in normal promise, otherwise Promise.all will cause some of these
// to resolve as undefined
return new Promise(resolve => {
return facilityIsAvailableAtLocation(facilityId, location).then(isAvailable => {
// NOTE: we're reusing 'hasContent' for both the facility/content cases for now
resolve({ ...location, hasContent: isAvailable });
});
});
});
});
return Promise.all(locationsWithAvailabilityPromises);
}
return Promise.all(locationsWithAvailabilityPromises);
}

// If facilityId is not provided, then we are at the initial Facility Import workflow
// disable any locations unless it is unavailable/offline
return locations.map(location => ({ ...location, hasContent: location.available }));
});
// If facilityId is not provided, then we are at the initial Facility Import workflow
// disable any locations unless it is unavailable/offline
return locations.map(location => ({ ...location, hasContent: location.available }));
}
);
}

export function fetchStaticAddresses(args) {
Expand Down
10 changes: 10 additions & 0 deletions kolibri/core/discovery/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ class NetworkLocationViewSet(viewsets.ModelViewSet):
serializer_class = NetworkLocationSerializer
queryset = NetworkLocation.objects.all()

def filter_queryset(self, queryset):
rtibbles marked this conversation as resolved.
Show resolved Hide resolved
if (
self.request.method == "GET"
and self.request.resolver_match.url_name.endswith("-list")
):
# only filter down the queryset in the case of the list view being requested
if self.request.query_params.get("filterSoUD", False):
return queryset.filter(subset_of_users_device=False)
return queryset


class DynamicNetworkLocationViewSet(NetworkLocationViewSet):
queryset = DynamicNetworkLocation.objects.all()
Expand Down