import six
from django.contrib import messages
from six.moves import map


class FlashMessages(object):
    """
    Intermediate container for flash messages.

    This is useful as, at the time of creating the message, we don't know
    whether the response is an AJAX response or not.
    """

    def __init__(self):
        self.msgs = {}

    def add_message(self, level, message):
        self.msgs.setdefault(level, []).append(message)

    def add_messages(self, level, messages):
        for msg in messages:
            self.add_message(level, msg)

    def info(self, message):
        self.add_message(messages.INFO, message)

    def warning(self, message):
        self.add_message(messages.WARNING, message)

    def error(self, message):
        self.add_message(messages.ERROR, message)

    def success(self, message):
        self.add_message(messages.SUCCESS, message)

    def to_json(self):
        payload = {}
        for level, msgs in self.msgs.items():
            tag = messages.DEFAULT_TAGS.get(level, 'info')
            payload[tag] = map(six.text_type, msgs)
        return payload

    def apply_to_request(self, request):
        for level, msgs in self.msgs.items():
            for msg in msgs:
                messages.add_message(request, level, msg)

import six
from django.contrib import messages
from six.moves import map


class FlashMessages(object):
    """
    Intermediate container for flash messages.

    This is useful as, at the time of creating the message, we don't know
    whether the response is an AJAX response or not.
    """

    def __init__(self):
        self.msgs = {}

    def add_message(self, level, message):
        self.msgs.setdefault(level, []).append(message)

    def add_messages(self, level, messages):
        for msg in messages:
            self.add_message(level, msg)

    def info(self, message):
        self.add_message(messages.INFO, message)

    def warning(self, message):
        self.add_message(messages.WARNING, message)

    def error(self, message):
        self.add_message(messages.ERROR, message)

    def success(self, message):
        self.add_message(messages.SUCCESS, message)

    def to_json(self):
        payload = {}
        for level, msgs in self.msgs.items():
            tag = messages.DEFAULT_TAGS.get(level, 'info')
            payload[tag] = [six.text_type(msg) for msg in msgs]
        return payload

    def apply_to_request(self, request):
        for level, msgs in self.msgs.items():
            for msg in msgs:
                messages.add_message(request, level, msg)

Fix JSON serialisation problem with AJAX basket
Fix JSON serialisation problem with AJAX basket

six.moves.map returns itertools.imap which won't serialize to JSON.
This commit unpacks the list into a normal list of strings to circumvent
the issue.

  import six
  from django.contrib import messages
  from six.moves import map
  
  
  class FlashMessages(object):
      """
      Intermediate container for flash messages.
  
      This is useful as, at the time of creating the message, we don't know
      whether the response is an AJAX response or not.
      """
  
      def __init__(self):
          self.msgs = {}
  
      def add_message(self, level, message):
          self.msgs.setdefault(level, []).append(message)
  
      def add_messages(self, level, messages):
          for msg in messages:
              self.add_message(level, msg)
  
      def info(self, message):
          self.add_message(messages.INFO, message)
  
      def warning(self, message):
          self.add_message(messages.WARNING, message)
  
      def error(self, message):
          self.add_message(messages.ERROR, message)
  
      def success(self, message):
          self.add_message(messages.SUCCESS, message)
  
      def to_json(self):
          payload = {}
          for level, msgs in self.msgs.items():
              tag = messages.DEFAULT_TAGS.get(level, 'info')
-             payload[tag] = map(six.text_type, msgs)
+             payload[tag] = [six.text_type(msg) for msg in msgs]
          return payload
  
      def apply_to_request(self, request):
          for level, msgs in self.msgs.items():
              for msg in msgs:
                  messages.add_message(request, level, msg)
  
Fix JSON serialisation problem with AJAX basket
## Code Before:
import six
from django.contrib import messages
from six.moves import map


class FlashMessages(object):
    """
    Intermediate container for flash messages.

    This is useful as, at the time of creating the message, we don't know
    whether the response is an AJAX response or not.
    """

    def __init__(self):
        self.msgs = {}

    def add_message(self, level, message):
        self.msgs.setdefault(level, []).append(message)

    def add_messages(self, level, messages):
        for msg in messages:
            self.add_message(level, msg)

    def info(self, message):
        self.add_message(messages.INFO, message)

    def warning(self, message):
        self.add_message(messages.WARNING, message)

    def error(self, message):
        self.add_message(messages.ERROR, message)

    def success(self, message):
        self.add_message(messages.SUCCESS, message)

    def to_json(self):
        payload = {}
        for level, msgs in self.msgs.items():
            tag = messages.DEFAULT_TAGS.get(level, 'info')
            payload[tag] = map(six.text_type, msgs)
        return payload

    def apply_to_request(self, request):
        for level, msgs in self.msgs.items():
            for msg in msgs:
                messages.add_message(request, level, msg)

## Instruction:
Fix JSON serialisation problem with AJAX basket
## Code After:
import six
from django.contrib import messages
from six.moves import map


class FlashMessages(object):
    """
    Intermediate container for flash messages.

    This is useful as, at the time of creating the message, we don't know
    whether the response is an AJAX response or not.
    """

    def __init__(self):
        self.msgs = {}

    def add_message(self, level, message):
        self.msgs.setdefault(level, []).append(message)

    def add_messages(self, level, messages):
        for msg in messages:
            self.add_message(level, msg)

    def info(self, message):
        self.add_message(messages.INFO, message)

    def warning(self, message):
        self.add_message(messages.WARNING, message)

    def error(self, message):
        self.add_message(messages.ERROR, message)

    def success(self, message):
        self.add_message(messages.SUCCESS, message)

    def to_json(self):
        payload = {}
        for level, msgs in self.msgs.items():
            tag = messages.DEFAULT_TAGS.get(level, 'info')
            payload[tag] = [six.text_type(msg) for msg in msgs]
        return payload

    def apply_to_request(self, request):
        for level, msgs in self.msgs.items():
            for msg in msgs:
                messages.add_message(request, level, msg)
