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

Add filter_publishable_entities to publishing API [FC-0062] #257

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion openedx_learning/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
Open edX Learning ("Learning Core").
"""

__version__ = "0.17.0"
__version__ = "0.18.0"
20 changes: 20 additions & 0 deletions openedx_learning/apps/authoring/publishing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
"soft_delete_draft",
"reset_drafts_to_published",
"register_content_models",
"filter_publishable_entities",
]


Expand Down Expand Up @@ -493,3 +494,22 @@ def ready(self):
return PublishableContentModelRegistry.register(
content_model_cls, content_version_model_cls
)


def filter_publishable_entities(
entities: QuerySet[PublishableEntity],
has_draft=None,
has_published=None
) -> QuerySet[PublishableEntity]:
"""
Filter an entities query set.

has_draft: You can filter by entities that has a draft or not.
has_published: You can filter by entities that has a published version or not.
"""
if has_draft is not None:
entities = entities.filter(draft__version__isnull=not has_draft)
if has_published is not None:
entities = entities.filter(published__version__isnull=not has_published)

return entities
74 changes: 74 additions & 0 deletions tests/openedx_learning/apps/authoring/publishing/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,80 @@ def test_get_entities_with_unpublished_changes(self) -> None:
# should not return published soft-deleted entities.
assert len(entities) == 0

def test_filter_publishable_entities(self) -> None:
count_published = 7
count_drafts = 6
count_no_drafts = 3

for index in range(count_published):
# Create entities to publish
entity = publishing_api.create_publishable_entity(
self.learning_package.id,
f"entity_published_{index}",
created=self.now,
created_by=None,
)

publishing_api.create_publishable_entity_version(
entity.id,
version_num=1,
title=f"Entity_published_{index}",
created=self.now,
created_by=None,
)

publishing_api.publish_all_drafts(self.learning_package.id)

for index in range(count_drafts):
# Create entities with drafts
entity = publishing_api.create_publishable_entity(
self.learning_package.id,
f"entity_draft_{index}",
created=self.now,
created_by=None,
)

publishing_api.create_publishable_entity_version(
entity.id,
version_num=1,
title=f"Entity_draft_{index}",
created=self.now,
created_by=None,
)

for index in range(count_no_drafts):
# Create entities without drafts
entity = publishing_api.create_publishable_entity(
self.learning_package.id,
f"entity_no_draft_{index}",
created=self.now,
created_by=None,
)

drafts = publishing_api.filter_publishable_entities(
PublishableEntity.objects.all(),
has_draft=True,
)
assert drafts.count() == (count_published + count_drafts)

no_drafts = publishing_api.filter_publishable_entities(
PublishableEntity.objects.all(),
has_draft=False,
)
assert no_drafts.count() == count_no_drafts

published = publishing_api.filter_publishable_entities(
PublishableEntity.objects.all(),
has_published=True,
)
assert published.count() == count_published

no_published = publishing_api.filter_publishable_entities(
PublishableEntity.objects.all(),
has_published=False,
)
assert no_published.count() == (count_drafts + count_no_drafts)

def _get_published_version_num(self, entity: PublishableEntity) -> int | None:
published_version = publishing_api.get_published_version(entity.id)
if published_version is not None:
Expand Down