Skip to content

Commit b2b0558

Browse files
author
Andrii
committed
refactor: update child entities to new API + tests, fix some name & style issues
1 parent 8ffb425 commit b2b0558

File tree

5 files changed

+34
-31
lines changed

5 files changed

+34
-31
lines changed

openedx_learning/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
Open edX Learning ("Learning Core").
33
"""
44

5-
__version__ = "0.25.0"
5+
__version__ = "0.24.0"

openedx_learning/apps/authoring/sections/api.py

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,7 @@ def create_section_version(
6262
version_num: int,
6363
*,
6464
title: str,
65-
publishable_entities_pks: list[int],
66-
entity_version_pks: list[int | None],
65+
entity_rows: list[publishing_api.ContainerEntityRow],
6766
created: datetime,
6867
created_by: int | None = None,
6968
) -> SectionVersion:
@@ -78,17 +77,15 @@ def create_section_version(
7877
section_pk: The section ID.
7978
version_num: The version number.
8079
title: The title.
81-
publishable_entities_pk: The publishable entities.
82-
entity: The entity.
80+
entity_rows: child entities/versions
8381
created: The creation date.
8482
created_by: The user who created the section.
8583
"""
8684
return publishing_api.create_container_version(
8785
section.pk,
8886
version_num,
8987
title=title,
90-
publishable_entities_pks=publishable_entities_pks,
91-
entity_version_pks=entity_version_pks,
88+
entity_rows=entity_rows,
9289
created=created,
9390
created_by=created_by,
9491
container_version_cls=SectionVersion,
@@ -97,7 +94,7 @@ def create_section_version(
9794

9895
def _pub_entities_for_subsections(
9996
subsections: list[Subsection | SubsectionVersion] | None,
100-
) -> tuple[list[int], list[int | None]] | tuple[None, None]:
97+
) -> list[publishing_api.ContainerEntityRow] | None:
10198
"""
10299
Helper method: given a list of Subsection | SubsectionVersion, return the
103100
lists of publishable_entities_pks and entity_version_pks needed for the
@@ -108,19 +105,23 @@ def _pub_entities_for_subsections(
108105
"""
109106
if subsections is None:
110107
# When these are None, that means don't change the entities in the list.
111-
return None, None
108+
return None
112109
for u in subsections:
113110
if not isinstance(u, (Subsection, SubsectionVersion)):
114111
raise TypeError("Section subsections must be either Subsection or SubsectionVersion.")
115-
publishable_entities_pks = [
116-
(u.publishable_entity_id if isinstance(u, Subsection) else u.subsection.publishable_entity_id)
117-
for u in subsections
118-
]
119-
entity_version_pks = [
120-
(uv.pk if isinstance(uv, SubsectionVersion) else None)
121-
for uv in subsections
112+
return [
113+
(
114+
publishing_api.ContainerEntityRow(
115+
entity_pk=s.container.publishable_entity_id,
116+
version_pk=None,
117+
) if isinstance(s, Subsection)
118+
else publishing_api.ContainerEntityRow(
119+
entity_pk=s.subsection.container.publishable_entity_id,
120+
version_pk=s.container_version.publishable_entity_version_id,
121+
)
122+
)
123+
for s in subsections
122124
]
123-
return publishable_entities_pks, entity_version_pks
124125

125126

126127
def create_next_section_version(
@@ -138,17 +139,16 @@ def create_next_section_version(
138139
Args:
139140
section_pk: The section ID.
140141
title: The title. Leave as None to keep the current title.
141-
subsections: The subsections, as a list of Subsections (unpinned) and/or SubsectionVersions (pinned). Passing None
142-
will leave the existing subsections unchanged.
142+
subsections: The subsections, as a list of Subsections (unpinned) and/or SubsectionVersions (pinned).
143+
Passing None will leave the existing subsections unchanged.
143144
created: The creation date.
144145
created_by: The user who created the section.
145146
"""
146-
publishable_entities_pks, entity_version_pks = _pub_entities_for_subsections(subsections)
147+
entity_rows = _pub_entities_for_subsections(subsections)
147148
section_version = publishing_api.create_next_container_version(
148149
section.pk,
149150
title=title,
150-
publishable_entities_pks=publishable_entities_pks,
151-
entity_version_pks=entity_version_pks,
151+
entity_rows=entity_rows,
152152
created=created,
153153
created_by=created_by,
154154
container_version_cls=SectionVersion,
@@ -177,7 +177,7 @@ def create_section_and_version(
177177
created_by: The user who created the section.
178178
can_stand_alone: Set to False when created as part of containers
179179
"""
180-
publishable_entities_pks, entity_version_pks = _pub_entities_for_subsections(subsections)
180+
entity_rows = _pub_entities_for_subsections(subsections)
181181
with atomic():
182182
section = create_section(
183183
learning_package_id,
@@ -190,8 +190,7 @@ def create_section_and_version(
190190
section,
191191
1,
192192
title=title,
193-
publishable_entities_pks=publishable_entities_pks or [],
194-
entity_version_pks=entity_version_pks or [],
193+
entity_rows=entity_rows or [],
195194
created=created,
196195
created_by=created_by,
197196
)
@@ -286,7 +285,9 @@ def get_subsections_in_published_section_as_of(
286285
ancestors of every modified PublishableEntity in the publish.
287286
"""
288287
assert isinstance(section, Section)
289-
section_pub_entity_version = publishing_api.get_published_version_as_of(section.publishable_entity_id, publish_log_id)
288+
section_pub_entity_version = publishing_api.get_published_version_as_of(
289+
section.publishable_entity_id, publish_log_id
290+
)
290291
if section_pub_entity_version is None:
291292
return None # This section was not published as of the given PublishLog ID.
292293
container_version = section_pub_entity_version.containerversion
@@ -303,5 +304,7 @@ def get_subsections_in_published_section_as_of(
303304
# This is not optimized. It could be done in one query per section rather than one query per subsection.
304305
pub_entity_version = publishing_api.get_published_version_as_of(row.entity_id, publish_log_id)
305306
if pub_entity_version:
306-
entity_list.append(SectionListEntry(subsection_version=pub_entity_version.containerversion.subsectionversion, pinned=False))
307+
entity_list.append(SectionListEntry(
308+
subsection_version=pub_entity_version.containerversion.subsectionversion, pinned=False
309+
))
307310
return entity_list

openedx_learning/apps/authoring/sections/migrations/0001_initial.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Generated by Django 4.2.20 on 2025-04-11 12:53
22

3-
from django.db import migrations, models
43
import django.db.models.deletion
4+
from django.db import migrations, models
55

66

77
class Migration(migrations.Migration):

openedx_learning/apps/authoring/sections/models.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
Models that implement units
2+
Models that implement subsections
33
"""
44
from django.db import models
55

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515

1616
@ddt.ddt
17-
class SectionTestCase(SubSectionTestCase):
17+
class SectionTestCase(SubSectionTestCase): # pylint: disable=test-inherits-tests
1818
""" Test cases for Sections (containers of subsections) """
1919

2020
def setUp(self) -> None:
@@ -185,7 +185,7 @@ def test_create_section_queries(self):
185185
# The exact numbers here aren't too important - this is just to alert us if anything significant changes.
186186
with self.assertNumQueries(22):
187187
_empty_section = self.create_section_with_subsections([])
188-
with self.assertNumQueries(26):
188+
with self.assertNumQueries(27):
189189
# And try with a non-empty section:
190190
self.create_section_with_subsections([self.subsection_1, self.subsection_2_v1], key="u2")
191191

0 commit comments

Comments
 (0)