openedx-learning 0.11.1__py2.py3-none-any.whl → 0.11.3__py2.py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,4 +1,4 @@
1
1
  """
2
2
  Open edX Learning ("Learning Core").
3
3
  """
4
- __version__ = "0.11.1"
4
+ __version__ = "0.11.3"
@@ -3,8 +3,13 @@ Collections API (warning: UNSTABLE, in progress API)
3
3
  """
4
4
  from __future__ import annotations
5
5
 
6
+ from datetime import datetime, timezone
7
+
8
+ from django.core.exceptions import ValidationError
6
9
  from django.db.models import QuerySet
7
10
 
11
+ from ..publishing import api as publishing_api
12
+ from ..publishing.models import PublishableEntity
8
13
  from .models import Collection
9
14
 
10
15
  # The public API that will be re-exported by openedx_learning.apps.authoring.api
@@ -13,9 +18,12 @@ from .models import Collection
13
18
  # start with an underscore AND it is not in __all__, that function is considered
14
19
  # to be callable only by other apps in the authoring package.
15
20
  __all__ = [
21
+ "add_to_collection",
16
22
  "create_collection",
17
23
  "get_collection",
18
- "get_learning_package_collections",
24
+ "get_collections",
25
+ "get_entity_collections",
26
+ "remove_from_collection",
19
27
  "update_collection",
20
28
  ]
21
29
 
@@ -25,6 +33,7 @@ def create_collection(
25
33
  title: str,
26
34
  created_by: int | None,
27
35
  description: str = "",
36
+ enabled: bool = True,
28
37
  ) -> Collection:
29
38
  """
30
39
  Create a new Collection
@@ -34,6 +43,7 @@ def create_collection(
34
43
  title=title,
35
44
  created_by_id=created_by,
36
45
  description=description,
46
+ enabled=enabled,
37
47
  )
38
48
  return collection
39
49
 
