|
4 | 4 | import logging |
5 | 5 |
|
6 | 6 | from django.db.models.signals import post_save |
7 | | -from django.dispatch import receiver, Signal |
| 7 | +from django.dispatch import Signal, receiver |
| 8 | +from openedx_events.content_authoring.data import LibraryBlockData, XBlockData |
| 9 | +from openedx_events.content_authoring.signals import LIBRARY_BLOCK_DELETED, XBLOCK_DELETED |
8 | 10 |
|
9 | | -from lti_consumer.models import LtiAgsScore, Lti1p3Passport, LtiConfiguration |
| 11 | +from lti_consumer.models import Lti1p3Passport, LtiAgsScore, LtiConfiguration |
10 | 12 | from lti_consumer.plugin import compat |
11 | 13 |
|
12 | | - |
13 | 14 | log = logging.getLogger(__name__) |
| 15 | +SignalHandler = compat.get_signal_handler() |
14 | 16 |
|
15 | 17 |
|
16 | 18 | @receiver(post_save, sender=LtiAgsScore, dispatch_uid='publish_grade_on_score_update') |
@@ -87,4 +89,64 @@ def create_lti_1p3_passport(sender, instance: LtiConfiguration, **kwargs): # py |
87 | 89 | instance.create_lti_1p3_passport() |
88 | 90 |
|
89 | 91 |
|
| 92 | +@receiver(SignalHandler.pre_item_delete if SignalHandler else []) |
| 93 | +def delete_child_lti_configurations(**kwargs): |
| 94 | + """ |
| 95 | + Delete lti configurtion from database for this block children. |
| 96 | + """ |
| 97 | + usage_key = kwargs.get('usage_key') |
| 98 | + if usage_key: |
| 99 | + # Strip branch info |
| 100 | + usage_key = usage_key.for_branch(None) |
| 101 | + try: |
| 102 | + deleted_block = compat.load_enough_xblock(usage_key) |
| 103 | + except Exception as e: # pylint: disable=broad-exception-caught |
| 104 | + log.warning(f"Cannot find xblock for key {usage_key}. Reason: {str(e)}. ") |
| 105 | + return |
| 106 | + id_list = {str(deleted_block.location)} |
| 107 | + for block in compat.yield_dynamic_block_descendants(deleted_block, kwargs.get('user_id')): |
| 108 | + id_list.add(str(block.location)) |
| 109 | + |
| 110 | + LtiConfiguration.objects.filter( |
| 111 | + location__in=id_list |
| 112 | + ).delete() |
| 113 | + log.info(f"Deleted {len(id_list)} lti configurations in modulestore") |
| 114 | + result = Lti1p3Passport.objects.filter(lticonfiguration__isnull=True).delete() |
| 115 | + log.info(f"Deleted {result} lti 1.3 passport objects in library") |
| 116 | + |
| 117 | + |
| 118 | +@receiver(XBLOCK_DELETED) |
| 119 | +def delete_lti_configuration(**kwargs): |
| 120 | + """ |
| 121 | + Delete lti configurtion from database for this block. |
| 122 | + """ |
| 123 | + xblock_info = kwargs.get("xblock_info", None) |
| 124 | + if not xblock_info or not isinstance(xblock_info, XBlockData): |
| 125 | + log.error("Received null or incorrect data for event") |
| 126 | + return |
| 127 | + |
| 128 | + LtiConfiguration.objects.filter( |
| 129 | + location=str(xblock_info.usage_key) |
| 130 | + ).delete() |
| 131 | + result = Lti1p3Passport.objects.filter(lticonfiguration__isnull=True).delete() |
| 132 | + log.info(f"Deleted {result} lti 1.3 passport objects in library") |
| 133 | + |
| 134 | + |
| 135 | +@receiver(LIBRARY_BLOCK_DELETED) |
| 136 | +def delete_lib_lti_configuration(**kwargs): |
| 137 | + """ |
| 138 | + Delete lti configurtion from database for this library block. |
| 139 | + """ |
| 140 | + library_block = kwargs.get("library_block", None) |
| 141 | + if not library_block or not isinstance(library_block, LibraryBlockData): |
| 142 | + log.error("Received null or incorrect data for event") |
| 143 | + return |
| 144 | + |
| 145 | + LtiConfiguration.objects.filter( |
| 146 | + location=str(library_block.usage_key) |
| 147 | + ).delete() |
| 148 | + result = Lti1p3Passport.objects.filter(lticonfiguration__isnull=True).delete() |
| 149 | + log.info(f"Deleted {result} lti 1.3 passport objects in library") |
| 150 | + |
| 151 | + |
90 | 152 | LTI_1P3_PROCTORING_ASSESSMENT_STARTED = Signal() |
0 commit comments