# Path: courriers/settings.py
# FAIL_SILENTLY = getattr(settings, "COURRIERS_FAIL_SILENTLY", False)
# 
# DEFAULT_FROM_EMAIL = getattr(
#     settings, "COURRIERS_DEFAULT_FROM_EMAIL", settings.DEFAULT_FROM_EMAIL
# )
# 
# DEFAULT_FROM_NAME = getattr(settings, "COURRIERS_DEFAULT_FROM_NAME", "")
# 
# Path: courriers/backends/simple.py
# class SimpleBackend(BaseBackend):
#     def subscribe(self, list_id, email, lang=None, user=None):
#         pass
# 
#     def unsubscribe(self, list_id, email, lang=None, user=None):
#         pass
# 
#     def register(self, email, newsletter_list, lang=None, user=None):
#         if not user:
#             try:
#                 user = User.objects.get(email=email)
#             except User.DoesNotExist:
#                 user = User.objects.create(email=email, username=email)
# 
#         user.subscribe(newsletter_list, lang=lang)
# 
#     def unregister(self, email, newsletter_list=None, user=None, lang=None):
#         if not user:
#             try:
#                 user = User.objects.get(email=email)
#             except User.DoesNotExist:
#                 return
# 
#         user.unsubscribe(newsletter_list, lang=lang)
# 
#     def send_mails(self, newsletter, fail_silently=False, subscribers=None):
#         connection = mail.get_connection(fail_silently=fail_silently)
# 
#         emails = []
# 
#         old_language = translation.get_language()
# 
#         for subscriber in subscribers:
#             if (
#                 newsletter.newsletter_segment.lang
#                 and newsletter.newsletter_segment.lang != subscriber.lang
#             ):
#                 continue
# 
#             translation.activate(subscriber.lang)
# 
#             email = EmailMultiAlternatives(
#                 newsletter.name,
#                 render_to_string(
#                     "courriers/newsletter_raw_detail.txt",
#                     {"object": newsletter, "subscriber": subscriber},
#                 ),
#                 DEFAULT_FROM_EMAIL,
#                 [subscriber.email],
#                 connection=connection,
#             )
# 
#             html = render_to_string(
#                 "courriers/newsletter_raw_detail.html",
#                 {
#                     "object": newsletter,
#                     "items": newsletter.items.all().prefetch_related("newsletter"),
#                     "subscriber": subscriber,
#                 },
#             )
# 
#             for pre_processor in PRE_PROCESSORS:
#                 html = load_class(pre_processor)(html)
# 
#             email.attach_alternative(html, "text/html")
# 
#             emails.append(email)
# 
#         translation.activate(old_language)
# 
#         results = connection.send_messages(emails)
# 
#         newsletter.sent = True
#         newsletter.save(update_fields=("sent",))
# 
#         return results


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
        nl_segment = newsletter.newsletter_segment
        self.send_campaign(newsletter, nl_list.list_id, nl_segment.segment_id)


# Path: courriers/settings.py
# FAIL_SILENTLY = getattr(settings, "COURRIERS_FAIL_SILENTLY", False)
# 
# DEFAULT_FROM_EMAIL = getattr(
#     settings, "COURRIERS_DEFAULT_FROM_EMAIL", settings.DEFAULT_FROM_EMAIL
# )
# 
# DEFAULT_FROM_NAME = getattr(settings, "COURRIERS_DEFAULT_FROM_NAME", "")
# 
# Path: courriers/backends/simple.py
# class SimpleBackend(BaseBackend):
#     def subscribe(self, list_id, email, lang=None, user=None):
#         pass
# 
#     def unsubscribe(self, list_id, email, lang=None, user=None):
#         pass
# 
#     def register(self, email, newsletter_list, lang=None, user=None):
#         if not user:
#             try:
#                 user = User.objects.get(email=email)
#             except User.DoesNotExist:
#                 user = User.objects.create(email=email, username=email)
# 
#         user.subscribe(newsletter_list, lang=lang)
# 
#     def unregister(self, email, newsletter_list=None, user=None, lang=None):
#         if not user:
#             try:
#                 user = User.objects.get(email=email)
#             except User.DoesNotExist:
#                 return
# 
#         user.unsubscribe(newsletter_list, lang=lang)
# 
#     def send_mails(self, newsletter, fail_silently=False, subscribers=None):
#         connection = mail.get_connection(fail_silently=fail_silently)
# 
#         emails = []
# 
#         old_language = translation.get_language()
# 
#         for subscriber in subscribers:
#             if (
#                 newsletter.newsletter_segment.lang
#                 and newsletter.newsletter_segment.lang != subscriber.lang
#             ):
#                 continue
# 
#             translation.activate(subscriber.lang)
# 
#             email = EmailMultiAlternatives(
#                 newsletter.name,
#                 render_to_string(
#                     "courriers/newsletter_raw_detail.txt",
#                     {"object": newsletter, "subscriber": subscriber},
#                 ),
#                 DEFAULT_FROM_EMAIL,
#                 [subscriber.email],
#                 connection=connection,
#             )
# 
#             html = render_to_string(
#                 "courriers/newsletter_raw_detail.html",
#                 {
#                     "object": newsletter,
#                     "items": newsletter.items.all().prefetch_related("newsletter"),
#                     "subscriber": subscriber,
#                 },
#             )
# 
#             for pre_processor in PRE_PROCESSORS:
#                 html = load_class(pre_processor)(html)
# 
#             email.attach_alternative(html, "text/html")
# 
#             emails.append(email)
# 
#         translation.activate(old_language)
# 
#         results = connection.send_messages(emails)
# 
#         newsletter.sent = True
#         newsletter.save(update_fields=("sent",))
# 
#         return results


# Path: courriers/backends/campaign.py
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
        nl_segment = newsletter.newsletter_segment
        self.send_campaign(newsletter, nl_list.list_id, nl_segment.segment_id)


    def _format_slug(self, *args):