Skip to content

Commit 16a9af8

Browse files
committed
Return channel info from channel version snapshot
1 parent d0d323a commit 16a9af8

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

contentcuration/kolibri_public/tests/test_public_v1_api.py

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,52 @@ def test_public_channel_lookup_channel_version_and_channel_tokens_have_same_keys
172172
set(channel_version_response.data[0].keys()),
173173
set(channel_response.data[0].keys()),
174174
)
175+
176+
def test_channel_version_token_returns_snapshot_info_not_current_channel_info(self):
177+
"""
178+
When a channel version token is used, the returned name, description, and
179+
thumbnail should come from the ChannelVersion snapshot captured at publish time,
180+
not from the channel's current (possibly updated) values.
181+
"""
182+
self.channel.main_tree.published = True
183+
self.channel.main_tree.save()
184+
185+
# Set the channel info BEFORE the ChannelVersion is created so that the
186+
# snapshot captures these values.
187+
self.channel.name = "Original Published Name"
188+
self.channel.description = "Original published description"
189+
self.channel.thumbnail_encoding = {"base64": generated_base64encoding()}
190+
self.channel.version = 3
191+
self.channel.published_data = {"3": {"version_notes": "v3 notes"}}
192+
self.channel.save()
193+
194+
# The ChannelVersion for version == channel.version is auto-created by
195+
# Channel.on_update(); re-fetch it to get the snapshot that was captured.
196+
channel_version = ChannelVersion.objects.get(channel=self.channel, version=3)
197+
version_token = channel_version.new_token().token
198+
199+
# Now mutate the channel's info AFTER the snapshot was taken.
200+
self.channel.name = "Updated Name — should NOT appear in response"
201+
self.channel.description = "Updated description — should NOT appear in response"
202+
self.channel.thumbnail_encoding = {"base64": "UPDATED_ENCODING"}
203+
self.channel.save()
204+
205+
lookup_url = reverse(
206+
"get_public_channel_lookup",
207+
kwargs={"version": "v1", "identifier": version_token},
208+
)
209+
response = self.client.get(lookup_url)
210+
211+
self.assertEqual(response.status_code, 200)
212+
self.assertEqual(len(response.data), 1)
213+
result = response.data[0]
214+
215+
# Values must match the snapshot, not the current channel state.
216+
self.assertEqual(result["name"], "Original Published Name")
217+
self.assertEqual(result["description"], "Original published description")
218+
self.assertEqual(result["icon_encoding"], generated_base64encoding())
219+
220+
# Sanity-check: confirm the channel itself now has the updated values.
221+
self.channel.refresh_from_db()
222+
self.assertNotEqual(result["name"], self.channel.name)
223+
self.assertNotEqual(result["description"], self.channel.description)

contentcuration/kolibri_public/views_v1.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
from contentcuration.decorators import cache_no_user_data
2222
from contentcuration.models import Channel
2323
from contentcuration.models import ChannelVersion
24-
from contentcuration.serializers import get_thumbnail_encoding
2524
from contentcuration.serializers import PublicChannelSerializer
2625

2726

@@ -40,6 +39,12 @@ def _get_version_notes(channel, channel_version):
4039
return OrderedDict(sorted(data.items()))
4140

4241

42+
def get_thumbnail_encoding(channel_version):
43+
if channel_version.channel_thumbnail_encoding:
44+
return channel_version.channel_thumbnail_encoding.get("base64")
45+
return None
46+
47+
4348
def _serialize_channel_version(channel_version_qs):
4449
channel_version = channel_version_qs.first()
4550
if not channel_version or not channel_version.channel:
@@ -48,12 +53,12 @@ def _serialize_channel_version(channel_version_qs):
4853
channel = channel_version.channel
4954
return [
5055
{
51-
"id": channel.id,
52-
"name": channel.name,
53-
"language": channel.language_id,
56+
"id": channel_version.channel_id,
57+
"name": channel_version.channel_name,
58+
"language": channel_version.channel_language_id,
5459
"public": channel.public,
55-
"description": channel.description,
56-
"icon_encoding": get_thumbnail_encoding(channel),
60+
"description": channel_version.channel_description,
61+
"icon_encoding": get_thumbnail_encoding(channel_version),
5762
"version_notes": _get_version_notes(channel, channel_version),
5863
"version": channel_version.version,
5964
"kind_count": channel_version.kind_count,

0 commit comments

Comments
 (0)