Skip to content

Commit 7bd6d34

Browse files
rtibblesbotclaude
andcommitted
fix: snapshot channel fields on draft ChannelVersion in save()
Loosen the snapshot condition in ChannelVersion.save() to also fire when version is None (draft rows), so every draft publish captures the current channel name, description, thumbnail encoding, tagline, and language on the draft ChannelVersion. Repeated draft publishes always reflect the latest channel state. Tests cover: first draft publish populates all four snapshot fields, and a second draft publish after mutating channel metadata (including language change from "en" to "fr") refreshes all four fields on the existing row. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 14f4929 commit 7bd6d34

2 files changed

Lines changed: 66 additions & 3 deletions

File tree

contentcuration/contentcuration/models.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1671,9 +1671,12 @@ def save(self, *args, **kwargs):
16711671
if self.version is not None and self.version > self.channel.version:
16721672
raise ValidationError("Version cannot be greater than channel version")
16731673

1674-
if self._state.adding and self.version == self.channel.version:
1675-
# When creating a new ChannelVersion for current channel version,
1676-
# snapshot the current channel info
1674+
if self.version is None or (
1675+
self._state.adding and self.version == self.channel.version
1676+
):
1677+
# Snapshot channel info when creating a versioned ChannelVersion for the
1678+
# current channel version, or on every save for draft (version=None) rows
1679+
# so that repeated draft publishes always reflect the latest channel state.
16771680
self.channel_name = self.channel.name
16781681
self.channel_description = self.channel.description
16791682
self.channel_tagline = self.channel.tagline

contentcuration/contentcuration/tests/utils/test_publish.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,63 @@ def test_special_permissions_distributable_false_for_draft_publish(self):
379379
license_obj.distributable,
380380
"distributable must stay False for draft publishes",
381381
)
382+
383+
def test_draft_publish_populates_channel_snapshot_fields(self):
384+
"""
385+
After the first draft publish, channel_name, channel_description,
386+
channel_thumbnail_encoding, and channel_language on the draft
387+
ChannelVersion reflect the channel's state at publish time.
388+
"""
389+
self.channel.name = "Snapshot Channel"
390+
self.channel.description = "Snapshot description"
391+
self.channel.thumbnail_encoding = {"base64": "abc123"}
392+
self.channel.save()
393+
394+
self._run_draft_publish()
395+
draft_version = self._get_draft_version()
396+
397+
self.assertEqual(draft_version.channel_name, "Snapshot Channel")
398+
self.assertEqual(draft_version.channel_description, "Snapshot description")
399+
self.assertEqual(draft_version.channel_thumbnail_encoding, {"base64": "abc123"})
400+
self.assertEqual(draft_version.channel_language_id, self.channel.language_id)
401+
402+
def test_second_draft_publish_refreshes_channel_snapshot_fields(self):
403+
"""
404+
A second draft publish after mutating channel metadata must overwrite
405+
the stale snapshot values on the existing draft ChannelVersion row —
406+
for all four snapshot fields.
407+
408+
channel.language is changed from "en" to "fr" to confirm the refresh
409+
is not vacuous (the language snapshot must change to the new value).
410+
"""
411+
self.channel.name = "Old Name"
412+
self.channel.description = "Old description"
413+
self.channel.thumbnail_encoding = {"base64": "old_thumb"}
414+
# language is already "en" from testdata.channel(); no need to set it
415+
self.channel.save()
416+
417+
self._run_draft_publish()
418+
draft_version = self._get_draft_version()
419+
420+
self.assertEqual(draft_version.channel_name, "Old Name")
421+
self.assertEqual(
422+
draft_version.channel_thumbnail_encoding, {"base64": "old_thumb"}
423+
)
424+
self.assertEqual(draft_version.channel_language_id, "en")
425+
426+
# Mutate all four fields; language changes from "en" to "fr"
427+
self.channel.name = "New Name"
428+
self.channel.description = "New description"
429+
self.channel.thumbnail_encoding = {"base64": "new_thumb"}
430+
self.channel.language_id = "fr"
431+
self.channel.save()
432+
433+
self._run_draft_publish()
434+
draft_version.refresh_from_db()
435+
436+
self.assertEqual(draft_version.channel_name, "New Name")
437+
self.assertEqual(draft_version.channel_description, "New description")
438+
self.assertEqual(
439+
draft_version.channel_thumbnail_encoding, {"base64": "new_thumb"}
440+
)
441+
self.assertEqual(draft_version.channel_language_id, "fr")

0 commit comments

Comments
 (0)