Skip to content

Commit

Permalink
feat: adds get_collection_components to the authoring api
Browse files Browse the repository at this point in the history
  • Loading branch information
pomegranited committed Sep 20, 2024
1 parent 3a00a82 commit 290e79b
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 50 deletions.
16 changes: 16 additions & 0 deletions openedx_learning/apps/authoring/components/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"get_component_by_uuid",
"get_component_version_by_uuid",
"component_exists_by_key",
"get_collection_components",
"get_components",
"create_component_version_content",
"look_up_component_version_content",
Expand Down Expand Up @@ -339,6 +340,21 @@ def get_components(
return qset


def get_collection_components(
learning_package_id: int,
collection_key: str,
) -> QuerySet[Component]:
"""
Returns a QuerySet of Components relating to the PublishableEntities in a Collection.
Components have a one-to-one relationship with PublishableEntity, but the reverse may not always be true.
"""
return Component.objects.filter(
learning_package_id=learning_package_id,
publishable_entity__collections__key=collection_key,
).order_by('pk')


def look_up_component_version_content(
learning_package_key: str,
component_key: str,
Expand Down
108 changes: 58 additions & 50 deletions tests/openedx_learning/apps/authoring/collections/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
from openedx_learning.api.authoring_models import (
Collection,
CollectionPublishableEntity,
Component,
ComponentType,
LearningPackage,
PublishableEntity,
PublishableEntityVersion,
)
from openedx_learning.lib.test_utils import TestCase

Expand Down Expand Up @@ -208,13 +209,13 @@ def test_create_collection_without_description(self):

class CollectionEntitiesTestCase(CollectionsTestCase):
"""
Base class with collections that contain publishable entitites.
Base class with collections that contain components.
"""
published_entity: PublishableEntity
pe_version: PublishableEntityVersion
draft_entity: PublishableEntity
de_version: PublishableEntityVersion
published_component: Component
draft_component: Component
user: User # type: ignore [valid-type]
html_type: ComponentType
problem_type: ComponentType

@classmethod
def setUpTestData(cls) -> None:
Expand All @@ -228,17 +229,15 @@ def setUpTestData(cls) -> None:
email="[email protected]",
)

# Make and Publish one PublishableEntity
cls.published_entity = api.create_publishable_entity(
cls.html_type = api.get_or_create_component_type("xblock.v1", "html")
cls.problem_type = api.get_or_create_component_type("xblock.v1", "problem")

# Make and publish one Component
cls.published_component, _ = api.create_component_and_version(
cls.learning_package.id,
key="my_entity_published_example",
created=cls.now,
created_by=cls.user.id,
)
cls.pe_version = api.create_publishable_entity_version(
cls.published_entity.id,
version_num=1,
title="An Entity that we'll Publish 🌴",
cls.problem_type,
local_key="my_published_example",
title="My published problem",
created=cls.now,
created_by=cls.user.id,
)
Expand All @@ -248,43 +247,38 @@ def setUpTestData(cls) -> None:
published_at=cls.now,
)

# Create two Draft PublishableEntities, one in each learning package
cls.draft_entity = api.create_publishable_entity(
# Create a Draft component, one in each learning package
cls.draft_component, _ = api.create_component_and_version(
cls.learning_package.id,
key="my_entity_draft_example",
created=cls.now,
created_by=cls.user.id,
)
cls.de_version = api.create_publishable_entity_version(
cls.draft_entity.id,
version_num=1,
title="An Entity that we'll keep in Draft 🌴",
cls.html_type,
local_key="my_draft_example",
title="My draft html",
created=cls.now,
created_by=cls.user.id,
)

# Add some shared entities to the collections
# Add some shared components to the collections
cls.collection1 = api.add_to_collection(
cls.learning_package.id,
key=cls.collection1.key,
entities_qset=PublishableEntity.objects.filter(id__in=[
cls.published_entity.id,
cls.published_component.pk,
]),
created_by=cls.user.id,
)
cls.collection2 = api.add_to_collection(
cls.learning_package.id,
key=cls.collection2.key,
entities_qset=PublishableEntity.objects.filter(id__in=[
cls.published_entity.id,
cls.draft_entity.id,
cls.published_component.pk,
cls.draft_component.pk,
]),
)
cls.disabled_collection = api.add_to_collection(
cls.learning_package.id,
key=cls.disabled_collection.key,
entities_qset=PublishableEntity.objects.filter(id__in=[
cls.published_entity.id,
cls.published_component.pk,
]),
)

Expand All @@ -299,11 +293,11 @@ def test_create_collection_entities(self):
Ensure the collections were pre-populated with the expected publishable entities.
"""
assert list(self.collection1.entities.all()) == [
self.published_entity,
self.published_component.publishable_entity,
]
assert list(self.collection2.entities.all()) == [
self.published_entity,
self.draft_entity,
self.published_component.publishable_entity,
self.draft_component.publishable_entity,
]
assert not list(self.collection3.entities.all())

Expand All @@ -317,14 +311,14 @@ def test_add_to_collection(self):
self.learning_package.id,
self.collection1.key,
PublishableEntity.objects.filter(id__in=[
self.draft_entity.id,
self.draft_component.pk,
]),
created_by=self.user.id,
)

assert list(self.collection1.entities.all()) == [
self.published_entity,
self.draft_entity,
self.published_component.publishable_entity,
self.draft_component.publishable_entity,
]
for collection_entity in CollectionPublishableEntity.objects.filter(collection=self.collection1):
assert collection_entity.created_by == self.user
Expand All @@ -340,13 +334,13 @@ def test_add_to_collection_again(self):
self.learning_package.id,
self.collection2.key,
PublishableEntity.objects.filter(id__in=[
self.published_entity.id,
self.published_component.pk,
]),
)

assert list(self.collection2.entities.all()) == [
self.published_entity,
self.draft_entity,
self.published_component.publishable_entity,
self.draft_component.publishable_entity,
]
assert self.collection2.modified == modified_time

Expand All @@ -359,7 +353,7 @@ def test_add_to_collection_wrong_learning_package(self):
self.learning_package_2.id,
self.collection3.key,
PublishableEntity.objects.filter(id__in=[
self.published_entity.id,
self.published_component.pk,
]),
)

Expand All @@ -375,12 +369,12 @@ def test_remove_from_collection(self):
self.learning_package.id,
self.collection2.key,
PublishableEntity.objects.filter(id__in=[
self.published_entity.id,
self.published_component.pk,
]),
)

assert list(self.collection2.entities.all()) == [
self.draft_entity,
self.draft_component.publishable_entity,
]
assert self.collection2.modified == modified_time

Expand All @@ -390,13 +384,27 @@ def test_get_entity_collections(self):
"""
collections = api.get_entity_collections(
self.learning_package.id,
self.published_entity.key,
self.published_component.publishable_entity.key,
)
assert list(collections) == [
self.collection1,
self.collection2,
]

def test_get_collection_components(self):
assert list(api.get_collection_components(
self.learning_package.id,
self.collection1.key,
)) == [self.published_component]
assert list(api.get_collection_components(
self.learning_package.id,
self.collection2.key,
)) == [self.published_component, self.draft_component]
assert not list(api.get_collection_components(
self.learning_package.id,
self.collection3.key,
))


class UpdateCollectionTestCase(CollectionTestCase):
"""
Expand Down Expand Up @@ -508,8 +516,8 @@ def test_soft_delete(self):
assert collection == api.get_collection(self.learning_package.id, collection.key)
# ...and the collection's entities remain intact.
assert list(collection.entities.all()) == [
self.published_entity,
self.draft_entity,
self.published_component.publishable_entity,
self.draft_component.publishable_entity,
]

def test_delete(self):
Expand All @@ -532,11 +540,11 @@ def test_delete(self):
# ...and the entities have been removed from this collection
assert list(api.get_entity_collections(
self.learning_package.id,
self.published_entity.key,
self.published_component.publishable_entity.key,
)) == [self.collection1]
assert not list(api.get_entity_collections(
self.learning_package.id,
self.draft_entity.key,
self.draft_component.publishable_entity.key,
))

def test_restore(self):
Expand All @@ -561,6 +569,6 @@ def test_restore(self):
assert collection == api.get_collection(self.learning_package.id, collection.key)
# ...and the collection's entities remain intact.
assert list(collection.entities.all()) == [
self.published_entity,
self.draft_entity,
self.published_component.publishable_entity,
self.draft_component.publishable_entity,
]

0 comments on commit 290e79b

Please sign in to comment.