Skip to content

Commit 9a4a20e

Browse files
feat: auto-publish children when publishing a container
1 parent 64bd03d commit 9a4a20e

4 files changed

Lines changed: 28 additions & 10 deletions

File tree

openedx_learning/apps/authoring/publishing/api.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,30 @@ def publish_from_drafts(
332332
published_at = datetime.now(tz=timezone.utc)
333333

334334
with atomic():
335+
# If the drafts include any containers, we need to auto-publish their descendants:
336+
# TODO: this only handles one level deep and would need to be updated to support sections > subsections > units
337+
338+
# Get the IDs of the ContainerVersion for any Containers whose drafts are slated to be published.
339+
container_version_ids = (
340+
Container.objects.filter(publishable_entity__draft__in=draft_qset)
341+
.values_list("publishable_entity__draft__version__containerversion__pk", flat=True)
342+
)
343+
if container_version_ids:
344+
# We are publishing at least one container. Check if it has any child components that aren't already slated
345+
# to be published.
346+
unpublished_draft_children = EntityListRow.objects.filter(
347+
entity_list__container_versions__pk__in=container_version_ids,
348+
entity_version=None, # Unpinned entities only
349+
).exclude(
350+
entity__draft__version=F("entity__published__version") # Exclude already published things
351+
).values_list("entity__draft__pk", flat=True)
352+
if unpublished_draft_children:
353+
# Force these additional child components to be published at the same time by adding them to the qset:
354+
draft_qset=Draft.objects.filter(
355+
Q(pk__in=draft_qset.values_list("pk", flat=True)) |
356+
Q(pk__in=unpublished_draft_children)
357+
)
358+
335359
# One PublishLog for this entire publish operation.
336360
publish_log = PublishLog(
337361
learning_package_id=learning_package_id,

openedx_learning/apps/authoring/publishing/migrations/0003_containers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class Migration(migrations.Migration):
4141
fields=[
4242
('publishable_entity_version', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to='oel_publishing.publishableentityversion')),
4343
('container', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='versions', to='oel_publishing.container')),
44-
('entity_list', models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, related_name='entity_list', to='oel_publishing.entitylist')),
44+
('entity_list', models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, related_name='container_versions', to='oel_publishing.entitylist')),
4545
],
4646
options={
4747
'abstract': False,

openedx_learning/apps/authoring/publishing/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -627,5 +627,5 @@ class ContainerVersion(PublishableEntityVersionMixin):
627627
EntityList,
628628
on_delete=models.RESTRICT,
629629
null=False,
630-
related_name="entity_list",
630+
related_name="container_versions",
631631
)

tests/openedx_learning/apps/authoring/units/test_api.py

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ def test_create_next_unit_version_with_unpinned_and_pinned_components(self):
234234
]
235235
assert authoring_api.get_components_in_published_unit(unit) is None
236236

237-
@pytest.mark.skip(reason="FIXME: auto-publishing children is not implemented yet")
238237
def test_auto_publish_children(self):
239238
"""
240239
Test that publishing a unit publishes its child components automatically.
@@ -486,16 +485,11 @@ def test_publishing_shared_component(self):
486485
c5_v2 = self.modify_component(c5, title="C5 version 2")
487486

488487
# 4️⃣ The author then publishes Unit 1, and therefore everything in it.
489-
# FIXME: this should only require publishing the unit itself, but we don't yet do auto-publishing children
490488
authoring_api.publish_from_drafts(
491489
self.learning_package.pk,
492490
draft_qset=authoring_api.get_all_drafts(self.learning_package.pk).filter(
493-
entity_id__in=[
494-
unit1.publishable_entity.id,
495-
c1.publishable_entity.id,
496-
c2.publishable_entity.id,
497-
c3.publishable_entity.id,
498-
],
491+
# Note: we only publish the unit; the publishing API should auto-publish its components too.
492+
entity_id=unit1.publishable_entity.id,
499493
),
500494
)
501495

0 commit comments

Comments
 (0)