Skip to content

feat: add library restore endpoint#37439

Merged
ormsbee merged 4 commits intoopenedx:masterfrom
WGU-Open-edX:tpayne/library-restore-endpoint
Oct 22, 2025
Merged

feat: add library restore endpoint#37439
ormsbee merged 4 commits intoopenedx:masterfrom
WGU-Open-edX:tpayne/library-restore-endpoint

Conversation

@wgu-taylor-payne
Copy link
Copy Markdown
Contributor

@wgu-taylor-payne wgu-taylor-payne commented Oct 7, 2025

Description

Adds a library restore endpoint to restore a learning package from a backup. The learning package can then be used to create a content library.

POST /api/libraries/v2/restore/

Requires a file field, which must be a ZIP file.

A library restore task will be queued for execution, and a task id will be returned in the JSON response.

{
    "task_id": "29cd29cb-a963-427b-8aaa-675c999832a1"
}

GET /api/libraries/v2/restore/

Requires a task_id parameter to be provided. If given a valid task_id it will return the task status and data based on the result of the restore.

Success:

{
    "state": "Succeeded",
    "result": {
        "learning_package_id": 1,
        "title": "Demo Learning Package",
        "org": "CL-TEST",
        "slug": "LIB_C001",
        "key": "lp-restore:Admin:CL-TEST:LIB_C001:1761076028573",
        "archive_key": "lib:CL-TEST:LIB_C001",
        "containers": 0,
        "components": 0,
        "collections": 0,
        "sections": 0,
        "subsections": 0,
        "units": 0,
        "created_on_server": "cms.test",
        "created_at": "2025-10-05T18:23:45.180Z",
        "created_by": {
            "username": "test_author",
            "email": "[email protected]"
        }
    },
    "error": null,
    "error_log": null
}

Failure:

{
    "state": "Failed",
    "result": null,
    "error": "Library restore failed. See error log for details.",
    "error_log": "http://testserver/uploads/user_tasks/2025/10/21/2cff2a1c-7fa9-4097-b8ca-6b1b80126475-error.log"
}

Pending (All other non-final states, like "In-Progress", will have the same result - only state is provided)

{
    "state": "Pending",
    "result": null,
    "error": null,
    "error_log": null
}

Supporting information

Resolves openedx/openedx-core#388.

Testing instructions

pytest -p no:randomly --ds cms.envs.test \
openedx/core/djangoapps/content_libraries/tests/test_content_libraries.py::LibraryRestoreViewTestCase

Deadline

Ulmo release

@wgu-taylor-payne wgu-taylor-payne self-assigned this Oct 7, 2025
@openedx-webhooks openedx-webhooks added open-source-contribution PR author is not from Axim or 2U core contributor PR author is a Core Contributor (who may or may not have write access to this repo). labels Oct 7, 2025
@openedx-webhooks
Copy link
Copy Markdown

openedx-webhooks commented Oct 7, 2025

Thanks for the pull request, @wgu-taylor-payne!

This repository is currently maintained by @openedx/wg-maintenance-edx-platform.

Once you've gone through the following steps feel free to tag them in a comment and let them know that your changes are ready for engineering review.

🔘 Get product approval

