from django.db.models.signals import pre_save
from django.dispatch import receiver
from django.utils import timezone
from collaborators.models import Access
from institute.models import Institute


@receiver(pre_save, sender=Institute)
def update(sender, instance, **kwargs):
    if instance.tracker.has_changed('name'):
        Access.objects.filter(institute__id=instance.id).update(institute_name=instance.name, modified_at=timezone.now())
    # Note that the tracker which we defined in Institute Model and used here is just tracking fields if the instance of Institute is created before.
    # It means that this tracker does not work at the moment of creating an instance of Institute.