"""This module contains the signals for the ``versionable`` discrete app"""

from django.db.models.signals import post_save

from .models import Duplicable


# pylint: disable=unused-argument
def post_save_duplicable(sender, instance, **kwargs):
    """Receiver for the ``post_save`` signal to update the ``Versionable`` of a ``Duplicable``

    If a ``Duplicable`` instance is created / updated, we want to also update the linked
    ``Versionable`` instance "updated_at" field.

    Parameters
    ----------
    For the parameters, see ``django.db.models.signals.post_save``

    """

    if not issubclass(sender, Duplicable):
        pass

    try:
        instance.versionable_instance.save()  # This should update the "updated_at" field
    except (AttributeError, TypeError):
        pass


post_save.connect(post_save_duplicable, weak=False, dispatch_uid='post_save_duplicable')
