from knowledge.utils import get_module
from knowledge import settings


def send_alerts(target_dict, response):
    """
    This can be overridden via KNOWLEDGE_ALERTS_FUNCTION_PATH.
    """
    from django.contrib.auth.models import User
    from django.template.loader import render_to_string
    from django.contrib.sites.models import Site
    from django.core.mail import EmailMultiAlternatives

    site = Site.objects.get_current()

    for email, name in target_dict.items():
        if isinstance(name, User):
            name = '{0} {1}'.format(name.first_name, name.last_name)

        context = {
            'name': name,
            'email': email,
            'response': response,
            'site': site
        }

        subject = render_to_string(
            'django_knowledge/emails/subject.txt', context)

        message = render_to_string(
            'django_knowledge/emails/message.txt', context)

        message_html = render_to_string(
            'django_knowledge/emails/message.html', context)

        msg = EmailMultiAlternatives(subject, message, to=[email])
        msg.attach_alternative(message_html, 'text/html')
        msg.send()


def response_post_save(response, created):
    """
    Gathers all the responses for the sender's parent question
    and shuttles them to the predefined module.

    For some reason I get crazy errors for a standard Django 
    signal. Will revisit.
    """
    func = get_module(settings.ALERTS_FUNCTION_PATH)

    if settings.ALERTS and created:
        # pull together the instances (could be question or response)
        instances = list(response.question.get_responses())
        instances += [response.question]

        # dedupe people who want alerts thanks to dict keys...
        out_dict = dict([[i.get_email(), i.get_user_or_pair()] 
                        for i in instances if i.alert])

        # remove the creator...
        if response.get_email() in out_dict.keys():
            del out_dict[response.get_email()]

        func(out_dict, response)

from knowledge.utils import get_module
from knowledge import settings


def send_alerts(target_dict, response):
    """
    This can be overridden via KNOWLEDGE_ALERTS_FUNCTION_PATH.
    """
    from django.contrib.auth.models import User
    from django.template.loader import render_to_string
    from django.contrib.sites.models import Site
    from django.core.mail import EmailMultiAlternatives

    site = Site.objects.get_current()

    for email, name in target_dict.items():
        if isinstance(name, User):
            name = '{0} {1}'.format(name.first_name, name.last_name)
        else:
            name = name[0]

        context = {
            'name': name,
            'email': email,
            'response': response,
            'site': site
        }

        subject = render_to_string(
            'django_knowledge/emails/subject.txt', context)

        message = render_to_string(
            'django_knowledge/emails/message.txt', context)

        message_html = render_to_string(
            'django_knowledge/emails/message.html', context)

        msg = EmailMultiAlternatives(subject, message, to=[email])
        msg.attach_alternative(message_html, 'text/html')
        msg.send()


def response_post_save(response, created):
    """
    Gathers all the responses for the sender's parent question
    and shuttles them to the predefined module.

    For some reason I get crazy errors for a standard Django 
    signal. Will revisit.
    """
    func = get_module(settings.ALERTS_FUNCTION_PATH)

    if settings.ALERTS and created:
        # pull together the instances (could be question or response)
        instances = list(response.question.get_responses())
        instances += [response.question]

        # dedupe people who want alerts thanks to dict keys...
        out_dict = dict([[i.get_email(), i.get_user_or_pair()] 
                        for i in instances if i.alert])

        # remove the creator...
        if response.get_email() in out_dict.keys():
            del out_dict[response.get_email()]

        func(out_dict, response)

fix tuple glitch