#!/usr/bin/env python
# -*- coding: utf-8 -*-
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
            )

@@ -39,16 +39,26 @@
 f-8 -*-%0A
+import io%0A
 import o
@@ -1022,16 +1022,19 @@
    with 
+io.
 open(arc
@@ -1053,16 +1053,33 @@
 ath, 'w'
+, encoding='utf8'
 ) as f:%0A
@@ -1089,16 +1089,23 @@
         
+ data =
  json.du
@@ -1106,16 +1106,17 @@
 son.dump
+s
 (%0A      
@@ -1215,56 +1215,108 @@
     
-f,%0A                default=serializer%0A            
+default=serializer,%0A                ensure_ascii=False%0A            )%0A            f.write(unicode(data)
 )%0A

Write out page cache as unicode utf8