<commit_before>import registration
import mail

from .ids import parse_tracking, MAIL_EVENT, MAIL_NEWS, MAIL_REGISTRATION

import logging
logger = logging.getLogger("helfertool.mail")


def _handle_event(uuid_str, deliverynotification):
    # uuid if tracking_uuid of SentMail
    try:
        sentmail = mail.models.SentMail.objects.get(tracking_uuid=uuid_str)
    except (mail.models.SentMail.DoesNotExist, mail.models.SentMail.MultipleObjectsReturned):
        return False

    # now find the MailDelivery based on the mail address
    maildeliveries = mail.models.MailDelivery.objects.filter(sentmail=sentmail,
                                                             helper__email=deliverynotification.recipient)
    if not maildeliveries.exists():
        return False

    for delivery in maildeliveries:
        delivery.failed = deliverynotification.error_text
        delivery.save()

        logger.info("mail handled", extra={
            'type': MAIL_EVENT,
            'event': delivery.helper.event,
            'helper': delivery.helper,
            'mail_tracking': sentmail.tracking_uuid,
        })

    return True


def _handle_registration(uuid_str, deliverynotification):
    # uuid is PK of helper
    try:
        helper = registration.models.Helper.objects.get(id=uuid_str)
        helper.mail_failed = deliverynotification.error_text
        helper.save()

        logger.info("mail handled", extra={
            'type': MAIL_REGISTRATION,
            'helper': helper,
        })

        return True
    except registration.models.Helper.DoesNotExist:
        return False


_handlers = {
    MAIL_EVENT: _handle_event,
    MAIL_NEWS: None,
    MAIL_REGISTRATION: _handle_registration,
}


def handle_tracking(header_value, deliverynotification):
    # parse header
    try:
        msg_type, uuid_str = parse_tracking(header_value)
    except ValueError:
        return False

    # call handler
    handler = _handlers.get(msg_type, None)
    if handler:
        return handler(uuid_str, deliverynotification)
    return False
<commit_msg>add event to "mail handled" log entry<commit_after>import registration
import mail

from .ids import parse_tracking, MAIL_EVENT, MAIL_NEWS, MAIL_REGISTRATION

import logging
logger = logging.getLogger("helfertool.mail")


def _handle_event(uuid_str, deliverynotification):
    # uuid if tracking_uuid of SentMail
    try:
        sentmail = mail.models.SentMail.objects.get(tracking_uuid=uuid_str)
    except (mail.models.SentMail.DoesNotExist, mail.models.SentMail.MultipleObjectsReturned):
        return False

    # now find the MailDelivery based on the mail address
    maildeliveries = mail.models.MailDelivery.objects.filter(sentmail=sentmail,
                                                             helper__email=deliverynotification.recipient)
    if not maildeliveries.exists():
        return False

    for delivery in maildeliveries:
        delivery.failed = deliverynotification.error_text
        delivery.save()

        logger.info("mail handled", extra={
            'type': MAIL_EVENT,
            'event': delivery.helper.event,
            'helper': delivery.helper,
            'mail_tracking': sentmail.tracking_uuid,
        })

    return True


def _handle_registration(uuid_str, deliverynotification):
    # uuid is PK of helper
    try:
        helper = registration.models.Helper.objects.get(id=uuid_str)
        helper.mail_failed = deliverynotification.error_text
        helper.save()

        logger.info("mail handled", extra={
            'type': MAIL_REGISTRATION,
            'event': helper.event,
            'helper': helper,
        })

        return True
    except registration.models.Helper.DoesNotExist:
        return False


_handlers = {
    MAIL_EVENT: _handle_event,
    MAIL_NEWS: None,
    MAIL_REGISTRATION: _handle_registration,
}


def handle_tracking(header_value, deliverynotification):
    # parse header
    try:
        msg_type, uuid_str = parse_tracking(header_value)
    except ValueError:
        return False

    # call handler
    handler = _handlers.get(msg_type, None)
    if handler:
        return handler(uuid_str, deliverynotification)
    return False

{'fraction_non_alphanumeric': 0.07816583511522927, 'fraction_numerical': 0.0, 'mean_word_length': 2.725663716814159, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2618490', 'n_tokens_mistral': 1186, 'n_tokens_neox': 1155, 'n_words': 262}