Skip to content
Draft
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
40 changes: 40 additions & 0 deletions src/openedx_content/applets/publishing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@
"publish_from_drafts",
"get_draft_version",
"get_published_version",
"get_entity_draft_history",
"set_draft_version",
"soft_delete_draft",
"reset_drafts_to_published",
Expand Down Expand Up @@ -584,6 +585,45 @@ def get_published_version(publishable_entity_or_id: PublishableEntity | int, /)
return published.version


def get_entity_draft_history(
publishable_entity_or_id: PublishableEntity | int, /
) -> QuerySet[DraftChangeLogRecord]:
"""
Return DraftChangeLogRecords for a PublishableEntity since its last publication,
ordered from most recent to oldest.

If the entity has never been published, all DraftChangeLogRecords are returned.
Copy link
Contributor

Choose a reason for hiding this comment

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

What is the intended behavior of this function when the last "publish" was a deletion?

Copy link
Contributor

Choose a reason for hiding this comment

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

I think the current code does work in this case, to be clear. And I realize that it's not something that's likely to come up in the UX. I just want to make sure that the behavior is well defined and tested for in that edge case.

"""
if isinstance(publishable_entity_or_id, int):
entity_id = publishable_entity_or_id
else:
entity_id = publishable_entity_or_id.pk

qs = (
DraftChangeLogRecord.objects
.filter(entity_id=entity_id)
.select_related(
"draft_change_log__changed_by",
"old_version",
"new_version",
)
.order_by("-draft_change_log__changed_at")
)

# Narrow to changes since the last publication
try:
published = Published.objects.select_related(
"publish_log_record__publish_log"
).get(entity_id=entity_id)
qs = qs.filter(
draft_change_log__changed_at__gt=published.publish_log_record.publish_log.published_at
)
except Published.DoesNotExist:
pass

return qs


def set_draft_version(
draft_or_id: Draft | int,
publishable_entity_version_pk: int | None,
Expand Down