Skip to content

Commit 5f08231

Browse files
refactor: Simplify create_next_unit_version() API
1 parent 236f7cc commit 5f08231

File tree

2 files changed

+21
-25
lines changed
  • openedx_learning/apps/authoring/units
  • tests/openedx_learning/apps/authoring/units

2 files changed

+21
-25
lines changed

openedx_learning/apps/authoring/units/api.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
from django.db.transaction import atomic
88

9-
from openedx_learning.apps.authoring.components.models import ComponentVersion
9+
from openedx_learning.apps.authoring.components.models import Component, ComponentVersion
1010
from openedx_learning.apps.authoring.containers.models import EntityListRow
1111
from ..publishing import api as publishing_api
1212
from ..containers import api as container_api
@@ -97,8 +97,7 @@ def create_unit_version(
9797
def create_next_unit_version(
9898
unit: Unit,
9999
title: str,
100-
publishable_entities_pks: list[int],
101-
entity_version_pks: list[int | None],
100+
components: list[Component | ComponentVersion],
102101
created: datetime,
103102
created_by: int | None = None,
104103
) -> Unit:
@@ -107,11 +106,21 @@ def create_next_unit_version(
107106
Args:
108107
unit_pk: The unit ID.
109108
title: The title.
110-
publishable_entities_pk: The components.
109+
components: The components, as a list of Components (unpinned) and/or ComponentVersions (pinned)
111110
entity: The entity.
112111
created: The creation date.
113112
created_by: The user who created the unit.
114113
"""
114+
for c in components:
115+
assert isinstance(c, (Component, ComponentVersion))
116+
publishable_entities_pks = [
117+
(c.publishable_entity_id if isinstance(c, Component) else c.component.publishable_entity_id)
118+
for c in components
119+
]
120+
entity_version_pks = [
121+
(cv.pk if isinstance(cv, ComponentVersion) else None)
122+
for cv in components
123+
]
115124
with atomic():
116125
# TODO: how can we enforce that publishable entities must be components?
117126
# This currently allows for any publishable entity to be added to a unit.

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

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,13 @@ def test_create_empty_unit_and_version(self):
5757
assert unit.versioning.published is None
5858

5959
def test_create_next_unit_version_with_two_components(self):
60-
"""Test creating a unit version with two components.
60+
"""Test creating a unit version with two unpinned components.
6161
6262
Expected results:
6363
1. A new unit version is created.
6464
2. The unit version number is 2.
6565
3. The unit version is in the unit's versions.
66-
4. The components are in the unit version's user defined list.
67-
5. Initial list contains the pinned versions of the defined list.
68-
6. Frozen list is empty.
66+
4. The components are in the draft unit version's component list and are unpinned.
6967
"""
7068
unit, unit_version = authoring_api.create_unit_and_version(
7169
learning_package_id=self.learning_package.id,
@@ -77,11 +75,7 @@ def test_create_next_unit_version_with_two_components(self):
7775
unit_version_v2 = authoring_api.create_next_unit_version(
7876
unit=unit,
7977
title="Unit",
80-
publishable_entities_pks=[
81-
self.component_1.publishable_entity.id,
82-
self.component_2.publishable_entity.id,
83-
],
84-
entity_version_pks=[None, None],
78+
components=[self.component_1, self.component_2],
8579
created=self.now,
8680
created_by=None,
8781
)
@@ -118,10 +112,7 @@ def test_add_component_after_publish(self):
118112
unit_version_v2 = authoring_api.create_next_unit_version(
119113
unit=unit,
120114
title=unit_version.title,
121-
publishable_entities_pks=[
122-
self.component_1.publishable_entity.id,
123-
],
124-
entity_version_pks=[None],
115+
components=[self.component_1],
125116
created=self.now,
126117
created_by=None,
127118
)
@@ -152,10 +143,7 @@ def test_modify_component_after_publish(self):
152143
unit_version_v2 = authoring_api.create_next_unit_version(
153144
unit=unit,
154145
title=unit_version.title,
155-
publishable_entities_pks=[
156-
self.component_1.publishable_entity.id,
157-
],
158-
entity_version_pks=[None],
146+
components=[self.component_1],
159147
created=self.now,
160148
created_by=None,
161149
)
@@ -216,7 +204,7 @@ def test_query_count_of_contains_unpublished_changes(self):
216204
)
217205
# Add 100 components (unpinned)
218206
component_count = 100
219-
publishable_entities_pks = []
207+
components = []
220208
for i in range(0, component_count):
221209
component, _version = authoring_api.create_component_and_version(
222210
self.learning_package.id,
@@ -225,12 +213,11 @@ def test_query_count_of_contains_unpublished_changes(self):
225213
title=f"Querying Counting Problem {i}",
226214
created=self.now,
227215
)
228-
publishable_entities_pks.append(component.publishable_entity_id)
216+
components.append(component)
229217
authoring_api.create_next_unit_version(
230218
unit=unit,
231219
title=unit_version.title,
232-
publishable_entities_pks=publishable_entities_pks,
233-
entity_version_pks=[None] * component_count,
220+
components=components,
234221
created=self.now,
235222
)
236223
authoring_api.publish_all_drafts(self.learning_package.id)

0 commit comments

Comments
 (0)