Created
June 4, 2012 17:46
-
-
Save cwmanning/2869790 to your computer and use it in GitHub Desktop.
story view messing with asset parsing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from django.conf import settings | |
from django.http import HttpResponse | |
from django.shortcuts import render | |
from dummyjson import views as dj | |
from ordereddict import OrderedDict | |
import json | |
def story(request, content_id, slug=None, template=None): | |
path = '/static/content/modules/stories/story_' + content_id + '.json' | |
if not template: | |
template = 'story.html' | |
extra_context = { | |
'base_page_type' : 'story', | |
'nav_state' : 'collapsed', | |
} | |
#if request.is_ajax(): | |
# template = 'story.html' | |
# first two conditions are temporary - DEV ONLY testing | |
if len(content_id) == 1: | |
return dj.load_json(request, path, 'story-old.html', extra_context, slug) | |
elif len(content_id) == 2: | |
file = open(settings.PROJECT_ROOT + path, 'r') | |
content = json.load(file, object_pairs_hook=OrderedDict) | |
if 'Body' in content: | |
parse_story(content) | |
extra_context['article'] = content | |
return render(request, template, extra_context) | |
#else: | |
# get and parse json from api | |
def parse_story(content): | |
# create a hash index of asset_id to asset_data | |
assets_dict = dict(for (a['id'], a) in content['Assets'] if 'id' in a) | |
for body_item in content['Body']: | |
if 'type' in body_item: | |
if body_item.get('type') == 'asset': | |
asset_id = body_item.get('value') | |
if asset_id in asset_dict: | |
body_item['asset_data'] = asset_dict[asset_id] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment