import os
import json
from datetime import datetime
from bigbuild.models import PageList
from bigbuild import get_archive_directory
from django.core.management.base import BaseCommand


def serializer(obj):
    """
    JSON serializer for objects not serializable by default json code
    """
    if isinstance(obj, datetime):
        serial = obj.isoformat()
        return serial
    raise TypeError("Type not serializable")


class Command(BaseCommand):
    help = "Cache page metadata to increase the application speed"

    def handle(self, *args, **options):
        # Set the cache path for archived pages
        archive_cache_path = os.path.join(get_archive_directory(), '.cache')

        # Delete it if it already exists
        if os.path.exists(archive_cache_path):
            os.remove(archive_cache_path)

        # Pull the live PageList from the YAML files
        page_list = PageList()

        # Save the archived pages out to a new cache
        with open(archive_cache_path, 'w') as f:
            json.dump(
                dict(archived_pages=[p.to_json() for p in page_list.archived_pages]),
                f,
                default=serializer
            )

import io
import os
import json
from datetime import datetime
from bigbuild.models import PageList
from bigbuild import get_archive_directory
from django.core.management.base import BaseCommand


def serializer(obj):
    """
    JSON serializer for objects not serializable by default json code
    """
    if isinstance(obj, datetime):
        serial = obj.isoformat()
        return serial
    raise TypeError("Type not serializable")


class Command(BaseCommand):
    help = "Cache page metadata to increase the application speed"

    def handle(self, *args, **options):
        # Set the cache path for archived pages
        archive_cache_path = os.path.join(get_archive_directory(), '.cache')

        # Delete it if it already exists
        if os.path.exists(archive_cache_path):
            os.remove(archive_cache_path)

        # Pull the live PageList from the YAML files
        page_list = PageList()

        # Save the archived pages out to a new cache
        with io.open(archive_cache_path, 'w', encoding='utf8') as f:
            data = json.dumps(
                dict(archived_pages=[p.to_json() for p in page_list.archived_pages]),
                default=serializer,
                ensure_ascii=False
            )
            f.write(unicode(data))

Write out page cache as unicode utf8
Write out page cache as unicode utf8

+ import io
  import os
  import json
  from datetime import datetime
  from bigbuild.models import PageList
  from bigbuild import get_archive_directory
  from django.core.management.base import BaseCommand
  
  
  def serializer(obj):
      """
      JSON serializer for objects not serializable by default json code
      """
      if isinstance(obj, datetime):
          serial = obj.isoformat()
          return serial
      raise TypeError("Type not serializable")
  
  
  class Command(BaseCommand):
      help = "Cache page metadata to increase the application speed"
  
      def handle(self, *args, **options):
          # Set the cache path for archived pages
          archive_cache_path = os.path.join(get_archive_directory(), '.cache')
  
          # Delete it if it already exists
          if os.path.exists(archive_cache_path):
              os.remove(archive_cache_path)
  
          # Pull the live PageList from the YAML files
          page_list = PageList()
  
          # Save the archived pages out to a new cache
-         with open(archive_cache_path, 'w') as f:
+         with io.open(archive_cache_path, 'w', encoding='utf8') as f:
-             json.dump(
+             data = json.dumps(
                  dict(archived_pages=[p.to_json() for p in page_list.archived_pages]),
-                 f,
-                 default=serializer
+                 default=serializer,
+                 ensure_ascii=False
              )
+             f.write(unicode(data))
  
Write out page cache as unicode utf8
## Code Before:
import os
import json
from datetime import datetime
from bigbuild.models import PageList
from bigbuild import get_archive_directory
from django.core.management.base import BaseCommand


def serializer(obj):
    """
    JSON serializer for objects not serializable by default json code
    """
    if isinstance(obj, datetime):
        serial = obj.isoformat()
        return serial
    raise TypeError("Type not serializable")


class Command(BaseCommand):
    help = "Cache page metadata to increase the application speed"

    def handle(self, *args, **options):
        # Set the cache path for archived pages
        archive_cache_path = os.path.join(get_archive_directory(), '.cache')

        # Delete it if it already exists
        if os.path.exists(archive_cache_path):
            os.remove(archive_cache_path)

        # Pull the live PageList from the YAML files
        page_list = PageList()

        # Save the archived pages out to a new cache
        with open(archive_cache_path, 'w') as f:
            json.dump(
                dict(archived_pages=[p.to_json() for p in page_list.archived_pages]),
                f,
                default=serializer
            )

## Instruction:
Write out page cache as unicode utf8
## Code After:
import io
import os
import json
from datetime import datetime
from bigbuild.models import PageList
from bigbuild import get_archive_directory
from django.core.management.base import BaseCommand


def serializer(obj):
    """
    JSON serializer for objects not serializable by default json code
    """
    if isinstance(obj, datetime):
        serial = obj.isoformat()
        return serial
    raise TypeError("Type not serializable")


class Command(BaseCommand):
    help = "Cache page metadata to increase the application speed"

    def handle(self, *args, **options):
        # Set the cache path for archived pages
        archive_cache_path = os.path.join(get_archive_directory(), '.cache')

        # Delete it if it already exists
        if os.path.exists(archive_cache_path):
            os.remove(archive_cache_path)

        # Pull the live PageList from the YAML files
        page_list = PageList()

        # Save the archived pages out to a new cache
        with io.open(archive_cache_path, 'w', encoding='utf8') as f:
            data = json.dumps(
                dict(archived_pages=[p.to_json() for p in page_list.archived_pages]),
                default=serializer,
                ensure_ascii=False
            )
            f.write(unicode(data))