If you haven't already, check this list to see if your contribution needs to go through the product review process.

  • If it does, you'll need to submit a product proposal for your contribution, and have it reviewed by the Product Working Group.
    • This process (including the steps you'll need to take) is documented here.
  • If it doesn't, simply proceed with the next step.
🔘 Provide context

To help your reviewers and other members of the community understand the purpose and larger context of your changes, feel free to add as much of the following information to the PR description as you can:

  • Dependencies

    This PR must be merged before / after / at the same time as ...

  • Blockers

    This PR is waiting for OEP-1234 to be accepted.

  • Timeline information

    This PR must be merged by XX date because ...

  • Partner information

    This is for a course on edx.org.

  • Supporting documentation
  • Relevant Open edX discussion forum threads
🔘 Get a green build

If one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green.

Details
Where can I find more information?

If you'd like to get more details on all aspects of the review process for open source pull requests (OSPRs), check out the following resources:

When can I expect my changes to be merged?

Our goal is to get community contributions seen and reviewed as efficiently as possible.

However, the amount of time that it takes to review and merge a PR can vary significantly based on factors such as:

  • The size and impact of the changes that it introduces
  • The need for product review
  • Maintenance status of the parent repository

💡 As a result it may take up to several weeks or months to complete a review and merge your PR.

@github-project-automation github-project-automation Bot moved this to Needs Triage in Contributions Oct 7, 2025
@wgu-taylor-payne wgu-taylor-payne marked this pull request as draft October 7, 2025 15:38
Comment thread openedx/core/djangoapps/content_libraries/tasks.py
Comment thread openedx/core/djangoapps/content_libraries/rest_api/libraries.py
Comment thread openedx/core/djangoapps/content_libraries/rest_api/libraries.py Outdated
Comment thread openedx/core/djangoapps/content_libraries/rest_api/libraries.py Outdated
Comment thread openedx/core/djangoapps/content_libraries/rest_api/libraries.py Outdated
Comment thread openedx/core/djangoapps/content_libraries/rest_api/libraries.py Outdated
Comment thread openedx/core/djangoapps/content_libraries/rest_api/libraries.py Outdated
Comment thread openedx/core/djangoapps/content_libraries/rest_api/libraries.py Outdated
Comment thread openedx/core/djangoapps/content_libraries/tasks.py Outdated
@mphilbrick211 mphilbrick211 added the mao-onboarding Reviewing this will help onboard devs from an Axim mission-aligned organization (MAO). label Oct 8, 2025
@mphilbrick211 mphilbrick211 moved this from Needs Triage to In Eng Review in Contributions Oct 8, 2025
Copy link
Copy Markdown
Contributor

@dwong2708 dwong2708 left a comment

Choose a reason for hiding this comment

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

Nice job, Tylor, and thank you for addressing this. I left some comments, but please note that they depend on the merge of this PR
.

Comment thread openedx/core/djangoapps/content_libraries/rest_api/serializers.py Outdated
Comment thread openedx/core/djangoapps/content_libraries/tasks.py Outdated
Comment thread openedx/core/djangoapps/content_libraries/rest_api/libraries.py Outdated
Comment thread openedx/core/djangoapps/content_libraries/api/libraries.py Outdated
Comment thread openedx/core/djangoapps/content_libraries/api/libraries.py Outdated
@wgu-taylor-payne wgu-taylor-payne force-pushed the tpayne/library-restore-endpoint branch from 4170bdf to e196adb Compare October 14, 2025 21:59
Comment thread openedx/core/djangoapps/content_libraries/api/libraries.py
@ormsbee
Copy link
Copy Markdown
Contributor

ormsbee commented Oct 17, 2025

Just wanted to flag that #37499 might introduce some minor conflicts with this PR.

@wgu-taylor-payne wgu-taylor-payne force-pushed the tpayne/library-restore-endpoint branch 2 times, most recently from 9272ef8 to a3aedf5 Compare October 17, 2025 23:46
Copy link
Copy Markdown
Contributor

@rodmgwgu rodmgwgu left a comment

Choose a reason for hiding this comment

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

Looking good, just some small comments

Comment thread openedx/core/djangoapps/content_libraries/rest_api/serializers.py Outdated
Comment thread openedx/core/djangoapps/content_libraries/rest_api/libraries.py Outdated
Comment thread openedx/core/djangoapps/content_libraries/tasks.py
@wgu-taylor-payne wgu-taylor-payne force-pushed the tpayne/library-restore-endpoint branch from ca4c169 to d8e073c Compare October 21, 2025 22:12
@wgu-taylor-payne wgu-taylor-payne marked this pull request as ready for review October 21, 2025 22:34
Copy link
Copy Markdown
Contributor

@rodmgwgu rodmgwgu left a comment

Choose a reason for hiding this comment

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

Thanks for addressing the comments, code looks good. I haven't been able to test it yet as I'm facing some issues with my local env.

Copy link
Copy Markdown
Contributor

@ormsbee ormsbee left a comment

Choose a reason for hiding this comment

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

Some minor requests. I haven't reviewed the test code yet, but I wanted to submit this review anyway, in case you were still working today. (I need to make dinner for the family, and can't be back on for a while.)

