-
Notifications
You must be signed in to change notification settings - Fork 4.2k
feat: add library restore endpoint #37439
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
feat: add library restore endpoint #37439
Conversation
Thanks for the pull request, @wgu-taylor-payne! This repository is currently maintained by 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 approvalIf you haven't already, check this list to see if your contribution needs to go through the product review process.
🔘 Provide contextTo 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:
🔘 Get a green buildIf one or more checks are failing, continue working on your changes until this is no longer the case and your build turns green. 🔘 Update the status of your PRYour PR is currently marked as a draft. After completing the steps above, update its status by clicking "Ready for Review", or removing "WIP" from the title, as appropriate. 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:
💡 As a result it may take up to several weeks or months to complete a review and merge your PR. |
openedx/core/djangoapps/content_libraries/rest_api/libraries.py
Outdated
Show resolved
Hide resolved
openedx/core/djangoapps/content_libraries/rest_api/libraries.py
Outdated
Show resolved
Hide resolved
openedx/core/djangoapps/content_libraries/rest_api/libraries.py
Outdated
Show resolved
Hide resolved
openedx/core/djangoapps/content_libraries/rest_api/libraries.py
Outdated
Show resolved
Hide resolved
openedx/core/djangoapps/content_libraries/rest_api/libraries.py
Outdated
Show resolved
Hide resolved
openedx/core/djangoapps/content_libraries/rest_api/libraries.py
Outdated
Show resolved
Hide resolved
TASK_LOGGER.info('Restoring learning package from temporary file %s', tmp_path) | ||
|
||
try: | ||
result = authoring_api.load_dump_zip_file(tmp_path) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ormsbee In the scenario where LibraryPackage.key already exists, should we assign a unique temporary key to stage this library package? Currently, the staged_key is built later using the LP.id
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my comment about this, I suggested making the LearningPackage.key
be a combination of the learning key of the archive, the username of the person uploading, and a primary key (though really, anything that's unique will work--like a simple random hash value). So it would look like: lib-restore:dave:Axim:problem-bank:1002
(Okay, thinking on that more, I guess we'd want to construct the key in this bit of code and pass it to load_dump_zip_file
as an argument, so that would favor having a random hash instead of a pkey at the end.
The goals of the key would be:
- easily look up the user's uploads
- reconstruct the original key of the archive without having to open the archive up
- namespace it so that the key entries never collide
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Passing the staged LP sounds good to me, but @wgu-taylor-payne suggested passing only the User object, since the organization name (e.g., Axim in the example above) is already included in the zip file, and extracting it at the endpoint level wouldn’t make sense.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Recap: We talked about this at our sync-up today, where @dwong2708 brought me up to speed on what he and @wgu-taylor-payne were discussing.
I think the idea of having Learning Core decide what the temporary key is makes sense, and it also makes sense that we'd have to pass in the user object, since that is how Learning Core would namespace things. Overall, this gives LC the flexibility to determine how it wants to manage this specific kind of staged LearningPackage for a given user. In terms of the last part of the key--anything uniquely identifying will do. We could use a timestamp with sufficient precision, for instance.
In all cases, we shouldn't rely on parsing the key when displaying information to the frontend. The frontend should still get all its information from specific fields in the JSON artifact that is produced during the upload process.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Currently in this PR, along with creating and updating the restored LP with the "staged" key, the code also validates the key format, extracts the username from the key to see if the user is authorized to create a library from the LP, and then changes the key back to the standard version after the LP is tied to a content library.
It would be nice to have Learning Core provide an interface for this functionality as well.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me, Just to clarify, @ormsbee , we don’t need to check that the uploaded user matches the backup user, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right. Anyone could upload an archive created by anyone else.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In a nutshell:
The staged key generation happens on the LC side (PR
, pending review). Based on that, I believe the logic here can be simplified:
- In this line, we pass the user and True value to generate the staged key:
authoring_api.load_dump_zip_file(tmp_path, user=user, use_staged_lp_key=True)
- The user task returns the Learning Package (LP) ID.
- When creating the Content Library, the frontend will send the LP ID, and the endpoint will update the key based on the slug and org provided by the frontend. At this point, the ContentLibrary model has already been created, and you can use its library_key property to pass it to the update_library_package API.
There was a problem hiding this 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
.
slug = serializers.CharField(source="key.slug", validators=(validate_unicode_slug, )) | ||
title = serializers.CharField() | ||
description = serializers.CharField(allow_blank=True) | ||
learning_package_key = serializers.CharField(required=False) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the new learning package key will be generated by the ContentLibrary
model, I think passing the learning_package_key
is unnecessary.
# staged key format: lib-restore:user:org:slug:id, where user is the username of the user | ||
# performing the restore, and id is the id of the learning package | ||
user = User.objects.get(id=user_id) | ||
staged_key = f"{namespace}-restore:{user.username}:{org}:{slug}:{learning_package.id}" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No longer needed once this PR is merged.
learning_package = None | ||
if learning_package_key := data.pop("learning_package_key", None): | ||
username = learning_package_key.split(':')[1] | ||
if request.user.username != username: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not necessary — please remove it
namespace = namespace.split('-')[0] # Remove "-restore" suffix | ||
authoring_api.update_learning_package( | ||
learning_package.id, | ||
key=f'{namespace}:{lp_org}:{lp_slug}', |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use ref.library_key
and pass it here, since the new LP key
is generated from the org and slug provided by the new ContentLibrary
.
|
||
if provided_learning_package: | ||
# Convert "staged" key to normal key | ||
namespace, _, lp_org, lp_slug, _ = learning_package.key.split(':') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Won’t be necessary once this PR
is merged.
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.
GET /api/libraries/v2/restore/
Requires a
task_id
parameter to be provided. If given a validtask_id
it will return the task status and data based on the result of the restore.Success:
Failure:
Supporting information
Resolves openedx/openedx-learning#388.
Depends on functionality in openedx/openedx-learning#406.
Testing instructions
TODO
Deadline
Ulmo release