@@ -69,12 +79,85 @@ def update_collection(
69
79
  return collection
70
80
 
71
81
 
72
- def get_learning_package_collections(learning_package_id: int) -> QuerySet[Collection]:
82
+ def add_to_collection(
83
+ collection_id: int,
84
+ entities_qset: QuerySet[PublishableEntity],
85
+ created_by: int | None = None,
86
+ ) -> Collection:
87
+ """
88
+ Adds a QuerySet of PublishableEntities to a Collection.
89
+
90
+ These Entities must belong to the same LearningPackage as the Collection, or a ValidationError will be raised.
91
+
92
+ PublishableEntities already in the Collection are silently ignored.
93
+
94
+ The Collection object's modified date is updated.
95
+
96
+ Returns the updated Collection object.
97
+ """
98
+ collection = get_collection(collection_id)
99
+ learning_package_id = collection.learning_package_id
100
+
101
+ # Disallow adding entities outside the collection's learning package
102
+ invalid_entity = entities_qset.exclude(learning_package_id=learning_package_id).first()
103
+ if invalid_entity:
104
+ raise ValidationError(
105
+ f"Cannot add entity {invalid_entity.pk} in learning package {invalid_entity.learning_package_id} "
106
+ f"to collection {collection_id} in learning package {learning_package_id}."
107
+ )
108
+
109
+ collection.entities.add(
110
+ *entities_qset.all(),
111
+ through_defaults={"created_by_id": created_by},
112
+ )
113
+ collection.modified = datetime.now(tz=timezone.utc)
114
+ collection.save()
115
+
116
+ return collection
117
+
118
+
119
+ def remove_from_collection(
120
+ collection_id: int,
121
+ entities_qset: QuerySet[PublishableEntity],
122
+ ) -> Collection:
123
+ """
124
+ Removes a QuerySet of PublishableEntities from a Collection.
125
+
126
+ PublishableEntities are deleted (in bulk).
127
+
128
+ The Collection's modified date is updated (even if nothing was removed).
129
+
130
+ Returns the updated Collection.
131
+ """
132
+ collection = get_collection(collection_id)
133
+
134
+ collection.entities.remove(*entities_qset.all())
135
+ collection.modified = datetime.now(tz=timezone.utc)
136
+ collection.save()
137
+
138
+ return collection
139
+
140
+
141
+ def get_entity_collections(learning_package_id: int, entity_key: str) -> QuerySet[Collection]:
142
+ """
143
+ Get all collections in the given learning package which contain this entity.
144
+
145
+ Only enabled collections are returned.
146
+ """
147
+ entity = publishing_api.get_publishable_entity_by_key(
148
+ learning_package_id,
149
+ key=entity_key,
150
+ )
151
+ return entity.collections.filter(enabled=True).order_by("pk")
152
+
153
+
154
+ def get_collections(learning_package_id: int, enabled: bool | None = True) -> QuerySet[Collection]:
73
155
  """
74
156
  Get all collections for a given learning package
75
157
 
76
- Only enabled collections are returned
158
+ Enabled collections are returned by default.
77
159
  """
78
- return Collection.objects \
79
- .filter(learning_package_id=learning_package_id, enabled=True) \
80
- .select_related("learning_package")
160
+ qs = Collection.objects.filter(learning_package_id=learning_package_id)
161
+ if enabled is not None:
162
+ qs = qs.filter(enabled=enabled)
163
+ return qs.select_related("learning_package").order_by('pk')
@@ -0,0 +1,38 @@
1
+ # Generated by Django 4.2.15 on 2024-08-29 00:05
2
+
3
+ import django.db.models.deletion
4
+ from django.conf import settings
5
+ from django.db import migrations, models
6
+
7
+ import openedx_learning.lib.validators
8
+
9
+
10
+ class Migration(migrations.Migration):
11
+
12
+ dependencies = [
13
+ migrations.swappable_dependency(settings.AUTH_USER_MODEL),
14
+ ('oel_publishing', '0002_alter_learningpackage_key_and_more'),
15
+ ('oel_collections', '0002_remove_collection_name_collection_created_by_and_more'),
16
+ ]
17
+
18
+ operations = [
19
+ migrations.CreateModel(
20
+ name='CollectionPublishableEntity',
21
+ fields=[
22
+ ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
23
+ ('created', models.DateTimeField(auto_now_add=True, validators=[openedx_learning.lib.validators.validate_utc_datetime])),
24
+ ('collection', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='oel_collections.collection')),
25
+ ('created_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
26
+ ('entity', models.ForeignKey(on_delete=django.db.models.deletion.RESTRICT, to='oel_publishing.publishableentity')),
27
+ ],
28
+ ),
29
+ migrations.AddField(
30
+ model_name='collection',
31
+ name='entities',
32
+ field=models.ManyToManyField(related_name='collections', through='oel_collections.CollectionPublishableEntity', to='oel_publishing.publishableentity'),
33
+ ),
34
+ migrations.AddConstraint(
35
+ model_name='collectionpublishableentity',
36
+ constraint=models.UniqueConstraint(fields=('collection', 'entity'), name='oel_collections_cpe_uniq_col_ent'),
37
+ ),
38
+ ]
@@ -1,5 +1,68 @@
1
1
  """
2
2
  Core models for Collections
3
+
4
+ TLDR Guidelines:
5
+ 1. DO NOT modify these models to store full version snapshots.
6
+ 2. DO NOT use these models to try to reconstruct historical versions of
7
+ Collections for fast querying.
8
+
9
+ If you're trying to do either of these things, you probably want a new model or
10
+ app. For more details, read on.
11
+
12
+ The goal of these models is to provide a lightweight method of organizing
13
+ PublishableEntities. The first use case for this is modeling the structure of a
14
+ v1 Content Library within a LearningPackage. This is what we'll use the
15
+ Collection model for.
16
+
17
+ An important thing to note here is that Collections are *NOT* publishable
18
+ entities themselves. They have no "Draft" or "Published" versions. Collections
19
+ are never "published", though the things inside of them are.
20
+
21
+ When a LibraryContentBlock makes use of a Content Library, it copies all of
22
+ the items it will use into the Course itself. It will also store a version
23
+ on the LibraryContentBlock -- this is a MongoDB ObjectID in v1 and an integer in
24
+ v2 Libraries. Later on, the LibraryContentBlock will want to check back to see
25
+ if any updates have been made, using its version as a key. If a new version
26
+ exists, the course team has the option of re-copying data from the Library.
27
+
28
+ ModuleStore based v1 Libraries and Blockstore-based v2 libraries both version
29
+ the entire library in a series of snapshots. This makes it difficult to have
30
+ very large libraries, which is an explicit goal for Modular Learning. In
31
+ Learning Core, we've moved to tracking the versions of individual Components to
32
+ address this issue. But that means we no longer have a single version indicator
33
+ for "has anything here changed"?
34
+
35
+ We *could* have put that version in the ``publishing`` app's PublishLog, but
36
+ that would make it too broad. We want the ability to eventually collapse many v1
37
+ Libraries into a single Learning Core backed v2 Library. If we tracked the
38
+ versioning in only a central location, then we'd have many false positives where
39
+ the version was bumped because something else in the Learning Package changed.
40
+ So instead, we're creating a new Collection model inside the LearningPackage to
41
+ track that concept.
42
+
43
+ A critical takeaway is that we don't have to store snapshots of every version of
44
+ a Collection, because that data has been copied over by the LibraryContentBlock.
45
+ We only need to store the current state of the Collection, and increment the
46
+ version numbers when changes happen. This will allow the LibraryContentBlock to
47
+ check in and re-copy over the latest version if the course team desires.
48
+
49
+ That's why these models only store the current state of a Collection. Unlike the
50
+ ``components`` app, ``collections`` does not store fully materialized snapshots
51
+ of past versions. This is done intentionally in order to save space and reduce
52
+ the cost of writes. Collections may grow to be very large, and we don't want to
53
+ be writing N rows with every version, where N is the number of
54
+ PublishableEntities in a Collection.
55
+
56
+ MVP of these models does not store changesets, but we can add this when there's a
57
+ use case for it. The number of rows in these changesets would grow in proportion
58
+ to the number of things that are actually changing (instead of copying over
59
+ everything on every version). This is could be used to make it easier to figure out
60
+ what changed between two given versions of a Collection. A LibraryContentBlock
61
+ in a course would have stored the version number of the last time it copied data
62
+ from the Collection, and we can eventually surface this data to the user. Note that
63
+ while it may be possible to reconstruct past versions of Collections based off of
64
+ this changeset data, it's going to be a very slow process to do so, and it is
65
+ strongly discouraged.
3
66
  """
4
67
  from __future__ import annotations
5
68
 
@@ -9,10 +72,11 @@ from django.utils.translation import gettext_lazy as _
9
72
 
10
73
  from ....lib.fields import MultiCollationTextField, case_insensitive_char_field
11
74
  from ....lib.validators import validate_utc_datetime
12
- from ..publishing.models import LearningPackage
75
+ from ..publishing.models import LearningPackage, PublishableEntity
13
76
 
14
77
  __all__ = [
15
78
  "Collection",
79
+ "CollectionPublishableEntity",
16
80
  ]
17
81
 
18
82
 
@@ -79,6 +143,12 @@ class Collection(models.Model):
79
143
  ],
