Skip to content

Commit

Permalink
refactor: improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Oct 4, 2024
1 parent 5c6b7b9 commit ba7dbd1
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions openedx_learning/apps/authoring/components/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -613,15 +613,15 @@ def set_collections(
component: Component,
collection_qset: QuerySet[Collection],
created_by: int | None = None,
) -> Component:
) -> set[Collection]:
"""
Set collections for a given component.
These Collections must belong to the same LearningPackage as the Component, or a ValidationError will be raised.
The collection_qset object's modified date is updated.
Modified date of all collections related to component is updated.
Returns the updated component object.
Returns the updated collections.
"""
# Disallow adding entities outside the collection's learning package
invalid_collection = collection_qset.exclude(learning_package_id=learning_package_id).first()
Expand All @@ -635,14 +635,18 @@ def set_collections(
relations = CollectionPublishableEntity.objects.filter(
entity=component.publishable_entity
).exclude(collection__in=collection_qset)
removed_collections = [relation.collection for relation in relations]
removed_collections = set(relation.collection for relation in relations)
relations.delete()
component.publishable_entity.collections.add(
*collection_qset.all(),
through_defaults={"created_by_id": created_by},
)
for collection in list(collection_qset.all()) + removed_collections:
collection.modified = datetime.now(tz=timezone.utc)
collection.save()

return component
# Update modified date via update to avoid triggering post_save signal for collections
# The signal triggers index update for each collection synchronously which will be very slow in this case.
# Instead trigger the index update in the caller function asynchronously.
affected_collection = removed_collections | set(collection_qset.all())
Collection.objects.filter(
id__in=[collection.id for collection in affected_collection]
).update(modified=datetime.now(tz=timezone.utc))

return affected_collection

0 comments on commit ba7dbd1

Please sign in to comment.