from base64 import b64encode

from django.conf import settings

from mail_factory.messages import EmailMultiRelated


class PreviewMessage(EmailMultiRelated):
    def has_body_html(self):
        """Test if a message contains an alternative rendering in text/html"""
        return 'text/html' in self.alternatives

    @property
    def body_html(self):
        """Return an alternative rendering in text/html"""
        return self.alternatives.get('text/html', '')

    @property
    def body_html_escaped(self):
        """Return an alternative rendering in text/html escaped to work in a iframe"""
        return b64encode(self.body_html)


class BasePreviewMail(object):
    """Abstract class that helps creating preview emails.

    You also may overwrite:
     * get_context_data: to add global context such as SITE_NAME
    """
    message_class = PreviewMessage

    def get_message(self, lang=None):
        """Return a new message instance based on your MailClass"""
        return self.mail.create_email_msg(self.get_email_receivers(),
                                          lang=lang,
                                          message_class=self.message_class)

    @property
    def mail(self):
        return self.mail_class(self.get_context_data())

    def get_email_receivers(self):
        """Returns email receivers."""
        return [settings.SERVER_EMAIL, ]

    def get_context_data():
        """Returns automatic context_data."""
        return {}

    @property
    def mail_class(self):
        raise NotImplementedError

from django.conf import settings
from django.utils.encoding import smart_str

from mail_factory.messages import EmailMultiRelated


class PreviewMessage(EmailMultiRelated):
    def has_body_html(self):
        """Test if a message contains an alternative rendering in text/html"""
        return 'text/html' in self.rendering_formats

    @property
    def body_html(self):
        """Return an alternative rendering in text/html"""
        return self.rendering_formats.get('text/html', '')

    @property
    def rendering_formats(self):
        return dict((v, k) for k, v in self.alternatives)


class BasePreviewMail(object):
    """Abstract class that helps creating preview emails.

    You also may overwrite:
     * get_context_data: to add global context such as SITE_NAME
    """
    message_class = PreviewMessage

    def get_message(self, lang=None):
        """Return a new message instance based on your MailClass"""
        return self.mail.create_email_msg(self.get_email_receivers(),
                                          lang=lang,
                                          message_class=self.message_class)

    @property
    def mail(self):
        return self.mail_class(self.get_context_data())

    def get_email_receivers(self):
        """Returns email receivers."""
        return [settings.SERVER_EMAIL, ]

    def get_context_data():
        """Returns automatic context_data."""
        return {}

    @property
    def mail_class(self):
        raise NotImplementedError

Remove `body_html_escaped`. Add rendering filtered by formats.
Remove `body_html_escaped`.
Add rendering filtered by formats.

- from base64 import b64encode
- 
  from django.conf import settings
+ from django.utils.encoding import smart_str
  
  from mail_factory.messages import EmailMultiRelated
  
  
  class PreviewMessage(EmailMultiRelated):
      def has_body_html(self):
          """Test if a message contains an alternative rendering in text/html"""
-         return 'text/html' in self.alternatives
+         return 'text/html' in self.rendering_formats
  
      @property
      def body_html(self):
          """Return an alternative rendering in text/html"""
-         return self.alternatives.get('text/html', '')
+         return self.rendering_formats.get('text/html', '')
  
      @property
+     def rendering_formats(self):
+         return dict((v, k) for k, v in self.alternatives)
-     def body_html_escaped(self):
-         """Return an alternative rendering in text/html escaped to work in a iframe"""
-         return b64encode(self.body_html)
  
  
  class BasePreviewMail(object):
      """Abstract class that helps creating preview emails.
  
      You also may overwrite:
       * get_context_data: to add global context such as SITE_NAME
      """
      message_class = PreviewMessage
  
      def get_message(self, lang=None):
          """Return a new message instance based on your MailClass"""
          return self.mail.create_email_msg(self.get_email_receivers(),
                                            lang=lang,
                                            message_class=self.message_class)
  
      @property
      def mail(self):
          return self.mail_class(self.get_context_data())
  
      def get_email_receivers(self):
          """Returns email receivers."""
          return [settings.SERVER_EMAIL, ]
  
      def get_context_data():
          """Returns automatic context_data."""
          return {}
  
      @property
      def mail_class(self):
          raise NotImplementedError
  
Remove `body_html_escaped`. Add rendering filtered by formats.
## Code Before:
from base64 import b64encode

from django.conf import settings

from mail_factory.messages import EmailMultiRelated


class PreviewMessage(EmailMultiRelated):
    def has_body_html(self):
        """Test if a message contains an alternative rendering in text/html"""
        return 'text/html' in self.alternatives

    @property
    def body_html(self):
        """Return an alternative rendering in text/html"""
        return self.alternatives.get('text/html', '')

    @property
    def body_html_escaped(self):
        """Return an alternative rendering in text/html escaped to work in a iframe"""
        return b64encode(self.body_html)


class BasePreviewMail(object):
    """Abstract class that helps creating preview emails.

    You also may overwrite:
     * get_context_data: to add global context such as SITE_NAME
    """
    message_class = PreviewMessage

    def get_message(self, lang=None):
        """Return a new message instance based on your MailClass"""
        return self.mail.create_email_msg(self.get_email_receivers(),
                                          lang=lang,
                                          message_class=self.message_class)

    @property
    def mail(self):
        return self.mail_class(self.get_context_data())

    def get_email_receivers(self):
        """Returns email receivers."""
        return [settings.SERVER_EMAIL, ]

    def get_context_data():
        """Returns automatic context_data."""
        return {}

    @property
    def mail_class(self):
        raise NotImplementedError

## Instruction:
Remove `body_html_escaped`. Add rendering filtered by formats.
## Code After:
from django.conf import settings
from django.utils.encoding import smart_str

from mail_factory.messages import EmailMultiRelated


class PreviewMessage(EmailMultiRelated):
    def has_body_html(self):
        """Test if a message contains an alternative rendering in text/html"""
        return 'text/html' in self.rendering_formats

    @property
    def body_html(self):
        """Return an alternative rendering in text/html"""
        return self.rendering_formats.get('text/html', '')

    @property
    def rendering_formats(self):
        return dict((v, k) for k, v in self.alternatives)


class BasePreviewMail(object):
    """Abstract class that helps creating preview emails.

    You also may overwrite:
     * get_context_data: to add global context such as SITE_NAME
    """
    message_class = PreviewMessage

    def get_message(self, lang=None):
        """Return a new message instance based on your MailClass"""
        return self.mail.create_email_msg(self.get_email_receivers(),
                                          lang=lang,
                                          message_class=self.message_class)

    @property
    def mail(self):
        return self.mail_class(self.get_context_data())

    def get_email_receivers(self):
        """Returns email receivers."""
        return [settings.SERVER_EMAIL, ]

    def get_context_data():
        """Returns automatic context_data."""
        return {}

    @property
    def mail_class(self):
        raise NotImplementedError
