from django.dispatch import receiver

from oscar.apps.order.signals import order_line_status_changed
from oscar.core.loading import get_class

from ocyan.plugin.oscar.pipeline import PIPELINE_STATUS_CANCELLED

EventHandler = get_class("order.processing", "EventHandler")


# pylint: disable=unused-argument
@receiver(order_line_status_changed)
def handle_order_line_status_changed(line, old_status, new_status, **kwargs):
    if old_status != new_status and new_status == PIPELINE_STATUS_CANCELLED:
        EventHandler().cancel_stock_allocations(line.order, [line], [line.quantity])
