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
20 changes: 18 additions & 2 deletions openedx_learning/apps/authoring/backup_restore/toml.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
from typing import Any, Dict

import tomlkit
from django.conf import settings

from openedx_learning.apps.authoring.collections.models import Collection
from openedx_learning.apps.authoring.publishing import api as publishing_api
from openedx_learning.apps.authoring.publishing.models import PublishableEntity, PublishableEntityVersion
from openedx_learning.apps.authoring.publishing.models.learning_package import LearningPackage


def toml_learning_package(learning_package: LearningPackage, timestamp: datetime) -> str:
def toml_learning_package(
learning_package: LearningPackage,
timestamp: datetime,
format_version: int = 1,
user: str | None = None

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.

This should take an actual User object, so that we can add more info about the user later if we decide it's prudent, e.g. their email.

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.

Makes sense. Changes have been applied.

) -> str:
"""
Create a TOML representation of the learning package.

Expand All @@ -28,13 +34,23 @@ def toml_learning_package(learning_package: LearningPackage, timestamp: datetime
updated = 2025-09-03T17:50:59.536190Z
"""
doc = tomlkit.document()
doc.add(tomlkit.comment(f"Datetime of the export: {timestamp}"))

# Learning package main info
section = tomlkit.table()
section.add("title", learning_package.title)
section.add("key", learning_package.key)
section.add("description", learning_package.description)
section.add("created", learning_package.created)
section.add("updated", learning_package.updated)

# Learning package metadata
metadata = tomlkit.table()
metadata.add("format_version", format_version)
metadata.add("created_by", user or "unknown")

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.

If we don't have user information, let's just omit the field. The string "unknown" is a highly unlikely, but technically legal username. Omitting the field is more or less the TOML version of making null.

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.

Got it. Done. Thanks.

metadata.add("created_at", timestamp)
metadata.add("origin_server", getattr(settings, "CMS_BASE", "unknown"))

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.

Argh. I forgot that we'd need to pipe this value from edx-platform settings if we were to include it. We don't want to start the precedent of openedx-learning knowing about edx-platform specific settings. Let's just make this an optional param on the function for now, instead of reading from the settings. Libraries code in edx-platform can pass it in later. This is another one of those fields that we can omit if there's no value passed in.

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.

I see. The value needs to be provided from edx-platform. Changes applied.


doc.add("meta", metadata)
doc.add("learning_package", section)
return tomlkit.dumps(doc)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from django.contrib.auth import get_user_model
from django.core.management import CommandError, call_command
from django.db.models import QuerySet
from django.test import override_settings

from openedx_learning.api import authoring as api
from openedx_learning.api.authoring_models import Collection, Component, Content, LearningPackage, PublishableEntity
Expand Down Expand Up @@ -212,6 +213,7 @@ def check_zip_file_structure(self, zip_path: Path):
for expected_path in expected_paths:
self.assertIn(expected_path, zip_name_list)

@override_settings(CMS_BASE="http://cms.test", LMS_BASE="http://lms.test")
def test_lp_dump_command(self):
lp_key = self.learning_package.key
file_name = f"{lp_key}.zip"
Expand All @@ -238,6 +240,11 @@ def test_lp_dump_command(self):
f'key = "{self.learning_package.key}"',
f'title = "{self.learning_package.title}"',
f'description = "{self.learning_package.description}"',
'[meta]',
'format_version = 1',
'created_by = "unknown"',
'origin_server = "http://cms.test"',
'created_at =',
]
)

Expand Down
Loading