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:

https://github.com/edx/edx-platform/blob/dda92d13198b935f8ebaaaf1ab29b3f05c2ec6ea/cms/djangoapps/contentstore/signals.py#L63-L75

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

On Fri, Feb 17, 2017 at 12:50 PM, Juan Carlos Hierro <[email protected]
> wrote:

> 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!
>
> --
> 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
> <https://groups.google.com/d/msgid/edx-code/c687fc26-8fd9-4641-9bbd-4cd59ac007fb%40googlegroups.com?utm_medium=email&utm_source=footer>
> .
>

-- 
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/CAO_oFPyZ8GwpKvuYT8SUoiRDoT7qu2_XHDFBEetPHttFw0nhfA%40mail.gmail.com.

Reply via email to