event-timeline/app/event/views.py

77 lines
2.3 KiB
Python

import json
from django.shortcuts import render
from django.conf import settings
from .models import Event
from ..page.models import Page
def events(request, slug=''):
context = {}
context['settings'] = settings
context['events'] = Event.objects.all().order_by('position')
context['about'], c = Page.objects.get_or_create(slug='about')
context['imprint'], c = Page.objects.get_or_create(slug='imprint')
return render(request, 'events.html', context)
def timeline(request):
context = {}
context['settings'] = settings
context['title'], c = Page.objects.get_or_create(slug='title')
context['about'], c = Page.objects.get_or_create(slug='about')
context['imprint'], c = Page.objects.get_or_create(slug='imprint')
timeline = {'events': []}
for event in Event.objects.all().order_by('position'):
if not event.date:
continue
date = event.date.split(' ')
start = date[0].split('-')
if len(date) > 1:
end = date[1].split('-')
else:
end = start
while len(end) < 3:
end.append(1)
while len(start) < 3:
start.append(1)
timeline['events'].append({
"start_date": {
"year": start[0],
"month": start[1],
"day": start[2],
"hour": "",
"minute": "",
"second": "",
"millisecond": "",
"format": ""
},
"end_date": {
"year": end[0],
"month": end[1],
"day": end[2],
"hour": "",
"minute": "",
"second": "",
"millisecond": "",
"format": ""
},
"display_date": event.display_date if timeline['events'] else '&nbsp;',
"text": {
"headline": event.title,
"text": event.body
},
"group": event.type.lower().strip(),
"unique_id": event.slug,
})
if event.media:
timeline['events'][-1]['media'] = {
"url": event.media,
"caption": event.media_caption,
}
context['timeline_json'] = json.dumps(timeline)
return render(request, 'timeline.html', context)