80
144
  )
81
145
 
146
+ entities: models.ManyToManyField[PublishableEntity, "CollectionPublishableEntity"] = models.ManyToManyField(
147
+ PublishableEntity,
148
+ through="CollectionPublishableEntity",
149
+ related_name="collections",
150
+ )
151
+
82
152
  class Meta:
83
153
  verbose_name_plural = "Collections"
84
154
  indexes = [
@@ -96,3 +166,42 @@ class Collection(models.Model):
96
166
  User-facing string representation of a Collection.
97
167
  """
98
168
  return f"<{self.__class__.__name__}> ({self.id}:{self.title})"
169
+
170
+
171
+ class CollectionPublishableEntity(models.Model):
172
+ """
173
+ Collection -> PublishableEntity association.
174
+ """
175
+ collection = models.ForeignKey(
176
+ Collection,
177
+ on_delete=models.CASCADE,
178
+ )
179
+ entity = models.ForeignKey(
180
+ PublishableEntity,
181
+ on_delete=models.RESTRICT,
182
+ )
183
+ created_by = models.ForeignKey(
184
+ settings.AUTH_USER_MODEL,
185
+ on_delete=models.SET_NULL,
186
+ null=True,
187
+ blank=True,
188
+ )
189
+ created = models.DateTimeField(
190
+ auto_now_add=True,
191
+ validators=[
192
+ validate_utc_datetime,
193
+ ],
194
+ )
195
+
196
+ class Meta:
197
+ constraints = [
198
+ # Prevent race conditions from making multiple rows associating the
199
+ # same Collection to the same Entity.
200
+ models.UniqueConstraint(
201
+ fields=[
202
+ "collection",
203
+ "entity",
204
+ ],
205
+ name="oel_collections_cpe_uniq_col_ent",
206
+ )
207
+ ]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: openedx-learning
3
- Version: 0.11.1
3
+ Version: 0.11.3
4
4
  Summary: Open edX Learning Core and Tagging.
5
5
  Home-page: https://github.com/openedx/openedx-learning
6
6
  Author: David Ormsbee
@@ -18,12 +18,12 @@ Classifier: Programming Language :: Python :: 3.11
18
18
  Classifier: Programming Language :: Python :: 3.12
19
19
  Requires-Python: >=3.11
20
20
  License-File: LICENSE.txt
21
- Requires-Dist: rules<4.0
21
+ Requires-Dist: Django<5.0
22
+ Requires-Dist: djangorestframework<4.0
22
23
  Requires-Dist: edx-drf-extensions
23
- Requires-Dist: celery
24
24
  Requires-Dist: attrs
25
- Requires-Dist: djangorestframework<4.0
26
- Requires-Dist: Django<5.0
25
+ Requires-Dist: rules<4.0
26
+ Requires-Dist: celery
27
27
 
28
28
  Open edX Learning Core (and Tagging)
29
29
  ====================================
@@ -1,4 +1,4 @@
1
- openedx_learning/__init__.py,sha256=WHmySeEcWrQjNX67Sfy3ikRckRukGcPd2C61xga0d4I,68
1
+ openedx_learning/__init__.py,sha256=GWXUDXEETsD5xIkwTotP1eDkTbHb3eZA6iOYL9y-pl4,68
2
2
  openedx_learning/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
3
  openedx_learning/api/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  openedx_learning/api/authoring.py,sha256=vbRpiQ2wOfN3oR2bbN0-bI2ra0QRtha9tVixKW1ENis,929
@@ -6,11 +6,12 @@ openedx_learning/api/authoring_models.py,sha256=PrhEYozI7nzC7N1v_sP1QgjwjRvLX6it
6
6
  openedx_learning/apps/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
7
  openedx_learning/apps/authoring/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
8
8
  openedx_learning/apps/authoring/collections/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
- openedx_learning/apps/authoring/collections/api.py,sha256=3blr4y40cGGqE7QXVQaexzhqMdeDrGa8JTw2wi6UU8M,2205
9
+ openedx_learning/apps/authoring/collections/api.py,sha256=l39uBtmYL-0QujkwleGRa9g2iSI0zzwlMsIRRuU1rl4,4821
10
10
  openedx_learning/apps/authoring/collections/apps.py,sha256=67imy9vnyXml4cfLNGFJhuZr2yx1q92-xz4ih3yJgNU,416
11
- openedx_learning/apps/authoring/collections/models.py,sha256=K_hfFxMirAqntgL1yxoS8LjTJkVOHCszoR6PwMfJGQ8,2427
11
+ openedx_learning/apps/authoring/collections/models.py,sha256=ZON6YHPtHM2nWCpsF1sdk2uANg93M2o_g30-uXEe4WQ,7415
12
12
  openedx_learning/apps/authoring/collections/migrations/0001_initial.py,sha256=xcJoqMLROW9FTILYrz6UsyotVu9wBN5tRBwRoAWG6dg,1496
13
13
  openedx_learning/apps/authoring/collections/migrations/0002_remove_collection_name_collection_created_by_and_more.py,sha256=NRl-_-EkE-w4JLeJsZf6lHxYNV3TIAJVIWbv_-xytGo,2162
14
+ openedx_learning/apps/authoring/collections/migrations/0003_collection_entities.py,sha256=-YddfCFS0HW3JsEcpatjakOyeGVklOmba9uRQ-0zIxQ,1805
14
15
  openedx_learning/apps/authoring/collections/migrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
15
16
  openedx_learning/apps/authoring/components/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
16
17
  openedx_learning/apps/authoring/components/admin.py,sha256=QE7e76C6X2V1AQPxQe6SayQPfCMmbs4RZPEPIGcvTWw,4672
@@ -103,8 +104,8 @@ openedx_tagging/core/tagging/rest_api/v1/serializers.py,sha256=gbvEBLvsmfPc3swWz
103
104
  openedx_tagging/core/tagging/rest_api/v1/urls.py,sha256=dNUKCtUCx_YzrwlbEbpDfjGVQbb2QdJ1VuJCkladj6E,752
104
105
  openedx_tagging/core/tagging/rest_api/v1/views.py,sha256=ZRkSILdb8g5k_BcuuVVfdffEdY9vFQ_YtMa3JrN0Xz8,35581
105
106
  openedx_tagging/core/tagging/rest_api/v1/views_import.py,sha256=kbHUPe5A6WaaJ3J1lFIcYCt876ecLNQfd19m7YYub6c,1470
106
- openedx_learning-0.11.1.dist-info/LICENSE.txt,sha256=QTW2QN7q3XszgUAXm9Dzgtu5LXYKbR1SGnqMa7ufEuY,35139
107
- openedx_learning-0.11.1.dist-info/METADATA,sha256=rOqyDvQXf8aZxVVUF2ZnHOmbx7V7HXtsmUkp-P__cho,8777
108
- openedx_learning-0.11.1.dist-info/WHEEL,sha256=M4n4zmFKzQZk4mLCcycNIzIXO7YPKE_b5Cw4PnhHEg8,109
109
- openedx_learning-0.11.1.dist-info/top_level.txt,sha256=IYFbr5mgiEHd-LOtZmXj3q3a0bkGK1M9LY7GXgnfi4M,33
110
- openedx_learning-0.11.1.dist-info/RECORD,,
107
+ openedx_learning-0.11.3.dist-info/LICENSE.txt,sha256=QTW2QN7q3XszgUAXm9Dzgtu5LXYKbR1SGnqMa7ufEuY,35139
108
+ openedx_learning-0.11.3.dist-info/METADATA,sha256=fLZdsvpznsY606-CD4AdMsn5NygFE35jI0LyhxaqUB0,8777
109
+ openedx_learning-0.11.3.dist-info/WHEEL,sha256=WDDPHYzpiOIm6GP1C2_8y8W6q16ICddAgOHlhTje9Qc,109
110
+ openedx_learning-0.11.3.dist-info/top_level.txt,sha256=IYFbr5mgiEHd-LOtZmXj3q3a0bkGK1M9LY7GXgnfi4M,33
111
+ openedx_learning-0.11.3.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (72.2.0)
2
+ Generator: setuptools (74.0.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py2-none-any
5
5
  Tag: py3-none-any