Skip to content

Commit 60fdb44

Browse files
refactor: use container API to get unit members
1 parent 0efb913 commit 60fdb44

3 files changed

Lines changed: 30 additions & 22 deletions

File tree

openedx_learning/apps/authoring/containers/models.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
from django.db import models
22

3-
from openedx_learning.apps.authoring.publishing.models import PublishableEntity, PublishableEntityVersion
4-
from ..publishing.model_mixins import PublishableEntityMixin, PublishableEntityVersionMixin
3+
from openedx_learning.apps.authoring.publishing.models import (
4+
PublishableEntity,
5+
PublishableEntityVersion,
6+
)
7+
from ..publishing.model_mixins import (
8+
PublishableEntityMixin,
9+
PublishableEntityVersionMixin,
10+
)
511

612

713
class EntityList(models.Model):
@@ -14,6 +20,7 @@ class EntityList(models.Model):
1420
anonymous in a sense–they're pointed to by ContainerEntityVersions and
1521
other models, rather than being looked up by their own identifers.
1622
"""
23+
1724
pass
1825

1926

@@ -25,6 +32,7 @@ class EntityListRow(models.Model):
2532
There is a row in this table for each member of an EntityList. The order_num
2633
field is used to determine the order of the members in the list.
2734
"""
35+
2836
entity_list = models.ForeignKey(EntityList, on_delete=models.CASCADE)
2937

3038
# This ordering should be treated as immutable–if the ordering needs to
@@ -70,6 +78,7 @@ class ContainerEntity(PublishableEntityMixin):
7078
PublishLog and Containers that were affected in a publish because their
7179
child elements were published.
7280
"""
81+
7382
pass
7483

7584

@@ -93,6 +102,7 @@ class ContainerEntityVersion(PublishableEntityVersionMixin):
93102
makes things easier to reason about if we say that defined_list never
94103
changes for a given ContainerEntityVersion.
95104
"""
105+
96106
container = models.ForeignKey(
97107
ContainerEntity,
98108
on_delete=models.CASCADE,

openedx_learning/apps/authoring/units/api.py

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
"""
55

66
from django.db.transaction import atomic
7+
8+
from openedx_learning.apps.authoring.containers.models import EntityListRow
79
from ..publishing import api as publishing_api
810
from ..containers import api as container_api
911
from .models import Unit, UnitVersion
12+
from django.db.models import QuerySet
13+
1014

1115
from datetime import datetime
1216

@@ -18,10 +22,9 @@
1822
"get_unit",
1923
"get_unit_version",
2024
"get_latest_unit_version",
21-
"get_unit_version_by_version_num",
22-
"get_user_defined_components_in_unit_version",
23-
"get_initial_components_in_unit_version",
24-
"get_frozen_components_in_unit_version",
25+
"get_user_defined_list_in_unit_version",
26+
"get_initial_list_in_unit_version",
27+
"get_frozen_list_in_unit_version",
2528
]
2629

2730

@@ -175,23 +178,14 @@ def get_latest_unit_version(unit_pk: int) -> UnitVersion:
175178
return Unit.objects.get(pk=unit_pk).versioning.latest
176179

177180

178-
def get_unit_version_by_version_num(unit_pk: int, version_num: int) -> UnitVersion:
179-
"""Get a unit version by version number.
180-
181-
Args:
182-
unit_pk: The unit ID.
183-
version_num: The version number.
184-
"""
185-
return Unit.objects.get(pk=unit_pk).versioning.get(version_num=version_num)
186-
187-
188-
def get_user_defined_list_in_unit_version(unit_version_pk: int) -> list[int]:
181+
def get_user_defined_list_in_unit_version(unit_version_pk: int) -> QuerySet[EntityListRow]:
189182
"""Get the list in a unit version.
190183
191184
Args:
192185
unit_version_pk: The unit version ID.
193186
"""
194-
return UnitVersion.objects.get(pk=unit_version_pk).container_version.defined_list
187+
unit_version = UnitVersion.objects.get(pk=unit_version_pk)
188+
return container_api.get_defined_list_for_container_version(unit_version.container_entity_version)
195189

196190

197191
def get_initial_list_in_unit_version(unit_version_pk: int) -> list[int]:
@@ -200,7 +194,8 @@ def get_initial_list_in_unit_version(unit_version_pk: int) -> list[int]:
200194
Args:
201195
unit_version_pk: The unit version ID.
202196
"""
203-
return UnitVersion.objects.get(pk=unit_version_pk).container_version.initial_list
197+
unit_version = UnitVersion.objects.get(pk=unit_version_pk)
198+
return container_api.get_initial_list_for_container_version(unit_version.container_entity_version)
204199

205200

206201
def get_frozen_list_in_unit_version(unit_version_pk: int) -> list[int]:
@@ -209,4 +204,5 @@ def get_frozen_list_in_unit_version(unit_version_pk: int) -> list[int]:
209204
Args:
210205
unit_version_pk: The unit version ID.
211206
"""
212-
return UnitVersion.objects.get(pk=unit_version_pk).container_version.frozen_list
207+
unit_version = UnitVersion.objects.get(pk=unit_version_pk)
208+
return container_api.get_frozen_list_for_container_version(unit_version.container_entity_version)

openedx_learning/apps/authoring/units/models.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,23 @@
11
from django.db import models
22

3-
from ..containers.model_mixin import ContainerEntityMixin, ContainerEntityVersionMixin
3+
from ..containers.models_mixin import ContainerEntityMixin, ContainerEntityVersionMixin
4+
45

56
class Unit(ContainerEntityMixin):
67
"""
78
A Unit is Container, which is a PublishableEntity.
89
"""
910

11+
1012
class UnitVersion(ContainerEntityVersionMixin):
1113
"""
1214
A UnitVersion is a ContainerVersion, which is a PublishableEntityVersion.
1315
"""
16+
1417
# Not sure what other metadata goes here, but we want to try to separate things
1518
# like scheduling information and such into different models.
1619
unit = models.ForeignKey(
1720
Unit,
1821
on_delete=models.CASCADE,
1922
related_name="versions",
2023
)
21-

0 commit comments

Comments
 (0)