Created
July 24, 2012 16:28
-
-
Save cwmanning/3171020 to your computer and use it in GitHub Desktop.
Messing around with timeline dates
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 gannettapi.api import api | |
from datetime import datetime | |
from dateutil.parser import parse | |
from dateutil.relativedelta import relativedelta | |
from ordereddict import OrderedDict | |
from relux import utils | |
import json | |
import photos.views as photos | |
def story(request, content_id, slug=None, template=None, format='html'): | |
path = '/static/content/modules/stories/story_' + content_id + '.json' | |
if not template: | |
template = 'story.html' | |
context = { | |
'base_page_type' : 'story', | |
'mapbox_url' : settings.MAPBOX_URL | |
} | |
# len() conditions for DEV ONLY json testing | |
if settings.DEBUG and len(content_id) <= 2: | |
if len(content_id) == 1: | |
return dj.load_json(request, path, 'story-old.html', context, slug) | |
else: | |
file = open(settings.PROJECT_ROOT + path, 'r') | |
content = json.load(file, object_pairs_hook=OrderedDict) | |
else: | |
content = api.article.get_content(content_id) | |
if 'body' in content: | |
parse_story(content) | |
context['article'] = content | |
ssts = utils.get_nested(content, "ssts", default={}) | |
context[u'aws_data'] = \ | |
api.aws.get_inventory(domain=settings.AWS_DOMAIN, | |
type="article.htm", | |
**utils.only_keys(ssts, | |
"section", | |
"subsection", | |
"topic", | |
"subtopic")) | |
if format == 'json': | |
return HttpResponse(json.dumps(context), | |
content_type="application/json") | |
return render(request, template, context) | |
def parse_story(content): | |
# create a hash index of asset_id to asset_data | |
asset_dict = dict((a['id'], a) for a in content['assets'] if 'id' in a) | |
# check for galleries | |
for a_id, a in asset_dict.iteritems(): | |
if a['type'] == 'gallery': | |
gallery = photos.get_photos(str(a_id)) | |
a[u'gallery'] = gallery | |
# check for prioritized content | |
try: | |
priority_id = int(utils.get_nested(content, 'metadata', 'items', 'layoutpriority')) | |
content[u'priority_asset'] = asset_dict[priority_id] | |
except: | |
priority_id = None | |
for body_item in content['body']: | |
try: | |
asset_id = int(body_item.get('value')) | |
except ValueError: | |
asset_id = -1 | |
if body_item.get('type') == 'asset' and asset_id in asset_dict: | |
body_item[u'asset_data'] = asset_dict[asset_id] | |
# if prioritized, remove asset reference from article body | |
if asset_id == priority_id: | |
content['body'].remove(body_item) | |
# remove original assets data to prevent duplication | |
del content['assets'] | |
def timeline(request): | |
path = '/static/content/json/timeline.json' | |
file = open(settings.PROJECT_ROOT + path, 'r') | |
content = json.load(file, object_pairs_hook=OrderedDict) | |
events = utils.get_nested(content, 'data', 'events', default={}) | |
event_dates = [] | |
for e in events: | |
event_dates.append(parse(e['date'])) | |
start = min(event_dates) | |
end = max(event_dates) | |
time_scale = int(utils.get_nested(content, 'data', 'mainScale', default=0)) | |
# years | |
if time_scale == 0: | |
unit = 'years' | |
graph_start = datetime(start.year, 1, 1) | |
graph_end = datetime(end.year + 1, 1, 1) | |
# months | |
elif time_scale == 1: | |
unit = 'months' | |
graph_start = datetime(start.year, start.month, 1) | |
graph_end = datetime(end.year, end.month + 1, 1) | |
# days | |
elif time_scale == 2: | |
unit = 'days' | |
graph_start = datetime(start.year, start.month, start.day) | |
graph_end = datetime(end.year, end.month, end.day + 1) | |
# hours | |
elif time_scale == 3: | |
unit = 'hours' | |
graph_start = datetime(start.year, start.month, start.day, start.hours) | |
graph_end = datetime(end.year, end.month, end.day, end.hours + 1) | |
# minutes | |
elif time_scale == 4: | |
unit = 'minutes' | |
graph_start = datetime(start.year, start.month, start.day, start.hours, start.minutes) | |
graph_end = datetime(end.year, end.month, end.day, end.hours, end.minutes) | |
# number of years, months, etc. that need to be charted, labeled | |
unit_count = abs(getattr(relativedelta(graph_start, graph_end), unit)) | |
# get milliseconds difference between start and end | |
range_ms = (graph_end - graph_start).total_seconds() * 1000 | |
for index, e in enumerate(events): | |
event_ms = (event_dates[index] - graph_start).total_seconds() * 1000 | |
if range_ms != 0: | |
percent = event_ms / range_ms | |
else: | |
percent = 0 | |
e[u'boxPosition'] = percent | |
print e['boxPosition'] | |
content['data']['graphStart'] = graph_start | |
content['data']['graphEnd'] = graph_end | |
return render(request, 'stories/interactive_timeline.html', content) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment