Skip to content

Instantly share code, notes, and snippets.

@ericmoritz
Forked from cwmanning/stories-views.py
Created June 4, 2012 21:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ericmoritz/2870884 to your computer and use it in GitHub Desktop.
Save ericmoritz/2870884 to your computer and use it in GitHub Desktop.
story view messing with asset parsing
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']:
asset_id = body_item.get('value')
if body_item.get('type') == 'asset' and 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