Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 19 additions & 12 deletions openedx_learning/apps/authoring/backup_restore/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@

import tomlkit

from openedx_learning.apps.authoring.publishing.models.learning_package import LearningPackage
from openedx_learning.apps.authoring.publishing.models.publishable_entity import (
PublishableEntityMixin,
PublishableEntityVersionMixin,
from openedx_learning.apps.authoring.publishing.models import (
Draft,
PublishableEntity,
PublishableEntityVersion,
Published,
)
from openedx_learning.apps.authoring.publishing.models.learning_package import LearningPackage


def toml_learning_package(learning_package: LearningPackage) -> str:
Expand All @@ -27,20 +29,25 @@ def toml_learning_package(learning_package: LearningPackage) -> str:
return tomlkit.dumps(doc)


def toml_publishable_entity(entity: PublishableEntityMixin) -> str:
def toml_publishable_entity(entity: PublishableEntity) -> str:
"""Create a TOML representation of a publishable entity."""

current_draft_version = getattr(entity, "draft", None)
current_published_version = getattr(entity, "published", None)

doc = tomlkit.document()
entity_table = tomlkit.table()
entity_table.add("uuid", str(entity.uuid))
entity_table.add("can_stand_alone", entity.can_stand_alone)

draft = tomlkit.table()
draft.add("version_num", entity.versioning.draft.version_num)
entity_table.add("draft", draft)
if current_draft_version:
draft = tomlkit.table()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: It might be clearer to call this a draft_table so it's clear at a glance that this is not a Draft model object.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Totally agree, changed. Thanks

draft.add("version_num", current_draft_version.version.version_num)
entity_table.add("draft", draft)

published = tomlkit.table()
if entity.versioning.published:
published.add("version_num", entity.versioning.published.version_num)
if current_published_version:
published.add("version_num", current_published_version.version.version_num)
else:
published.add(tomlkit.comment("unpublished: no published_version_num"))
entity_table.add("published", published)
Expand All @@ -49,7 +56,7 @@ def toml_publishable_entity(entity: PublishableEntityMixin) -> str:
doc.add(tomlkit.nl())
doc.add(tomlkit.comment("### Versions"))

for entity_version in entity.versioning.versions.all():
for entity_version in entity.versions.all():
version = tomlkit.aot()
version_table = toml_publishable_entity_version(entity_version)
version.append(version_table)
Expand All @@ -58,7 +65,7 @@ def toml_publishable_entity(entity: PublishableEntityMixin) -> str:
return tomlkit.dumps(doc)


def toml_publishable_entity_version(version: PublishableEntityVersionMixin) -> tomlkit.items.Table:
def toml_publishable_entity_version(version: PublishableEntityVersion) -> tomlkit.items.Table:
"""Create a TOML representation of a publishable entity version."""
version_table = tomlkit.table()
version_table.add("title", version.title)
Expand Down
4 changes: 2 additions & 2 deletions openedx_learning/apps/authoring/backup_restore/zipper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from pathlib import Path

from openedx_learning.apps.authoring.backup_restore.toml import toml_learning_package, toml_publishable_entity
from openedx_learning.apps.authoring.components import api as components_api
from openedx_learning.apps.authoring.publishing import api as publishing_api
from openedx_learning.apps.authoring.publishing.models.learning_package import LearningPackage

TOML_PACKAGE_NAME = "package.toml"
Expand Down Expand Up @@ -45,7 +45,7 @@ def create_zip(self, path: str) -> None:
zipf.writestr(collections_info, "") # Add explicit empty directory

# Add each entity's TOML file
for entity in components_api.get_components(self.learning_package.pk):
for entity in publishing_api.get_entities(self.learning_package.pk):
# Create a TOML representation of the entity
entity_toml_content: str = toml_publishable_entity(entity)
entity_toml_filename = f"{entity.key}.toml"
Expand Down
8 changes: 8 additions & 0 deletions openedx_learning/apps/authoring/publishing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
"get_publishable_entity_by_key",
"get_last_publish",
"get_all_drafts",
"get_entities",
"get_entities_with_unpublished_changes",
"get_entities_with_unpublished_deletes",
"publish_all_drafts",
Expand Down Expand Up @@ -261,6 +262,13 @@ def get_all_drafts(learning_package_id: int, /) -> QuerySet[Draft]:
)


def get_entities(learning_package_id: int, /) -> QuerySet[PublishableEntity]:
Comment thread
dwong2708 marked this conversation as resolved.
"""
Get all entities in a learning package.
"""
return PublishableEntity.objects.filter(learning_package_id=learning_package_id)


def get_entities_with_unpublished_changes(
learning_package_id: int,
/,
Expand Down
Loading