Skip to content
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

Soft delete collections #35496

Merged
merged 16 commits into from
Sep 25, 2024
Merged

Conversation

pomegranited
Copy link
Contributor

@pomegranited pomegranited commented Sep 18, 2024

Description

Adds support for soft-deleting and restoring Collections.

As part of this change, we're refactoring the way the Collections-related events are triggered to use Django model signals, so that changes made in Django Admin will be reflected in the Studio search index.

  • Which edX user roles will this change impact? Library Authors; not enabled in production by default.

Supporting information

Related to: openedx/modular-learning#231
Depends on: openedx/openedx-learning#229
Private-ref: FAL-3809

Testing instructions

Recommend reviewing the code commit by commit.

  1. Reindex your studio content to update any existing Collection documents: tutor dev run cms ./manage.py cms reindex_studio --experimental
  2. In the Authoring frontend, create a Collection and add some components to it.
  3. Open http://meilisearch.local.edly.io:7700/
    Your api key can be found with tutor config printvalue MEILISEARCH_API_KEY
    Search for your collection key and verify that the collection document and its components are returned.
  4. Hit the "soft delete" REST API: DELETE http://studio.local.edly.io:8001/api/libraries/v2/<library_key>/collections/<collection_key>/
    Note that the collection no longer appears in the Authoring MFE list of collections.
    Search for your collection key in Meilisearch and verify that no results are found.
  5. Hit the "restore" REST API: POST http://studio.local.edly.io:8001/api/libraries/v2/<library_key>/collections/<collection_key>/restore/
    Verify that the collection again appears in the Authoring MFE list of collections.
    Search for your collection key in Meilisearch and verify that the collection document and its components are returned again.
  6. Open Studio Django Admin http://studio.local.edly.io:8001/admin/oel_collections/collection/
    Disabling a collection should have the same effect as the "soft delete" REST API (step 4).
    Re-enabling a collection should have the same effect as the "restore" REST API (step 5)
  7. Delete a Collection via Django Admin.
    This will permanently delete the Collection.
  8. Try to hit the "restore" REST API with the deleted collection key: POST http://studio.local.edly.io:8001/api/libraries/v2/<library_key>/collections/<collection_key>/restore/
    This should return a 404.

Deadline

ASAP

Author Notes & Concerns

  • f760c2b changes the Meilisearch ID used for Collections from the int Collection.pk to a string generated from the Collection's usage key.
    We needed to do this so that the Collection document could be identified by the information in the CONTENT_LIBRARY_DELETED event (library_key + collection_key), so this document can be removed after the Collection itself is removed.
    Users will need to reindex their studio search content.

@openedx-webhooks openedx-webhooks added the open-source-contribution PR author is not from Axim or 2U label Sep 18, 2024
@openedx-webhooks
Copy link

openedx-webhooks commented Sep 18, 2024

Thanks for the pull request, @pomegranited!

What's next?

Please work through the following steps to get your changes 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.

🔘 Let us know that your PR is ready for review:

Who will review my changes?

This repository is currently maintained by @openedx/wg-maintenance-edx-platform. Tag them in a comment and let them know that your changes are ready for review.

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.

This change standardises the search document "id" to be a meilisearch ID
generated from the usage key, for all types of indexed objects.

This is important for collections so we can locate the collection
document in the search index solely from the data provided by the
LIBRARY_COLLECTION_DELETED event (library_key + collection_key), even if
the collection has been deleted from the database.
* get_library_collection_usage_key and
  searchable_doc_tags_for_collection do not need a Collection object;
  the usage key can be created from the library_key and collection_key.

