Skip to content

Commit

Permalink
CustomContent: Add descendantOf parameter to API
Browse files Browse the repository at this point in the history
This patch adds a new optional parameter to the getContentByFilter API
call to be able to retrieve all descendant nodes using the MPTT model
fields.

With this new parameter it's possible to filter all the nodes below a
specific topic node, useful when combining with other filters like kind
or tags.
  • Loading branch information
danigm committed Dec 3, 2021
1 parent 35665d0 commit a505798
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 0 deletions.
15 changes: 15 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,20 @@ 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

0 comments on commit a505798

Please sign in to comment.