Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ def handle(self, *args, **options): # noqa: C901
"non_distributable_licenses_included"
),
"kind_count": pub_data.get("kind_count"),
"size": int(channel.published_size),
"resource_count": channel.total_resource_count,
"size": int(pub_data.get("size", 0)),
"resource_count": int(pub_data.get("resource_count", 0)),
},
)

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Generated by Django 3.2.24 on 2026-03-24 18:10
import django.db.models.deletion
from django.db import migrations
from django.db import models


class Migration(migrations.Migration):

dependencies = [
("contentcuration", "0163_merge_20260320_1809"),
]

operations = [
migrations.AddField(
model_name="channelversion",
name="channel_description",
field=models.CharField(blank=True, max_length=400, null=True),
),
migrations.AddField(
model_name="channelversion",
name="channel_language",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
related_name="+",
to="contentcuration.language",
),
),
migrations.AddField(
model_name="channelversion",
name="channel_name",
field=models.CharField(blank=True, max_length=200, null=True),
),
migrations.AddField(
model_name="channelversion",
name="channel_tagline",
field=models.CharField(blank=True, max_length=150, null=True),
),
migrations.AddField(
model_name="channelversion",
name="channel_thumbnail_encoding",
field=models.JSONField(blank=True, default=dict),
),
]
25 changes: 24 additions & 1 deletion contentcuration/contentcuration/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1607,14 +1607,37 @@ class ChannelVersion(models.Model):
blank=True,
)

# Snapshot of the channel info at the time of creation.
channel_name = models.CharField(max_length=200, blank=True, null=True)
channel_description = models.CharField(max_length=400, blank=True, null=True)
channel_tagline = models.CharField(max_length=150, blank=True, null=True)
channel_thumbnail_encoding = JSONField(default=dict, blank=True)
channel_language = models.ForeignKey(
"Language",
null=True,
blank=True,
related_name="+",
on_delete=models.SET_NULL,
)

class Meta:
unique_together = ("channel", "version")

def save(self, *args, **kwargs):
if self.version is not None and self.version > self.channel.version:
raise ValidationError("Version cannot be greater than channel version")

if self._state.adding and self.version == self.channel.version:
# When creating a new ChannelVersion for current channel version,
# snapshot the current channel info
self.channel_name = self.channel.name
self.channel_description = self.channel.description
self.channel_tagline = self.channel.tagline
self.channel_thumbnail_encoding = self.channel.thumbnail_encoding
self.channel_language = self.channel.language

self.full_clean()
super(ChannelVersion, self).save(*args, **kwargs)
super().save(*args, **kwargs)

def new_token(self):
if not self.secret_token:
Expand Down
30 changes: 17 additions & 13 deletions contentcuration/contentcuration/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@ def no_field_eval_repr(self):
serializers.ModelSerializer.__repr__ = no_field_eval_repr


def get_thumbnail_encoding(channel):
"""
Historically, we did not set channel.icon_encoding in the Studio database. We
only set it in the exported Kolibri sqlite db. So when Kolibri asks for the channel
information, fall back to the channel thumbnail data if icon_encoding is not set.
"""
if channel.icon_encoding:
return channel.icon_encoding
if channel.thumbnail_encoding:
base64 = channel.thumbnail_encoding.get("base64")
if base64:
return base64

return None


