Skip to content
Merged
Changes from 1 commit
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
18 changes: 14 additions & 4 deletions openedx_learning/apps/authoring/publishing/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,20 @@ def get_all_drafts(learning_package_id: int, /) -> QuerySet[Draft]:
)


def get_entities_with_unpublished_changes(learning_package_id: int, /) -> QuerySet[PublishableEntity]:
return PublishableEntity.objects \
.filter(learning_package_id=learning_package_id) \
.exclude(draft__version=F('published__version'))
def get_entities_with_unpublished_changes(
learning_package_id: int,
/,
include_deleted_drafts: bool = False
) -> QuerySet[PublishableEntity]:
"""
Fetch entities that have unpublished changes.

By default, this excludes soft-deleted drafts but can be included using include_deleted_drafts option.
"""
query_filters = {"learning_package_id": learning_package_id}
if not include_deleted_drafts:
query_filters['draft__version__isnull'] = False
return PublishableEntity.objects.filter(**query_filters).exclude(draft__version=F('published__version'))

@ormsbee ormsbee Aug 15, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This will erroneously return soft-deleted entries that have already been published, because of the NULL comparisons. I think that if we're making this flag, it needs to be something like:

entities_qs = (
    PublishableEntity.objects
    .filter(learning_package_id=learning_package_id)
    .exclude(draft__version=F('published__version'))
)

if include_deleted_drafts:
    # This means that we should also return PublishableEntities where the draft
    # has been soft-deleted, but that deletion has not been published yet. Just
    # excluding records where the Draft and Published versions don't match won't
    # be enough here, because that will return soft-deletes that have already
    # been published (since NULL != NULL in SQL).
    # 
    # So we explicitly exclude already-published soft-deletes:
    return entities_qs.exclude(
        Q(draft__version__isnull=True) & Q(published__version__isnull=True)
    )

# Simple case: exclude all entities that have been soft-deleted.
return entities_qs.exclude(draft__version__isnull=True)

(I had to deal with this in #211, which I'd love to get a review of if you have a moment.)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

@ormsbee Makes sense. Updated and included a test inspired by your PR: #211



def get_entities_with_unpublished_deletes(learning_package_id: int, /) -> QuerySet[PublishableEntity]:
Expand Down