Skip to content

Commit

Permalink
Add methods to calculate ordered categories and grade_levels
Browse files Browse the repository at this point in the history
  • Loading branch information
jredrejo committed Dec 13, 2024
1 parent 65743e2 commit 8aab3ff
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions kolibri/core/content/utils/annotation.py
Original file line number Diff line number Diff line change
Expand Up @@ -774,6 +774,8 @@ def set_channel_metadata_fields(channel_id, public=None):
calculate_published_size(channel)
calculate_total_resource_count(channel)
calculate_included_languages(channel)
calculate_ordered_categories(channel)
calculate_ordered_grade_levels(channel)
calculate_next_order(channel)

if public is not None:
Expand Down Expand Up @@ -811,6 +813,38 @@ def calculate_total_resource_count(channel):
channel.save()


def _calculate_ordered_field_values(channel, field_name):
content_nodes = ContentNode.objects.filter(
channel_id=channel.id, available=True
).exclude(**{field_name: None})
all_values = []
for node in content_nodes.values_list(field_name, flat=True):
if node: # just in case some field is an empty string
all_values.extend(node.split(","))

value_counts = {}
for value in all_values:
value_counts[value] = value_counts.get(value, 0) + 1

return sorted(value_counts.keys(), key=lambda x: value_counts[x], reverse=True)


def calculate_ordered_categories(channel):
ordered_categories = _calculate_ordered_field_values(channel, "categories")
channel.included_categories = (
",".join(ordered_categories) if ordered_categories else None
)
channel.save()


def calculate_ordered_grade_levels(channel):
ordered_grade_levels = _calculate_ordered_field_values(channel, "grade_levels")
channel.included_grade_levels = (
",".join(ordered_grade_levels) if ordered_grade_levels else None
)
channel.save()


def calculate_included_languages(channel):
content_nodes = ContentNode.objects.filter(
channel_id=channel.id, available=True
Expand Down

0 comments on commit 8aab3ff

Please sign in to comment.