from django.db.models.signals import post_save, post_delete
from django.dispatch import receiver


@receiver(post_save, sender='pricemon.PriceWatcher')
def reschedule_on_save(sender, instance, **kwargs):
    from pricemon.tasks import schedule_watcher, unschedule_watcher
    if instance.enabled:
        schedule_watcher(instance)
    else:
        unschedule_watcher(instance)


@receiver(post_delete, sender='pricemon.PriceWatcher')
def unschedule_on_delete(sender, instance, **kwargs):
    from pricemon.tasks import unschedule_watcher
    unschedule_watcher(instance)
