from django.db.models.signals import post_save
from django.contrib.auth.models import User
from django.dispatch import receiver
from .models import ShinyUserHash
from secrets import token_hex

@receiver(post_save, sender=User)
def create_hash(sender, instance, created, **kwargs):
    if created:
        hash = token_hex(16)
        ShinyUserHash.objects.create(user=instance, user_hash=hash)


@receiver(post_save, sender=User)
def save_hash(sender, instance, created, **kwargs):
    instance.shinyuserhash.save()