class PublicChannelSerializer(serializers.ModelSerializer):
"""
Called by the public API, primarily used by Kolibri. Contains information more specific to Kolibri's needs.
Expand All @@ -41,19 +57,7 @@ def match_tokens(self, channel):
)

def get_thumbnail_encoding(self, channel):
"""
Historically, we did not set channel.icon_encoding in the Studio database. We
only set it in the exported Kolibri sqlite db. So when Kolibri asks for the channel
information, fall back to the channel thumbnail data if icon_encoding is not set.
"""
if channel.icon_encoding:
return channel.icon_encoding
if channel.thumbnail_encoding:
base64 = channel.thumbnail_encoding.get("base64")
if base64:
return base64

return None
return get_thumbnail_encoding(channel)

def generate_kind_count(self, channel):
return channel.published_kind_count and json.loads(channel.published_kind_count)
Expand Down
103 changes: 103 additions & 0 deletions contentcuration/contentcuration/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
from contentcuration.models import FlagFeedbackEvent
from contentcuration.models import generate_object_storage_name
from contentcuration.models import Invitation
from contentcuration.models import Language
from contentcuration.models import License
from contentcuration.models import object_storage_name
from contentcuration.models import RecommendationsEvent
Expand Down Expand Up @@ -1814,3 +1815,105 @@ def test_version_cannot_exceed_channel_version(self):
self.assertIn(
"Version cannot be greater than channel version", str(context.exception)
)

def test_save_snapshots_channel_info_when_version_matches_channel_version(self):
"""Creating a ChannelVersion whose version equals channel.version should
automatically snapshot the channel's current name, description, tagline,
thumbnail_encoding, and language."""
lang = Language.objects.first()
# Use a queryset update to set channel fields without triggering on_update
# (which would call get_or_create and collide with the ChannelVersion we're
# about to create ourselves).
Channel.objects.filter(id=self.channel.id).update(
name="Snapshot Channel",
description="A channel to snapshot",
tagline="Learn something new",
thumbnail_encoding={"base64": "abc123"},
language=lang,
)
self.channel.refresh_from_db()

# setUp's self.channel.save() already auto-created a ChannelVersion for
# version 10 via on_update. Delete it so we can create a fresh one and
# observe the snapshot logic.
ChannelVersion.objects.filter(
channel=self.channel, version=self.channel.version
).delete()

cv = ChannelVersion(
channel=self.channel,
version=self.channel.version,
)
cv.save()

cv.refresh_from_db()
self.assertEqual(cv.channel_name, self.channel.name)
self.assertEqual(cv.channel_description, self.channel.description)
self.assertEqual(cv.channel_tagline, self.channel.tagline)
self.assertEqual(cv.channel_thumbnail_encoding, self.channel.thumbnail_encoding)
self.assertEqual(cv.channel_language, self.channel.language)

def test_save_does_not_snapshot_when_version_differs_from_channel_version(self):
"""Creating a ChannelVersion for an older version should NOT populate
the snapshot fields automatically."""
self.channel.name = "Current Name"
self.channel.save()

# version 5 is less than channel.version (10)
cv = ChannelVersion(
channel=self.channel,
version=5,
)
cv.save()

cv.refresh_from_db()
self.assertIsNone(cv.channel_name)
self.assertIsNone(cv.channel_description)
self.assertIsNone(cv.channel_tagline)
self.assertIsNone(cv.channel_language)

def test_save_does_not_re_snapshot_on_update(self):
"""Updating an existing ChannelVersion (not adding) should NOT overwrite
the snapshot fields even if the channel info has changed."""
# setUp's self.channel.save() already created a ChannelVersion for version 10
# via on_update -> get_or_create. Reuse that existing object so we're
# testing a genuine update (not insert) path.
cv = ChannelVersion.objects.get(
channel=self.channel, version=self.channel.version
)
original_name = cv.channel_name

# Change the channel name via a queryset update so on_update is not called
# (avoiding a second get_or_create for the same version).
Channel.objects.filter(id=self.channel.id).update(name="Updated Channel Name")
self.channel.refresh_from_db()

cv.version_notes = "some notes"
cv.save()

cv.refresh_from_db()
# The snapshot should still reflect the name captured when cv was first created.
self.assertEqual(cv.channel_name, original_name)
self.assertNotEqual(cv.channel_name, "Updated Channel Name")

def test_save_snapshots_null_language_when_channel_has_no_language(self):
"""When the channel has no language set, channel_language on the snapshot
should remain None."""
# Ensure no language on the channel via queryset update (bypasses on_update).
Channel.objects.filter(id=self.channel.id).update(language=None)
self.channel.refresh_from_db()

# Delete the ChannelVersion auto-created during setUp so we can insert a
# fresh one and observe the snapshot logic.
ChannelVersion.objects.filter(
channel=self.channel, version=self.channel.version
).delete()

cv = ChannelVersion(
channel=self.channel,
version=self.channel.version,
)
cv.save()

cv.refresh_from_db()
self.assertIsNone(cv.channel_language)
Loading
Loading