Comment on lines +409 to +410
if provided_learning_package := learning_package is not None:
assert isinstance(learning_package, LearningPackage)
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.

Is mypy not doing this validation properly? I wouldn't normally think that this would be necessary to have.

Comment thread openedx/core/djangoapps/content_libraries/api/libraries.py
# Make sure the LearningPackage key is in the correct format
authoring_api.update_learning_package(
learning_package.id,
key=f'lib:{org.short_name}:{slug}',
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 is also str(ref.library_key), right?

Comment on lines +421 to +434
if not provided_learning_package:
learning_package = authoring_api.create_learning_package(
key=str(ref.library_key),
title=title,
description=description,
)
assert learning_package is not None
ref.learning_package = learning_package
ref.save()

if provided_learning_package:
# Make sure the LearningPackage key is in the correct format
authoring_api.update_learning_package(
learning_package.id,
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: This whole block seems correct to me, but it seems a little more complex than it has to be? As opposed to something like:

if learning_package:
    # A temporary LearningPackage was passed in, so change its key to match our library.
    authoring_api.update_learning_package(learning_package.id, key=str(ref.library_key))
else:
    # We have to generate a new LearningPackage for this library.
    learning_package = authoring_api.create_learning_package(
        key=str(ref.library_key),
        title=title,
        description=description,
    )
ref.learning_package = learning_package
ref.save() 

(This is not a merge-blocker.)

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.

Just wondering — in the case where learning_package exists, don’t we need to pass the title? I noticed the frontend is requesting that field.

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.

along with title, should we also update the description?

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.

I didn’t see any description field in the design, but I think it could be helpful to include it just in case. What do you think, @ormsbee ?

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.

Yes to title--I had forgotten that we stored a that in the LearningPackage and not the ContentLibrary.

The description is something that used to be passed in and editable in an earlier iteration of the design, but got dropped at some point. I think it's fine to add it to the update_learning_package() call.

Comment thread openedx/core/djangoapps/content_libraries/rest_api/serializers.py
Comment thread openedx/core/djangoapps/content_libraries/rest_api/serializers.py
# Make sure the LearningPackage key is in the correct format
authoring_api.update_learning_package(
learning_package.id,
key=f'lib:{org.short_name}:{slug}',
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.

I noticed that when provided_learning_package is not provided, it uses str(self.key), which I believe is the correct way to create the key since it uses LibraryLocatorV2.

Comment on lines +421 to +434
if not provided_learning_package:
learning_package = authoring_api.create_learning_package(
key=str(ref.library_key),
title=title,
description=description,
)
assert learning_package is not None
ref.learning_package = learning_package
ref.save()

if provided_learning_package:
# Make sure the LearningPackage key is in the correct format
authoring_api.update_learning_package(
learning_package.id,
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.

Just wondering — in the case where learning_package exists, don’t we need to pass the title? I noticed the frontend is requesting that field.

@ormsbee ormsbee merged commit bf8ffe4 into openedx:master Oct 22, 2025
49 checks passed
@github-project-automation github-project-automation Bot moved this from In Eng Review to Done in Contributions Oct 22, 2025
haftamuk pushed a commit to haftamuk/edx-platform that referenced this pull request Nov 3, 2025
Adds a library restore endpoint to restore a learning package from a
backup zip archive (/api/libraries/v2/restore/). The learning package
can then be used to create a content library.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core contributor PR author is a Core Contributor (who may or may not have write access to this repo). mao-onboarding Reviewing this will help onboard devs from an Axim mission-aligned organization (MAO). open-source-contribution PR author is not from Axim or 2U

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

Implement Library Restore Endpoint

6 participants