<commit_before>import logging

from courriers.settings import FAIL_SILENTLY, DEFAULT_FROM_EMAIL, DEFAULT_FROM_NAME

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils import translation

from .simple import SimpleBackend

logger = logging.getLogger('courriers')


class CampaignBackend(SimpleBackend):
    def send_mails(self, newsletter):
        if not newsletter.is_online():
            raise Exception("This newsletter is not online. You can't send it.")

        nl_list = newsletter.newsletter_list
        self.send_campaign(newsletter, nl_list.list_id, nl_list.segment_id)

    def _format_slug(self, *args):
        raise NotImplementedError

    def send_campaign(self, newsletter, list_id, segment_id):
        if not DEFAULT_FROM_EMAIL:
            raise ImproperlyConfigured("You have to specify a DEFAULT_FROM_EMAIL in Django settings.")
        if not DEFAULT_FROM_NAME:
            raise ImproperlyConfigured("You have to specify a DEFAULT_FROM_NAME in Django settings.")

        try:
            self._send_campaign(newsletter, list_id, segment_id=segment_id)
        except Exception as e:
            logger.exception(e)

            if not FAIL_SILENTLY:
                raise e
        else:
            newsletter.sent = True
            newsletter.save(update_fields=('sent', ))<commit_msg>use the language of the segment, the NL template still has to be translated<commit_after>import logging

from courriers.settings import FAIL_SILENTLY, DEFAULT_FROM_EMAIL, DEFAULT_FROM_NAME

from django.conf import settings
from django.core.exceptions import ImproperlyConfigured
from django.utils import translation

from .simple import SimpleBackend

logger = logging.getLogger('courriers')


class CampaignBackend(SimpleBackend):
    def send_mails(self, newsletter):
        if not newsletter.is_online():
            raise Exception("This newsletter is not online. You can't send it.")

        nl_list = newsletter.newsletter_list
        self.send_campaign(newsletter, nl_list.list_id, nl_list.segment_id)

    def _format_slug(self, *args):
        raise NotImplementedError

    def send_campaign(self, newsletter, list_id, segment_id):
        if not DEFAULT_FROM_EMAIL:
            raise ImproperlyConfigured("You have to specify a DEFAULT_FROM_EMAIL in Django settings.")
        if not DEFAULT_FROM_NAME:
            raise ImproperlyConfigured("You have to specify a DEFAULT_FROM_NAME in Django settings.")

        old_language = translation.get_language()
        language = settings.LANGUAGE_CODE

        if newsletter.newsletter_list.segment_id:
            language = newsletter.newsletter_list.segment.lang

        translation.activate(language)

        try:
            self._send_campaign(newsletter, list_id, segment_id=segment_id)
        except Exception as e:
            logger.exception(e)

            if not FAIL_SILENTLY:
                raise e
        else:
            newsletter.sent = True
            newsletter.save(update_fields=('sent', ))


        translation.activate(old_language)
{'fraction_non_alphanumeric': 0.06524547803617571, 'fraction_numerical': 0.0, 'mean_word_length': 3.080368906455863, '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': '1659991', 'n_tokens_mistral': 861, 'n_tokens_neox': 823, 'n_words': 229}