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

CustomContent: Add withinDescendant parameter to API #8796

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
19 changes: 19 additions & 0 deletions kolibri/core/content/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ class ContentNodeFilter(IdFilter):
rght__lt = NumberFilter(field_name="rght", lookup_expr="lt")
authors = CharFilter(method="filter_by_authors")
tags = CharFilter(method="filter_by_tags")
descendant_of = UUIDFilter(method="filter_descendant_of")

class Meta:
model = models.ContentNode
Expand Down Expand Up @@ -299,6 +300,24 @@ def filter_by_tags(self, queryset, name, value):
tags = value.split(",")
return queryset.filter(tags__tag_name__in=tags).order_by("lft")

def filter_descendant_of(self, queryset, name, value):
"""
Show content that is descendant of the given node

:param queryset: all content nodes for this channel
:param value: the root node to filter descendant of
:return: all descendants content
"""
try:
node = models.ContentNode.objects.values("lft", "rght", "tree_id").get(
pk=value
)
except (models.ContentNode.DoesNotExist, ValueError):
return queryset.none()
return queryset.filter(
lft__gt=node["lft"], rght__lt=node["rght"], tree_id=node["tree_id"]
)

def filter_kind(self, queryset, name, value):
"""
Show only content of a given kind.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,7 @@
max_results: options.maxResults ? options.maxResults : 50,
kind: kind,
kind_in: kinds,
descendant_of: options.descendantOf,
},
})
.then(contentNodes => {
Expand Down
2 changes: 2 additions & 0 deletions packages/hashi/src/kolibri.js
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ export default class Kolibri extends BaseShim {
* an array of matching metadata
* @param {Object} options - The different options to filter by
* @param {string=} options.parent - id of the parent node to filter by, or 'self'
* @param {string=} options.descendantOf - id of the root node to filter
* by, to show all descendants, not just direct children
* @param {string[]} options.ids - an array of ids to filter by
* @param {number} [options.maxResults=50] - the maximum number of nodes per request
* @param {string[]} options.kinds - an array of kinds to filer by
Expand Down