Skip to content

Commit

Permalink
refactor: use container API to get unit members
Browse files Browse the repository at this point in the history
  • Loading branch information
mariajgrimaldi committed Nov 5, 2024
1 parent 0efb913 commit 60fdb44
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 22 deletions.
14 changes: 12 additions & 2 deletions openedx_learning/apps/authoring/containers/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
from django.db import models

from openedx_learning.apps.authoring.publishing.models import PublishableEntity, PublishableEntityVersion
from ..publishing.model_mixins import PublishableEntityMixin, PublishableEntityVersionMixin
from openedx_learning.apps.authoring.publishing.models import (
PublishableEntity,
PublishableEntityVersion,
)
from ..publishing.model_mixins import (
PublishableEntityMixin,
PublishableEntityVersionMixin,
)


class EntityList(models.Model):
Expand All @@ -14,6 +20,7 @@ class EntityList(models.Model):
anonymous in a sense–they're pointed to by ContainerEntityVersions and
other models, rather than being looked up by their own identifers.
"""

pass


Expand All @@ -25,6 +32,7 @@ class EntityListRow(models.Model):
There is a row in this table for each member of an EntityList. The order_num
field is used to determine the order of the members in the list.
"""

entity_list = models.ForeignKey(EntityList, on_delete=models.CASCADE)

# This ordering should be treated as immutable–if the ordering needs to
Expand Down Expand Up @@ -70,6 +78,7 @@ class ContainerEntity(PublishableEntityMixin):
PublishLog and Containers that were affected in a publish because their
child elements were published.
"""

pass


Expand All @@ -93,6 +102,7 @@ class ContainerEntityVersion(PublishableEntityVersionMixin):
makes things easier to reason about if we say that defined_list never
changes for a given ContainerEntityVersion.
"""

container = models.ForeignKey(
ContainerEntity,
on_delete=models.CASCADE,
Expand Down
32 changes: 14 additions & 18 deletions openedx_learning/apps/authoring/units/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
"""

from django.db.transaction import atomic

from openedx_learning.apps.authoring.containers.models import EntityListRow
from ..publishing import api as publishing_api
from ..containers import api as container_api
from .models import Unit, UnitVersion
from django.db.models import QuerySet


from datetime import datetime

Expand All @@ -18,10 +22,9 @@
"get_unit",
"get_unit_version",
"get_latest_unit_version",
"get_unit_version_by_version_num",
"get_user_defined_components_in_unit_version",
"get_initial_components_in_unit_version",
"get_frozen_components_in_unit_version",
"get_user_defined_list_in_unit_version",
"get_initial_list_in_unit_version",
"get_frozen_list_in_unit_version",
]


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


def get_unit_version_by_version_num(unit_pk: int, version_num: int) -> UnitVersion:
"""Get a unit version by version number.
Args:
unit_pk: The unit ID.
version_num: The version number.
"""
return Unit.objects.get(pk=unit_pk).versioning.get(version_num=version_num)


def get_user_defined_list_in_unit_version(unit_version_pk: int) -> list[int]:
def get_user_defined_list_in_unit_version(unit_version_pk: int) -> QuerySet[EntityListRow]:
"""Get the list in a unit version.
Args:
unit_version_pk: The unit version ID.
"""
return UnitVersion.objects.get(pk=unit_version_pk).container_version.defined_list
unit_version = UnitVersion.objects.get(pk=unit_version_pk)
return container_api.get_defined_list_for_container_version(unit_version.container_entity_version)


def get_initial_list_in_unit_version(unit_version_pk: int) -> list[int]:
Expand All @@ -200,7 +194,8 @@ def get_initial_list_in_unit_version(unit_version_pk: int) -> list[int]:
Args:
unit_version_pk: The unit version ID.
"""
return UnitVersion.objects.get(pk=unit_version_pk).container_version.initial_list
unit_version = UnitVersion.objects.get(pk=unit_version_pk)
return container_api.get_initial_list_for_container_version(unit_version.container_entity_version)


def get_frozen_list_in_unit_version(unit_version_pk: int) -> list[int]:
Expand All @@ -209,4 +204,5 @@ def get_frozen_list_in_unit_version(unit_version_pk: int) -> list[int]:
Args:
unit_version_pk: The unit version ID.
"""
return UnitVersion.objects.get(pk=unit_version_pk).container_version.frozen_list
unit_version = UnitVersion.objects.get(pk=unit_version_pk)
return container_api.get_frozen_list_for_container_version(unit_version.container_entity_version)
6 changes: 4 additions & 2 deletions openedx_learning/apps/authoring/units/models.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
from django.db import models

from ..containers.model_mixin import ContainerEntityMixin, ContainerEntityVersionMixin
from ..containers.models_mixin import ContainerEntityMixin, ContainerEntityVersionMixin


class Unit(ContainerEntityMixin):
"""
A Unit is Container, which is a PublishableEntity.
"""


class UnitVersion(ContainerEntityVersionMixin):
"""
A UnitVersion is a ContainerVersion, which is a PublishableEntityVersion.
"""

# Not sure what other metadata goes here, but we want to try to separate things
# like scheduling information and such into different models.
unit = models.ForeignKey(
Unit,
on_delete=models.CASCADE,
related_name="versions",
)

0 comments on commit 60fdb44

Please sign in to comment.