<commit_before>from django.conf import settings
from django.template.loader import render_to_string

from mail import models as mail_api
from groups import models as group_api
from mailgun import api as mailgun_api
from sequence import models as sequence_api

def send_email( email_uri ):
    """ Send the email to the intended target audience """
    email = mail_api.get_email(email_uri)

    if email['audience'] == 'groups':
        to_address = ','.join([g['address'] for g in group_api.get_groups(email['sequence'])])
    elif email['audience'] == 'individuals':
        to_address = sequence_api.sequence_list_name(email['sequence'])

    text_body = render_to_string('mail/email.txt', {'email': email})
    html_body = render_to_string('mail/email.html', {'email': email})

    mailgun_api.send_email(
        to_address,
        settings.DEFAULT_FROM_EMAIL,
        email['subject'],
        text_body,
        html_body,
        email['tags'].split(','),
        sequence_api.sequence_campaign(email['sequence'])
    )
    mail_api.mark_sent(email_uri)<commit_msg>Send group emails in batches<commit_after>from django.conf import settings
from django.template.loader import render_to_string

from mail import models as mail_api
from groups import models as group_api
from mailgun import api as mailgun_api
from sequence import models as sequence_api

def send_email( email_uri ):
    """ Send the email to the intended target audience """
    email = mail_api.get_email(email_uri)

    if email['audience'] == 'groups':
        to_addresses = [g['address'] for g in group_api.get_groups(email['sequence'])]
    elif email['audience'] == 'individuals':
        to_addresses = [sequence_api.sequence_list_name(email['sequence'])]

    text_body = render_to_string('mail/email.txt', {'email': email})
    html_body = render_to_string('mail/email.html', {'email': email})

    batch_size = 1

    for i in range(0, len(to_addresses), batch_size):
        mailgun_api.send_mass_email(
            to_addresses[i:i+batch_size],
            settings.DEFAULT_FROM_EMAIL,
            email['subject'],
            text_body,
            html_body,
            email['tags'].split(','),
            sequence_api.sequence_campaign(email['sequence'])
        )
    mail_api.mark_sent(email_uri)
{'fraction_non_alphanumeric': 0.112565445026178, 'fraction_numerical': 0.0008726003490401396, 'mean_word_length': 3.6987704918032787, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4174073', 'n_tokens_mistral': 684, 'n_tokens_neox': 669, 'n_words': 163}