# Path: courriers/settings.py
# MAILJET_API_KEY = getattr(settings, "COURRIERS_MAILJET_API_KEY", "")
# 
# DEFAULT_FROM_EMAIL = getattr(
#     settings, "COURRIERS_DEFAULT_FROM_EMAIL", settings.DEFAULT_FROM_EMAIL
# )
# 
# DEFAULT_FROM_NAME = getattr(settings, "COURRIERS_DEFAULT_FROM_NAME", "")
# 
# MAILJET_API_SECRET_KEY = getattr(settings, "COURRIERS_MAILJET_API_SECRET_KEY", "")
# 
# PRE_PROCESSORS = getattr(settings, "COURRIERS_PRE_PROCESSORS", ())
# 
# Path: courriers/backends/campaign.py
# 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):
#         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 = newsletter.newsletter_segment.lang or settings.LANGUAGE_CODE
# 
#         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)
# 
#     def subscribe(self, email, list_id):
#         pass
# 
#     def unsubscribe(self, email, list_id):
#         pass
# 
# Path: courriers/utils.py
# def load_class(class_path, setting_name=None):
#     """
#     Loads a class given a class_path. The setting value may be a string or a
#     tuple.
# 
#     The setting_name parameter is only there for pretty error output, and
#     therefore is optional
#     """
#     if not isinstance(class_path, str):
#         try:
#             class_path, app_label = class_path
#         except Exception:
#             if setting_name:
#                 raise exceptions.ImproperlyConfigured(
#                     CLASS_PATH_ERROR % (setting_name, setting_name)
#                 )
#             else:
#                 raise exceptions.ImproperlyConfigured(
#                     CLASS_PATH_ERROR % ("this setting", "It")
#                 )
# 
#     try:
#         class_module, class_name = class_path.rsplit(".", 1)
#     except ValueError:
#         if setting_name:
#             txt = "%s isn't a valid module. Check your %s setting" % (
#                 class_path,
#                 setting_name,
#             )
#         else:
#             txt = "%s isn't a valid module." % class_path
#         raise exceptions.ImproperlyConfigured(txt)
# 
#     try:
#         mod = import_module(class_module)
#     except ImportError as e:
#         if setting_name:
#             txt = 'Error importing backend %s: "%s". Check your %s setting' % (
#                 class_module,
#                 e,
#                 setting_name,
#             )
#         else:
#             txt = 'Error importing backend %s: "%s".' % (class_module, e)
# 
#         raise exceptions.ImproperlyConfigured(txt)
# 
#     try:
#         clazz = getattr(mod, class_name)
#     except AttributeError:
#         if setting_name:
#             txt = (
#                 'Backend module "%s" does not define a "%s" class. Check'
#                 " your %s setting" % (class_module, class_name, setting_name)
#             )
#         else:
#             txt = 'Backend module "%s" does not define a "%s" class.' % (
#                 class_module,
#                 class_name,
#             )
#         raise exceptions.ImproperlyConfigured(txt)
#     return clazz


from mailjet_rest import Client
from django.core.exceptions import ImproperlyConfigured
from django.template.loader import render_to_string
from ..settings import (
    MAILJET_API_KEY,
    DEFAULT_FROM_EMAIL,
    DEFAULT_FROM_NAME,
    MAILJET_API_SECRET_KEY,
    PRE_PROCESSORS,
)
from .campaign import CampaignBackend
from ..utils import load_class
from __future__ import absolute_import, unicode_literals





