Run action when I delete an xblock from studio

75 views
Skip to first unread message

Juan Carlos Hierro

unread,
Feb 17, 2017, 12:50:26 PM2/17/17
to General Open edX discussion
Good,

I am developing an xblock and I need to execute an action (hook) when I delete this xblock from the studio. If possible I would like a solution without having to modify the core.

Thanks and regards!

David Ormsbee

unread,
Feb 17, 2017, 3:22:41 PM2/17/17
to edx code
Hi Juan,

There's a Django signal emitted whenever an item is deleted from a course. You should be able to listen for that with your own code without touching anything in core. You can see an example of listening for it here:


Keep in mind that catching the signal happens synchronously in the same transaction as the request to delete the item. If you're going to do anything that might fail or take a long time (like a POST over a network connection), you'll probably want to make "my_handler" spin off a celery task instead so that it can run in the background and not slow down the act of deletion. It might look something like this:

from django.dispatch import receiver

from xmodule.modulestore.django import SignalHandler
from lms import CELERY_APP

@receiver(SignalHandler.item_deleted)
def my_handler(**kwargs):
    usage_key = kwargs.get('usage_key') # This is what identifies the unit that was deleted
    user_id = kwargs.get('user_id')     # This is the user who did the deleting
    course_key = usage_key.course_key   # The identifier for the course it was deleted from.

    do_something_slow.apply_async(args=(usage_key,))

@CELERY_APP.task(name='mymodule.tasks.do_something_slow')
def do_something_slow(usage_key):
    # Whatever slow operation you need to do.
    # If you need to make a network request, please use the "requests" library and
    # be sure to use timeouts.

I'm a little fuzzy on that celery syntax, but I think that's it. Hope that helps.

Take care.

Dave

--
You received this message because you are subscribed to the Google Groups "General Open edX discussion" group.
To view this discussion on the web visit https://groups.google.com/d/msgid/edx-code/c687fc26-8fd9-4641-9bbd-4cd59ac007fb%40googlegroups.com.

Juan Carlos Hierro

unread,
Feb 18, 2017, 1:05:10 PM2/18/17
to General Open edX discussion
Thanks for your help. But before I update the open edx version because the "SignalHandler" class does not have the "item_deleted" attribute.
Reply all
Reply to author
Forward
0 new messages