-
Notifications
You must be signed in to change notification settings - Fork 11
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
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
534e8d9
docs: adds guidelines for the Collection models
pomegranited 1fda3e5
feat: adds CollectionObject model and APIs
pomegranited a2cd234
feat: adds get_object_collections API and tests
pomegranited 7ec9ba5
chore: bumps version to 0.11.3
pomegranited 0e15dd0
fix: use PublishableEntity.key instead of IDs
pomegranited 00111bc
refactor: use "entities" instead of "contents" or "objects"
pomegranited b723d79
refactor: merge get_collections + get_learning_package_collections
pomegranited 2fe2e70
refactor: address PR review
pomegranited e03daaa
fix: use fewer queries to discover invalid entities
pomegranited File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
""" | ||
Open edX Learning ("Learning Core"). | ||
""" | ||
__version__ = "0.11.2" | ||
__version__ = "0.11.3" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
38 changes: 38 additions & 0 deletions
38
openedx_learning/apps/authoring/collections/migrations/0003_collection_entities.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# Generated by Django 4.2.15 on 2024-08-29 00:05 | ||
|
||
import django.db.models.deletion | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
import openedx_learning.lib.validators | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
migrations.swappable_dependency(settings.AUTH_USER_MODEL), | ||
('oel_publishing', '0002_alter_learningpackage_key_and_more'), | ||
('oel_collections', '0002_remove_collection_name_collection_created_by_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name='CollectionPublishableEntity', | ||
fields=[ | ||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), | ||
('created', models.DateTimeField(auto_now_add=True, validators=[openedx_learning.lib.validators.validate_utc_datetime])), | ||
('collection', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='oel_collections.collection')), | ||
('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)), | ||
('entity', models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, to='oel_publishing.publishableentity')), | ||
], | ||
), | ||
migrations.AddField( | ||
model_name='collection', | ||
name='entities', | ||
field=models.ManyToManyField(related_name='collections', through='oel_collections.CollectionPublishableEntity', to='oel_publishing.publishableentity'), | ||
), | ||
migrations.AddConstraint( | ||
model_name='collectionpublishableentity', | ||
constraint=models.UniqueConstraint(fields=('collection', 'entity'), name='oel_collections_cpe_uniq_col_ent'), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,68 @@ | ||
""" | ||
Core models for Collections | ||
|
||
TLDR Guidelines: | ||
1. DO NOT modify these models to store full version snapshots. | ||
2. DO NOT use these models to try to reconstruct historical versions of | ||
Collections for fast querying. | ||
|
||
If you're trying to do either of these things, you probably want a new model or | ||
app. For more details, read on. | ||
Comment on lines
+9
to
+10
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This adds a lot of context!! Thank you! ❤️ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh definitely.. @ormsbee 's notes are always worth preserving :) |
||
|
||
The goal of these models is to provide a lightweight method of organizing | ||
PublishableEntities. The first use case for this is modeling the structure of a | ||
v1 Content Library within a LearningPackage. This is what we'll use the | ||
Collection model for. | ||
|
||
An important thing to note here is that Collections are *NOT* publishable | ||
entities themselves. They have no "Draft" or "Published" versions. Collections | ||
are never "published", though the things inside of them are. | ||
|
||
When a LibraryContentBlock makes use of a Content Library, it copies all of | ||
the items it will use into the Course itself. It will also store a version | ||
on the LibraryContentBlock -- this is a MongoDB ObjectID in v1 and an integer in | ||
v2 Libraries. Later on, the LibraryContentBlock will want to check back to see | ||
if any updates have been made, using its version as a key. If a new version | ||
exists, the course team has the option of re-copying data from the Library. | ||
|
||
ModuleStore based v1 Libraries and Blockstore-based v2 libraries both version | ||
the entire library in a series of snapshots. This makes it difficult to have | ||
very large libraries, which is an explicit goal for Modular Learning. In | ||
Learning Core, we've moved to tracking the versions of individual Components to | ||
address this issue. But that means we no longer have a single version indicator | ||
for "has anything here changed"? | ||
|
||
We *could* have put that version in the ``publishing`` app's PublishLog, but | ||
that would make it too broad. We want the ability to eventually collapse many v1 | ||
Libraries into a single Learning Core backed v2 Library. If we tracked the | ||
versioning in only a central location, then we'd have many false positives where | ||
the version was bumped because something else in the Learning Package changed. | ||
So instead, we're creating a new Collection model inside the LearningPackage to | ||
track that concept. | ||
|
||
A critical takeaway is that we don't have to store snapshots of every version of | ||
a Collection, because that data has been copied over by the LibraryContentBlock. | ||
We only need to store the current state of the Collection, and increment the | ||
version numbers when changes happen. This will allow the LibraryContentBlock to | ||
check in and re-copy over the latest version if the course team desires. | ||
|
||
That's why these models only store the current state of a Collection. Unlike the | ||
``components`` app, ``collections`` does not store fully materialized snapshots | ||
of past versions. This is done intentionally in order to save space and reduce | ||
the cost of writes. Collections may grow to be very large, and we don't want to | ||
be writing N rows with every version, where N is the number of | ||
PublishableEntities in a Collection. | ||
|
||
MVP of these models does not store changesets, but we can add this when there's a | ||
use case for it. The number of rows in these changesets would grow in proportion | ||
to the number of things that are actually changing (instead of copying over | ||
everything on every version). This is could be used to make it easier to figure out | ||
what changed between two given versions of a Collection. A LibraryContentBlock | ||
in a course would have stored the version number of the last time it copied data | ||
from the Collection, and we can eventually surface this data to the user. Note that | ||
while it may be possible to reconstruct past versions of Collections based off of | ||
this changeset data, it's going to be a very slow process to do so, and it is | ||
strongly discouraged. | ||
""" | ||
from __future__ import annotations | ||
|
||
|
@@ -9,10 +72,11 @@ | |
|
||
from ....lib.fields import MultiCollationTextField, case_insensitive_char_field | ||
from ....lib.validators import validate_utc_datetime | ||
from ..publishing.models import LearningPackage | ||
from ..publishing.models import LearningPackage, PublishableEntity | ||
|
||
__all__ = [ | ||
"Collection", | ||
"CollectionPublishableEntity", | ||
] | ||
|
||
|
||
|
@@ -79,6 +143,12 @@ class Collection(models.Model): | |
], | ||
) | ||
|
||
entities: models.ManyToManyField[PublishableEntity, "CollectionPublishableEntity"] = models.ManyToManyField( | ||
PublishableEntity, | ||
through="CollectionPublishableEntity", | ||
related_name="collections", | ||
) | ||
|
||
class Meta: | ||
verbose_name_plural = "Collections" | ||
indexes = [ | ||
|
@@ -96,3 +166,42 @@ def __str__(self) -> str: | |
User-facing string representation of a Collection. | ||
""" | ||
return f"<{self.__class__.__name__}> ({self.id}:{self.title})" | ||
|
||
|
||
class CollectionPublishableEntity(models.Model): | ||
""" | ||
Collection -> PublishableEntity association. | ||
""" | ||
collection = models.ForeignKey( | ||
Collection, | ||
on_delete=models.CASCADE, | ||
) | ||
entity = models.ForeignKey( | ||
PublishableEntity, | ||
on_delete=models.RESTRICT, | ||
) | ||
created_by = models.ForeignKey( | ||
settings.AUTH_USER_MODEL, | ||
on_delete=models.SET_NULL, | ||
null=True, | ||
blank=True, | ||
) | ||
created = models.DateTimeField( | ||
auto_now_add=True, | ||
validators=[ | ||
validate_utc_datetime, | ||
], | ||
) | ||
|
||
pomegranited marked this conversation as resolved.
Show resolved
Hide resolved
pomegranited marked this conversation as resolved.
Show resolved
Hide resolved
|
||
class Meta: | ||
constraints = [ | ||
# Prevent race conditions from making multiple rows associating the | ||
# same Collection to the same Entity. | ||
models.UniqueConstraint( | ||
fields=[ | ||
"collection", | ||
"entity", | ||
], | ||
name="oel_collections_cpe_uniq_col_ent", | ||
) | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
[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 likeentity.collections.active()
.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.
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 :)