Skip to content

Commit

Permalink
Merge pull request #9539 from rtibbles/no_500
Browse files Browse the repository at this point in the history
Check node being available on filtered queryset to prevent index error.
  • Loading branch information
marcellamaki authored Jul 8, 2022
2 parents 953d32a + d7d6bba commit 7b68f64
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
8 changes: 6 additions & 2 deletions kolibri/core/content/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -969,8 +969,12 @@ def get_grandchild_ids(self, child_ids, depth, page_size):

def get_tree_queryset(self, request, pk):
# Get the model for the parent node here - we do this so that we trigger a 404 immediately if the node
# does not exist (or exists but is not available).
parent_id = pk if pk and self.get_queryset().filter(id=pk).exists() else None
# does not exist (or exists but is not available, or is filtered).
parent_id = (
pk
if pk and self.filter_queryset(self.get_queryset()).filter(id=pk).exists()
else None
)

if parent_id is None:
raise Http404
Expand Down
8 changes: 8 additions & 0 deletions kolibri/core/content/test/test_content_app.py
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,14 @@ def test_contentnode_tree(self):
)
self._recurse_and_assert([response.data], [root])

def test_contentnode_tree_filtered_queryset_node(self):
root = content.ContentNode.objects.get(title="root")
response = self.client.get(
reverse("kolibri:core:contentnode_tree-detail", kwargs={"pk": root.id})
+ "?parent={}".format(uuid.uuid4().hex)
)
self.assertEqual(response.status_code, 404)

@unittest.skipIf(
getattr(settings, "DATABASES")["default"]["ENGINE"]
== "django.db.backends.postgresql",
Expand Down

0 comments on commit 7b68f64

Please sign in to comment.