Skip to content
This repository has been archived by the owner on Apr 18, 2020. It is now read-only.

Commit

Permalink
Started on a JSON serializer rather than my custom JSON hackery.
Browse files Browse the repository at this point in the history
  • Loading branch information
palewire committed Jun 20, 2017
1 parent e3c51e9 commit d4c1d88
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 33 deletions.
26 changes: 7 additions & 19 deletions bigbuild/management/commands/cachepages.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,29 +3,18 @@
import io
import os
import six
import json
from datetime import datetime
import bigbuild
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")
from bigbuild.serializers import BigBuildJSONSerializer


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')
archive_cache_path = os.path.join(bigbuild.get_archive_directory(), '.cache')

# Delete it if it already exists
if os.path.exists(archive_cache_path):
Expand All @@ -34,11 +23,10 @@ def handle(self, *args, **options):
# Pull the live PageList from the YAML files
page_list = PageList()

# Create a JSON serializer
serializer = BigBuildJSONSerializer()

# 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
)
data = serializer.serialize(page_list)
f.write(six.text_type(data))
3 changes: 0 additions & 3 deletions bigbuild/models/archivedpages.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,6 @@ class ArchivedPage(BasePage):
"""
An archived custom page.
"""
class Meta:
abstract = True

def __str__(self):
return self.slug

Expand Down
13 changes: 5 additions & 8 deletions bigbuild/models/pagelists.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
# -*- coding: utf-8 -*-
import os
import six
import json
import logging
from django.conf import settings
from collections import Sequence
Expand All @@ -11,7 +10,7 @@
MissingRecommendedMetadataWarning
)
from bigbuild.models import Page, ArchivedPage
from dateutil.parser import parse as dateparse
from bigbuild.serializers import BigBuildJSONDeserializer
from bigbuild import get_page_directory, get_archive_directory
logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -133,12 +132,10 @@ def get_archived_pages(self):
logger.debug("Loading cached archived page list")
with open(self.archived_cache_path, 'r') as f:
page_list = []
dict_list = json.load(f)['archived_pages']
for d in dict_list:
d['pub_date'] = dateparse(d['pub_date'])
page = ArchivedPage(**d)
if page.should_build():
page_list.append(page)
dobj_list = BigBuildJSONDeserializer(f.read())
for dobj in dobj_list:
if dobj.object.should_build():
page_list.append(dobj.object)
# Otherwise get them from the YAML
else:
logger.debug("Retrieving YAML archived page list")
Expand Down
3 changes: 0 additions & 3 deletions bigbuild/models/pages.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,6 @@ class Page(BasePage):
"""
A custom page published via static.latimes.com
"""
class Meta:
abstract = True

def __str__(self):
return self.slug

Expand Down
32 changes: 32 additions & 0 deletions bigbuild/serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import six
import sys
import json
from django.core.serializers.json import Serializer
from django.core.serializers.base import DeserializationError
from django.core.serializers.python import Deserializer as PythonDeserializer


class BigBuildJSONSerializer(Serializer):
"""
A custom JSON serializer for bigbuild models.
"""
pass


def BigBuildJSONDeserializer(stream_or_string, **options):
"""
Deserialize a stream or string of JSON data.
"""
if not isinstance(stream_or_string, (bytes, six.string_types)):
stream_or_string = stream_or_string.read()
if isinstance(stream_or_string, bytes):
stream_or_string = stream_or_string.decode('utf-8')
try:
objects = json.loads(stream_or_string)
for obj in PythonDeserializer(objects, **options):
yield obj
except GeneratorExit:
raise
except Exception as e:
# Map to deserializer error
six.reraise(DeserializationError, DeserializationError(e), sys.exc_info()[2])

0 comments on commit d4c1d88

Please sign in to comment.