from django.db.models.signals import pre_save
from django.dispatch import receiver
from blacklist import models
from hashlib import sha256


@receiver(pre_save, sender=models.EmailEntry)
def email_entry_handler(sender, instance, **kwargs):
    """
    Handler that assigns to lower_case_entry_value the entry_value.lower()
    """

    instance.lower_case_entry_value = instance.entry_value.lower()
    email_hasher = sha256(instance.lower_case_entry_value.encode())
    instance.hashed_value = email_hasher.hexdigest().lower()


@receiver(pre_save, sender=models.IPEntry)
@receiver(pre_save, sender=models.EmailHostEntry)
def entry_handler(instance, **kwargs):
    """
    Handler that assigns to lower_case_entry_value the entry_value.lower()
    """

    instance.lower_case_entry_value = instance.entry_value.lower()

[((7, 1, 7, 45), 'django.dispatch.receiver', 'receiver', (), '', False, 'from django.dispatch import receiver\n'), ((18, 1, 18, 42), 'django.dispatch.receiver', 'receiver', (), '', False, 'from django.dispatch import receiver\n'), ((19, 1, 19, 49), 'django.dispatch.receiver', 'receiver', (), '', False, 'from django.dispatch import receiver\n')]