|
3 | 3 | """ |
4 | 4 |
|
5 | 5 | import logging |
| 6 | +from functools import partial |
6 | 7 |
|
7 | 8 | from attrs import asdict |
8 | 9 | from django.dispatch import receiver |
@@ -45,15 +46,26 @@ def entities_updated( |
45 | 46 | except ContentLibrary.DoesNotExist: |
46 | 47 | return # We don't care about non-library events. |
47 | 48 |
|
48 | | - entities_changed = [change.entity_id for change in change_log.changes] |
| 49 | + # Which entities were _directly_ changed here? |
| 50 | + direct_changes = [asdict(change) for change in change_log.changes if change.new_version != change.old_version] |
| 51 | + # And which entities were indirectly affected (e.g. parent containers)? |
| 52 | + indirect_changes = [asdict(change) for change in change_log.changes if change.new_version == change.old_version] |
49 | 53 |
|
50 | | - if len(entities_changed) == 1: |
51 | | - fn = tasks.send_change_events_for_modified_entities |
| 54 | + update_task_fn = tasks.send_change_events_for_modified_entities |
| 55 | + update_sync = partial(update_task_fn, learning_package_id=learning_package.id) |
| 56 | + update_async = partial(update_task_fn.delay, learning_package_id=learning_package.id) |
| 57 | + |
| 58 | + if len(direct_changes) == 1: |
| 59 | + # We directly changed only one entity. Update it synchronously so that the UI will reflect changes right away. |
| 60 | + if len(indirect_changes) <= 1: |
| 61 | + # And update any other affected entity synchronously too; there's at most one. (More efficient, better UX.) |
| 62 | + update_sync(change_list=[*direct_changes, *indirect_changes]) |
| 63 | + else: |
| 64 | + update_sync(change_list=direct_changes) # Update this one entity synchronously, and |
| 65 | + update_async(change_list=indirect_changes) # update the many other affects entities async. |
52 | 66 | else: |
53 | 67 | # More than one entity was changed at once. Handle asynchronously: |
54 | | - fn = tasks.send_change_events_for_modified_entities.delay |
55 | | - |
56 | | - fn(learning_package_id=learning_package.id, change_list=[asdict(chg) for chg in change_log.changes]) |
| 68 | + update_async(change_list=[*direct_changes, *indirect_changes]) |
57 | 69 |
|
58 | 70 |
|
59 | 71 | @receiver(content_signals.LEARNING_PACKAGE_ENTITIES_PUBLISHED) |
|
0 commit comments