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()

[((142, 186), 'django.dispatch.receiver', 'receiver', (['pre_save'], {'sender': 'models.EmailEntry'}), '(pre_save, sender=models.EmailEntry)\n', (150, 186), False, 'from django.dispatch import receiver\n'), ((531, 572), 'django.dispatch.receiver', 'receiver', (['pre_save'], {'sender': 'models.IPEntry'}), '(pre_save, sender=models.IPEntry)\n', (539, 572), False, 'from django.dispatch import receiver\n'), ((574, 622), 'django.dispatch.receiver', 'receiver', (['pre_save'], {'sender': 'models.EmailHostEntry'}), '(pre_save, sender=models.EmailHostEntry)\n', (582, 622), False, 'from django.dispatch import receiver\n')]