class MailjetRESTBackend(CampaignBackend):
    def __init__(self):
        if not MAILJET_API_KEY:
            raise ImproperlyConfigured(
                "Please specify your MAILJET API key in Django settings"
            )

        if not MAILJET_API_SECRET_KEY:
            raise ImproperlyConfigured(
                "Please specify your MAILJET API SECRET key in Django settings"
            )

        self.client = Client(auth=(MAILJET_API_KEY, MAILJET_API_SECRET_KEY))

    def _send_campaign(self, newsletter, list_id, segment_id=None):
        subject = newsletter.name

        options = {
            "Subject": subject,
            "ContactsListID": list_id,
            "Locale": "en",
            "SenderEmail": DEFAULT_FROM_EMAIL,
            "Sender": DEFAULT_FROM_NAME,

# Path: courriers/settings.py
# MAILJET_API_KEY = getattr(settings, "COURRIERS_MAILJET_API_KEY", "")
# 
# DEFAULT_FROM_EMAIL = getattr(
#     settings, "COURRIERS_DEFAULT_FROM_EMAIL", settings.DEFAULT_FROM_EMAIL
# )
# 
# DEFAULT_FROM_NAME = getattr(settings, "COURRIERS_DEFAULT_FROM_NAME", "")
# 
# MAILJET_API_SECRET_KEY = getattr(settings, "COURRIERS_MAILJET_API_SECRET_KEY", "")
# 
# PRE_PROCESSORS = getattr(settings, "COURRIERS_PRE_PROCESSORS", ())
# 
# Path: courriers/backends/campaign.py
# 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):
#         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 = newsletter.newsletter_segment.lang or settings.LANGUAGE_CODE
# 
#         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)
# 
#     def subscribe(self, email, list_id):
#         pass
# 
#     def unsubscribe(self, email, list_id):
#         pass
# 
# Path: courriers/utils.py
# def load_class(class_path, setting_name=None):
#     """
#     Loads a class given a class_path. The setting value may be a string or a
#     tuple.
# 
#     The setting_name parameter is only there for pretty error output, and
#     therefore is optional
#     """
#     if not isinstance(class_path, str):
#         try:
#             class_path, app_label = class_path
#         except Exception:
#             if setting_name:
#                 raise exceptions.ImproperlyConfigured(
#                     CLASS_PATH_ERROR % (setting_name, setting_name)
#                 )
#             else:
#                 raise exceptions.ImproperlyConfigured(
#                     CLASS_PATH_ERROR % ("this setting", "It")
#                 )
# 
#     try:
#         class_module, class_name = class_path.rsplit(".", 1)
#     except ValueError:
#         if setting_name:
#             txt = "%s isn't a valid module. Check your %s setting" % (
#                 class_path,
#                 setting_name,
#             )
#         else:
#             txt = "%s isn't a valid module." % class_path
#         raise exceptions.ImproperlyConfigured(txt)
# 
#     try:
#         mod = import_module(class_module)
#     except ImportError as e:
#         if setting_name:
#             txt = 'Error importing backend %s: "%s". Check your %s setting' % (
#                 class_module,
#                 e,
#                 setting_name,
#             )
#         else:
#             txt = 'Error importing backend %s: "%s".' % (class_module, e)
# 
#         raise exceptions.ImproperlyConfigured(txt)
# 
#     try:
#         clazz = getattr(mod, class_name)
#     except AttributeError:
#         if setting_name:
#             txt = (
#                 'Backend module "%s" does not define a "%s" class. Check'
#                 " your %s setting" % (class_module, class_name, setting_name)
#             )
#         else:
#             txt = 'Backend module "%s" does not define a "%s" class.' % (
#                 class_module,
#                 class_name,
#             )
#         raise exceptions.ImproperlyConfigured(txt)
#     return clazz


# Path: courriers/backends/mailjet_rest.py
from mailjet_rest import Client
from django.core.exceptions import ImproperlyConfigured
from django.template.loader import render_to_string
from ..settings import (
    MAILJET_API_KEY,
    DEFAULT_FROM_EMAIL,
    DEFAULT_FROM_NAME,
    MAILJET_API_SECRET_KEY,
    PRE_PROCESSORS,
)
from .campaign import CampaignBackend
from ..utils import load_class

from __future__ import absolute_import, unicode_literals





class MailjetRESTBackend(CampaignBackend):
    def __init__(self):
        if not MAILJET_API_KEY:
            raise ImproperlyConfigured(
                "Please specify your MAILJET API key in Django settings"
            )

        if not MAILJET_API_SECRET_KEY:
            raise ImproperlyConfigured(
                "Please specify your MAILJET API SECRET key in Django settings"
            )

        self.client = Client(auth=(MAILJET_API_KEY, MAILJET_API_SECRET_KEY))

    def _send_campaign(self, newsletter, list_id, segment_id=None):
        subject = newsletter.name

        options = {
            "Subject": subject,
            "ContactsListID": list_id,
            "Locale": "en",
            "SenderEmail": DEFAULT_FROM_EMAIL,
            "Sender": DEFAULT_FROM_NAME,

            "SenderName": DEFAULT_FROM_NAME,