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

Associate PublishableEntities with Collections [FC-0062] #216

Merged
merged 9 commits into from
Sep 3, 2024

Conversation

pomegranited
Copy link
Contributor

@pomegranited pomegranited commented Aug 21, 2024

Description

Adds models and APIs for linking objects (PublishableEntities) to Collections, and for fetching the Collections associated with a given entity.

Supporting information

Part of : openedx/modular-learning#227
Based on : #131

Private-ref: FAL-3784

Testing instructions

See open-craft/edx-platform#674

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

openedx-webhooks commented Aug 21, 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 @axim-engineering. 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.

@mphilbrick211 mphilbrick211 added the FC Relates to an Axim Funded Contribution project label Aug 22, 2024
@pomegranited pomegranited force-pushed the jill/collection-components branch 2 times, most recently from 341efc4 to 56c0a99 Compare August 23, 2024 07:51
to associate PublishableEntities with Collections.

Collection.modified timestamp is updated whenever its contents are changed.
@bradenmacdonald
Copy link
Contributor

@ormsbee Can you take a quick look at this when you get some time? In particular, if possible, I want to make sure that this is sufficiently general that we can link units/sections/subsections to collections with the same API, even if they're not implemented as XBlocks/Components. Is PublishableEntity the right "thing" to use here then?

@ormsbee
Copy link
Contributor

ormsbee commented Aug 28, 2024

@bradenmacdonald: I'll do a full review now this afternoon, but yes, PublishableEntity is the correct generic thing to join against to get all the different publishable things.

Copy link
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 questions and requests. Most of these are minor changes to match openedx_learning package coding conventions that I have failed to document anywhere, and there is no reasonable way for you to have been aware of them. 😛 Sorry about that, and thank you for your patience.

openedx_learning/apps/authoring/collections/models.py Outdated Show resolved Hide resolved
openedx_learning/apps/authoring/collections/models.py Outdated Show resolved Hide resolved
openedx_learning/apps/authoring/collections/models.py Outdated Show resolved Hide resolved
openedx_learning/apps/authoring/collections/readme.rst Outdated Show resolved Hide resolved
openedx_learning/apps/authoring/collections/api.py Outdated Show resolved Hide resolved
openedx_learning/apps/authoring/collections/models.py Outdated Show resolved Hide resolved
openedx_learning/apps/authoring/collections/api.py Outdated Show resolved Hide resolved
Comment on lines 49 to 54
added = add_to_collections(
Collection.objects.filter(id=collection.id),
contents_qset,
)
if added:
collection.refresh_from_db() # fetch updated modified date
Copy link
Contributor

Choose a reason for hiding this comment

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

[nitpick, optional]: It's a little weird to me that this is going to cause multiple writes to the same Collection with just slightly differing datetimes, but in the same transaction. I can't quite place my finger on why this bothers me, except some vague sense that something tracking the Collection lifecycle might get confused.

Instead of invoking add_to_collections and doing the refresh to get around the timestamp change, can we instead copy some of logic from there that does the adding of the records?

So something like:

        records_to_add = [
            CollectionPublishableEntity(
                collection_id=collection.id,
                entity_id=entity_id,
            )
            for entity_id in entities_qset.values_list("pk", flat=True)
        ]
        CollectionPublishableEntity.bulk_add(records_to_add)

Copy link
Contributor Author

@pomegranited pomegranited Aug 28, 2024

Choose a reason for hiding this comment

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

@ormsbee I don't have a use case for creating a collection with components in it, I was just copying that code from your PR. Maybe it's better just to keep them as separate API calls, to avoid this duplication, and the need for a transaction?

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm fine either way. I think that use case will come up when we do v1 library import functionality, but I'm fine with punting until then.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I ended up removing the entities_qset param from create_collection, we can add it back later if needed.

openedx_learning/apps/authoring/collections/api.py Outdated Show resolved Hide resolved
Copy link
Contributor

@rpenido rpenido left a comment

Choose a reason for hiding this comment

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

Hi @pomegranited!

Thank you for your work here! I just left some comments.

Comment on lines +9 to +10
If you're trying to do either of these things, you probably want a new model or
app. For more details, read on.
Copy link
Contributor

Choose a reason for hiding this comment

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

This adds a lot of context!! Thank you! ❤️

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 definitely.. @ormsbee 's notes are always worth preserving :)

Addresses PR review.

* CollectionObject -> CollectionPublishableEntity,
  with added created_by and created fields
* contents -> entities, contents_qset -> entities_qset
* get_object_collections -> get_entity_collections,
  with added learning_package_id argument
Copy link
Contributor Author

@pomegranited pomegranited left a comment

Choose a reason for hiding this comment

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

Thank you for your reviews, @ormsbee and @rpenido :) I addressed everything I could, but there's a couple open questions, if you could take another look?

openedx_learning/apps/authoring/collections/api.py Outdated Show resolved Hide resolved
We ended up not needing a "get all collections" function; we always
fetch them for a given learning package.

So we merged them into a single api.get_collections method that takes a
`learning_package_id` + optional `enabled` (defaults to True).
Copy link
Contributor

@rpenido rpenido left a comment

Choose a reason for hiding this comment

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

Thank you for the changes, @pomegranited!
LGTM 👍

openedx_learning/apps/authoring/collections/api.py Outdated Show resolved Hide resolved
openedx_learning/apps/authoring/collections/api.py Outdated Show resolved Hide resolved
API:
* create_collection -- removed entities_qset param, added enabled param
* add/remove entities from a single collection, not a qset
* raise ValidationError if adding entities to a collection with
  mismatching learning packages
* add/remove returns updated collection object (not count)

Tests:
* use authoring API
* simplified data used by test classes
@pomegranited
Copy link
Contributor Author

Thanks for your review @rpenido :) How's this looking now, @ormsbee ?

Copy link
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.

Optional nitpick and some speculation, but nothing that would block merging. Thank you!

(Also, this is a three-day weekend in the U.S., so I may be a bit slow on reviews.)

Comment on lines 101 to 103
# Disallow adding entities outside the collection's learning package
for entity in entities_qset.all():
if entity.learning_package_id != learning_package_id:
Copy link
Contributor

Choose a reason for hiding this comment

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

[Nit (optional)]: You can also push this to the database layer with something like:

invalid_entity = entities_qset.filter(~Q(learning_package_id=learning_package_id)).first()
if invalid_entity:
    # ... validation error with specifics

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's much cleaner, thank you, done with e03daaa

return collection


def get_entity_collections(learning_package_id: int, entity_key: str) -> QuerySet[Collection]:
Copy link
Contributor

Choose a reason for hiding this comment

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

[speculation (please do not block PR for this)]: It might be nice if we could a custom Manager on the through-model so that if someone has an entity, they could do something like entity.collections.active().

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 absolutely -- I am interested to see the other use cases for Collections besides what we're supporting here. But will leave that for a day when it's needed, thank you :)

@pomegranited pomegranited merged commit 4779ec9 into main Sep 3, 2024
9 checks passed
@pomegranited pomegranited deleted the jill/collection-components branch September 3, 2024 00:31
@openedx-webhooks
Copy link

@pomegranited 🎉 Your pull request was merged! Please take a moment to answer a two question survey so we can improve your experience in the future.

navinkarkera added a commit to open-craft/edx-platform that referenced this pull request Sep 4, 2024
As get_all_collections api was refactored [1] to only support fetching
collections by learning_package, update reindex function to get
collections by learning_package.

1. openedx/openedx-learning#216
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FC Relates to an Axim Funded Contribution project open-source-contribution PR author is not from Axim or 2U
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

6 participants