Skip to content

Commit 3ec299a

Browse files
authored
Merge pull request #5853 from rtibblesbot/issue-5852-efe933
fix: write version=0 in ChannelMetadata for draft publishes
2 parents d9dbadb + 8e58eb2 commit 3ec299a

2 files changed

Lines changed: 52 additions & 4 deletions

File tree

contentcuration/contentcuration/tests/utils/test_publish.py

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
from unittest import mock
55

66
from django.conf import settings
7+
from kolibri_content import models as kolibrimodels
8+
from kolibri_content.router import using_content_database
79
from le_utils.constants import licenses
810

911
import contentcuration.models as ccmodels
@@ -12,6 +14,7 @@
1214
from contentcuration.tests.utils.restricted_filesystemstorage import (
1315
RestrictedFileSystemStorage,
1416
)
17+
from contentcuration.utils.publish import create_content_database
1518
from contentcuration.utils.publish import create_draft_channel_version
1619
from contentcuration.utils.publish import ensure_versioned_database_exists
1720
from contentcuration.utils.publish import increment_channel_version
@@ -439,3 +442,45 @@ def test_second_draft_publish_refreshes_channel_snapshot_fields(self):
439442
draft_version.channel_thumbnail_encoding, {"base64": "new_thumb"}
440443
)
441444
self.assertEqual(draft_version.channel_language_id, "fr")
445+
446+
447+
class MapChannelToKolibriChannelTestCase(StudioTestCase):
448+
def setUp(self):
449+
super().setUp()
450+
self.channel = testdata.channel()
451+
# icon_encoding must not be None — the kolibri content DB thumbnail column is NOT NULL.
452+
# publish_channel calls set_channel_icon_encoding before create_content_database;
453+
# since we call create_content_database directly we set it here instead.
454+
self.channel.icon_encoding = ""
455+
self.channel.save()
456+
# increment_channel_version + refresh_from_db gives channel.version > 0,
457+
# so channel.version + 1 is distinguishable from the draft value of 0.
458+
increment_channel_version(self.channel)
459+
self.channel.refresh_from_db()
460+
461+
def _get_channel_metadata_version(self, is_draft_version):
462+
with mock.patch("contentcuration.utils.publish.save_export_database"):
463+
tempdb = create_content_database(
464+
self.channel,
465+
force=True,
466+
user_id=self.admin_user.id,
467+
force_exercises=False,
468+
is_draft_version=is_draft_version,
469+
)
470+
try:
471+
with using_content_database(tempdb):
472+
return kolibrimodels.ChannelMetadata.objects.get(
473+
id=self.channel.id
474+
).version
475+
finally:
476+
if os.path.exists(tempdb):
477+
os.remove(tempdb)
478+
479+
def test_draft_publish_sets_version_zero(self):
480+
self.assertEqual(self._get_channel_metadata_version(is_draft_version=True), 0)
481+
482+
def test_non_draft_publish_sets_version_plus_one(self):
483+
self.assertEqual(
484+
self._get_channel_metadata_version(is_draft_version=False),
485+
self.channel.version + 1,
486+
)

contentcuration/contentcuration/utils/publish.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,9 @@ def create_content_database(
183183
inherit_metadata=bool(channel.ricecooker_version),
184184
)
185185
tree_mapper.map_nodes()
186-
kolibri_channel = map_channel_to_kolibri_channel(channel, use_staging_tree)
186+
kolibri_channel = map_channel_to_kolibri_channel(
187+
channel, use_staging_tree, is_draft_version=is_draft_version
188+
)
187189
# It should be at this percent already, but just in case.
188190
if progress_tracker:
189191
progress_tracker.track(90)
@@ -796,16 +798,17 @@ def map_prerequisites(root_node):
796798
)
797799

798800

799-
def map_channel_to_kolibri_channel(channel, use_staging_tree=False):
801+
def map_channel_to_kolibri_channel(
802+
channel, use_staging_tree=False, is_draft_version=False
803+
):
800804
logging.debug("Generating the channel metadata.")
801805
base_tree = channel.staging_tree if use_staging_tree else channel.main_tree
802806
kolibri_channel = kolibrimodels.ChannelMetadata.objects.create(
803807
id=channel.id,
804808
name=channel.name,
805809
description=channel.description,
806810
tagline=channel.tagline,
807-
version=channel.version
808-
+ 1, # Need to save as version being published, not current version
811+
version=0 if is_draft_version else channel.version + 1,
809812
thumbnail=channel.icon_encoding,
810813
root_pk=base_tree.node_id,
811814
root_id=base_tree.node_id,

0 commit comments

Comments
 (0)