* updated searchable_doc_for_collection to require the parts of the
  collection usage key + an optional collection. This allows us to
  identify the collection's search document from its usage key without
  requiring an existing Collection object (in case it's been deleted).
  Also removes the edge case for indexing Collections not associated
  with a ContentLibrary -- this won't ever really happen.
so that added/removed collections are removed/re-added to component documents.

Special case: When a collection is soft-deleted/restored, we detect this
in the search index and update the collection's component documents
directly, without a CONTENT_OBJECT_ASSOCIATON_CHANGED signal.
@@ -1103,8 +1100,7 @@ def create_library_collection(
content_library: ContentLibrary | None = None,
) -> Collection:
"""
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Do we even need create_library_collection and update_library_collection API methods now? They're just wrappers for the authoring_api methods now that we're not sending events anymore.

@@ -93,7 +93,7 @@ libsass==0.10.0
click==8.1.6

# pinning this version to avoid updates while the library is being developed
openedx-learning==0.11.5
openedx-learning @ git+https://github.com/open-craft/openedx-learning.git@jill/collection-delete
Copy link
Contributor Author

Choose a reason for hiding this comment

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

To do: fix this when PR merges.

@pomegranited pomegranited marked this pull request as ready for review September 20, 2024 16:03
@ChrisChV
Copy link
Contributor

@pomegranited The code looks good.

When testing the "soft delete" and "hard delete" with the admin panel, it don't update the index:

https://www.loom.com/share/115ac6dcc9504200b6c58cf3ed97a358?sid=2280dbda-8be2-4bb2-a630-714e5a2cf5ab

@pomegranited
Copy link
Contributor Author

pomegranited commented Sep 24, 2024

Hi @ChrisChV

When testing the "soft delete" and "hard delete" with the admin panel, it don't update the index:

I found out the issue here -- it has to be done from the Studio Django Admin, not the LMS (PR instructions updated).

I'm not sure how best to deal with it though -- the libraries app is installed on the LMS and the CMS, and the right events get raised no matter whether you use the LMS or Studio Django Admin. However, since the content/search app is only installed in the CMS, so its handlers only get run when the event gets raised in Studio.

If I'm understanding this right, running in production with an event bus would fix this, because the LIBRARY_COLLECTION_UPDATED event would be put on the bus for all apps to see.

Copy link
Contributor

@navinkarkera navinkarkera left a comment

Choose a reason for hiding this comment

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

@pomegranited Nice work! 💯 Left a small nit.

  • I tested this: Soft-deleted collections and restored it, verified events.
  • I read through the code
  • I checked for accessibility issues
  • Includes documentation

Comment on lines 587 to 595
document = None
try:
index = client.get_index(STUDIO_INDEX_NAME)
return index.get_document(id)
except (MeilisearchError, MeilisearchApiError) as err:
# The index or documennt doesn't exist
log.exception(err)

return None
Copy link
Contributor

Choose a reason for hiding this comment

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

document is not used. I think you meant to save index.get_document(id) in document and return it at the end.

Suggested change
document = None
try:
index = client.get_index(STUDIO_INDEX_NAME)
return index.get_document(id)
except (MeilisearchError, MeilisearchApiError) as err:
# The index or documennt doesn't exist
log.exception(err)
return None
document = None
try:
index = client.get_index(STUDIO_INDEX_NAME)
document = index.get_document(id)
except (MeilisearchError, MeilisearchApiError) as err:
# The index or documennt doesn't exist
log.exception(err)
return document

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Oh wow, thank you for spotting those typos @navinkarkera ! Fixed with e92f405.

Copy link
Contributor

@ChrisChV ChrisChV left a comment

Choose a reason for hiding this comment

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

@pomegranited Looks good 👍

  • I tested this: I followed the testing instructions
  • I read through the code and considered the security, stability and performance implications of the changes.
  • Includes tests for bugfixes and/or features added.
  • Includes documentation
  • Includes fixtures that create objects required for manual testing.

@ChrisChV ChrisChV merged commit 5446877 into openedx:master Sep 25, 2024
49 checks passed
@ChrisChV ChrisChV deleted the jill/collection-delete branch September 25, 2024 18:29
@edx-pipeline-bot
Copy link
Contributor

2U Release Notice: This PR has been deployed to the edX staging environment in preparation for a release to production.

@edx-pipeline-bot
Copy link
Contributor

2U Release Notice: This PR has been deployed to the edX production environment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
open-source-contribution PR author is not from Axim or 2U
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants