from django.db.models.signals import post_save, pre_delete
from django.contrib.auth.models import User
from django.dispatch import receiver
from .models import Profile, Relationship

@receiver(post_save, sender=User)
def create_profile(sender, instance, created, **kwargs):
  if created:
    Profile.objects.create(user=instance)

@receiver(post_save, sender=Relationship)
def add_to_friends(sender, instance, **kwargs):
  sender_ = instance.sender
  receiver_ = instance.receiver
  if instance.status == 'accepted':
    sender_.friends.add(receiver_.user)
    receiver_.friends.add(sender_.user)
    sender_.save()
    receiver_.save()

@receiver(pre_delete, sender=Relationship)
def remove_from_friends(sender, instance, **kwargs):
  sender_ = instance.sender
  receiver_ = instance.receiver
  sender_.friends.remove(receiver_.user)
  receiver_.friends.remove(sender_.user)
  sender_.save()
  receiver_.save()