<commit_before># -*- coding: utf-8 -*-
#
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2012 ITerativ GmbH. All rights reserved.
#
# Created on Mar 19, 2012
# @author: github.com/maersu

from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.utils.encoding import smart_str, force_unicode
import logging
from django.template.loader import render_to_string
from django.contrib.sites.models import Site
from html2text import HTML2Text
from djangojames.string import strip_tags, strip_empty_tags
from djpostman.models import Message
from django.contrib.auth.models import User
from djpostman.utils import get_or_create_contact
from djpostman.task import send_mail_task

logger = logging.getLogger(__name__)

# @todo add attachment 
# http://djangosnippets.org/snippets/1063/

# attach_file() creates a new attachment using a file from your filesystem.
# Call it with the path of the file to attach and, optionally, the MIME type 
# to use for the attachment. If the MIME type is omitted, it will be guessed from the filename. 
# The simplest use would be:
#
# message.attach_file('/images/weather_map.png')

from_email = getattr(settings, 'FROM_EMAIL', settings.EMAIL_HOST_USER)

def render_to_send_multi_mail(subject, template, context, recipient_list, 
                    from_email=from_email, 
                    store=True, names_dict=None):

    context['current_site'] = Site.objects.get_current()
    context['current_domain'] = getattr(settings, 'PROTOCOL', 'http://')+Site.objects.get_current().domain
        
    content = render_to_string(template, context)
    return send_multi_mail(subject, content, recipient_list, from_email, store, names_dict)

def send_multi_mail(subject, content, recipient_list, 
                    from_email=from_email, 
                    store=True,
                    names_dict=None):
    
    def _add_name_dict(user):
        if not names_dict.get(user.email, None):
            names_dict[user.email] = user.get_full_name()        
    
    if names_dict is None:
        names_dict = {}

    if not isinstance(recipient_list, list): 
        recipient_list = [recipient_list]
    
    if len(recipient_list) == 0:
        return 0
    
    recipient_list = list(set(recipient_list))
    
    if store:
        msg = Message()
        msg.subject = smart_str(subject)
        msg.save()
        for u in User.objects.filter(email=from_email):
            msg.sender = u
            _add_name_dict(u)
        
    recipient_list_str = []
    
    for recipient in recipient_list:
        if isinstance(recipient, User):
            if store: 
                msg.recipients.add(recipient)
            _add_name_dict(recipient)

            recipient_list_str.append(recipient.email)
        else:
            if store: 
                for u in User.objects.filter(email=recipient):
                    _add_name_dict(u)
                    msg.recipients.add(u)
            recipient_list_str.append(recipient)
    
    print names_dict
    
    h = HTML2Text()
    h.ignore_images = True
    h.ignore_emphasis = True
    
    email = EmailMultiAlternatives(smart_str(subject), 
                                   smart_str(h.handle(force_unicode(strip_empty_tags(strip_tags(content, ['img', 'script', 'span']))))), 
                                   from_email, <commit_msg>remove print<commit_after># -*- coding: utf-8 -*-
#
# ITerativ GmbH
# http://www.iterativ.ch/
#
# Copyright (c) 2012 ITerativ GmbH. All rights reserved.
#
# Created on Mar 19, 2012
# @author: github.com/maersu

from django.conf import settings
from django.core.mail import EmailMultiAlternatives
from django.utils.encoding import smart_str, force_unicode
import logging
from django.template.loader import render_to_string
from django.contrib.sites.models import Site
from html2text import HTML2Text
from djangojames.string import strip_tags, strip_empty_tags
from djpostman.models import Message
from django.contrib.auth.models import User
from djpostman.utils import get_or_create_contact
from djpostman.task import send_mail_task

logger = logging.getLogger(__name__)

# @todo add attachment 
# http://djangosnippets.org/snippets/1063/

# attach_file() creates a new attachment using a file from your filesystem.
# Call it with the path of the file to attach and, optionally, the MIME type 
# to use for the attachment. If the MIME type is omitted, it will be guessed from the filename. 
# The simplest use would be:
#
# message.attach_file('/images/weather_map.png')

from_email = getattr(settings, 'FROM_EMAIL', settings.EMAIL_HOST_USER)

def render_to_send_multi_mail(subject, template, context, recipient_list, 
                    from_email=from_email, 
                    store=True, names_dict=None):

    context['current_site'] = Site.objects.get_current()
    context['current_domain'] = getattr(settings, 'PROTOCOL', 'http://')+Site.objects.get_current().domain
        
    content = render_to_string(template, context)
    return send_multi_mail(subject, content, recipient_list, from_email, store, names_dict)

def send_multi_mail(subject, content, recipient_list, 
                    from_email=from_email, 
                    store=True,
                    names_dict=None):
    
    def _add_name_dict(user):
        if not names_dict.get(user.email, None):
            names_dict[user.email] = user.get_full_name()        
    
    if names_dict is None:
        names_dict = {}

    if not isinstance(recipient_list, list): 
        recipient_list = [recipient_list]
    
    if len(recipient_list) == 0:
        return 0
    
    recipient_list = list(set(recipient_list))
    
    if store:
        msg = Message()
        msg.subject = smart_str(subject)
        msg.save()
        for u in User.objects.filter(email=from_email):
            msg.sender = u
            _add_name_dict(u)
        
    recipient_list_str = []
    
    for recipient in recipient_list:
        if isinstance(recipient, User):
            if store: 
                msg.recipients.add(recipient)
            _add_name_dict(recipient)

            recipient_list_str.append(recipient.email)
        else:
            if store: 
                for u in User.objects.filter(email=recipient):
                    _add_name_dict(u)
                    msg.recipients.add(u)
            recipient_list_str.append(recipient)

    h = HTML2Text()
    h.ignore_images = True
    h.ignore_emphasis = True
    
    email = EmailMultiAlternatives(smart_str(subject), 
                                   smart_str(h.handle(force_unicode(strip_empty_tags(strip_tags(content, ['img', 'script', 'span']))))), 
                                   from_email, 
{'fraction_non_alphanumeric': 0.08735700490268905, 'fraction_numerical': 0.005942653394740751, 'mean_word_length': 2.648780487804878, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2598125', 'n_tokens_mistral': 2084, 'n_tokens_neox': 1958, 'n_words': 475}