generate xml with xml.etree.ElementTree
This commit is contained in:
parent
9d97c0d0c3
commit
3aed5fd3ce
160
cfp/planning.py
160
cfp/planning.py
|
@ -10,6 +10,7 @@ from copy import deepcopy
|
||||||
from collections import OrderedDict, namedtuple
|
from collections import OrderedDict, namedtuple
|
||||||
from itertools import islice
|
from itertools import islice
|
||||||
from zlib import adler32
|
from zlib import adler32
|
||||||
|
import xml.etree.ElementTree as ET
|
||||||
|
|
||||||
from .models import Conference, Talk, Room
|
from .models import Conference, Talk, Room
|
||||||
|
|
||||||
|
@ -172,113 +173,80 @@ class Program:
|
||||||
def _as_xml(self):
|
def _as_xml(self):
|
||||||
if not self.initialized:
|
if not self.initialized:
|
||||||
self._lazy_init()
|
self._lazy_init()
|
||||||
result = """<?xml version="1.0" encoding="UTF-8"?>
|
schedule = ET.Element('schedule')
|
||||||
<schedule>
|
|
||||||
%(conference)s
|
|
||||||
%(days)s
|
|
||||||
</schedule>
|
|
||||||
"""
|
|
||||||
|
|
||||||
if not len(self.days):
|
#if not len(self.days):
|
||||||
return result % {'conference': '', 'days': ''}
|
# return result % {'conference': '', 'days': ''}
|
||||||
|
|
||||||
conference_xml = """<conference>
|
conference = ET.SubElement(schedule, 'conference')
|
||||||
<title>%(title)s</title>
|
elt = ET.SubElement(conference, 'title')
|
||||||
<subtitle></subtitle>
|
elt.text = self.site.name
|
||||||
<venue>%(venue)s</venue>
|
elt = ET.SubElement(conference, 'venue')
|
||||||
<city>%(city)s</city>
|
elt.text = ', '.join(map(lambda x: x.strip(), self.conference.venue.split('\n')))
|
||||||
<start>%(start_date)s</start>
|
elt = ET.SubElement(conference, 'city')
|
||||||
<end>%(end_date)s</end>
|
elt.text = self.conference.city
|
||||||
<days>%(days_count)s</days>
|
elt = ET.SubElement(conference, 'start_date')
|
||||||
<day_change>09:00:00</day_change>
|
elt.text = sorted(self.days.keys())[0].strftime('%Y-%m-%d')
|
||||||
<timeslot_duration>00:05:00</timeslot_duration>
|
elt = ET.SubElement(conference, 'end_date')
|
||||||
</conference>
|
elt.text = sorted(self.days.keys(), reverse=True)[0].strftime('%Y-%m-%d')
|
||||||
""" % {
|
elt = ET.SubElement(conference, 'days_count')
|
||||||
'title': self.site.name,
|
elt.text = str(len(self.days))
|
||||||
'venue': ', '.join(map(lambda x: x.strip(), self.conference.venue.split('\n'))),
|
|
||||||
'city': self.conference.city,
|
|
||||||
'start_date': sorted(self.days.keys())[0].strftime('%Y-%m-%d'),
|
|
||||||
'end_date': sorted(self.days.keys(), reverse=True)[0].strftime('%Y-%m-%d'),
|
|
||||||
'days_count': len(self.days),
|
|
||||||
}
|
|
||||||
|
|
||||||
days_xml = ''
|
|
||||||
for index, day in enumerate(sorted(self.days.keys())):
|
for index, day in enumerate(sorted(self.days.keys())):
|
||||||
days_xml += '<day index="%(index)s" date="%(date)s">\n' % {
|
day_elt = ET.SubElement(schedule, 'day', index=str(index+1), date=day.strftime('%Y-%m-%d'))
|
||||||
'index': index + 1,
|
|
||||||
'date': day.strftime('%Y-%m-%d'),
|
|
||||||
}
|
|
||||||
for room in self.rooms.all():
|
for room in self.rooms.all():
|
||||||
days_xml += ' <room name="%s">\n' % room.name
|
room_elt = ET.SubElement(day_elt, 'room', name=room.name)
|
||||||
for talk in self.talks.filter(room=room).order_by('start_date'):
|
for talk in self.talks.filter(room=room).order_by('start_date'):
|
||||||
if localtime(talk.start_date).date() != day:
|
if localtime(talk.start_date).date() != day:
|
||||||
continue
|
continue
|
||||||
|
talk_elt = ET.SubElement(day_elt, 'event', id=str(talk.id))
|
||||||
duration = talk.estimated_duration
|
duration = talk.estimated_duration
|
||||||
persons = ''
|
persons_elt = ET.SubElement(talk_elt, 'persons')
|
||||||
for speaker in talk.speakers.all():
|
for speaker in talk.speakers.all():
|
||||||
persons += ' <person id="%(person_id)s">%(person)s</person>\n' % {
|
person_elt = ET.SubElement(talk_elt, 'person', id=str(speaker.id))
|
||||||
'person_id': speaker.id,
|
person_elt.text = str(speaker)
|
||||||
'person': str(speaker),
|
# #if talk.registration_required and self.conference.subscriptions_open:
|
||||||
}
|
# # links += mark_safe("""
|
||||||
links = ''
|
# # <link tag="registration">%(link)s</link>""" % {
|
||||||
registration = ''
|
# # 'link': reverse('register-for-a-talk', args=[talk.slug]),
|
||||||
#if talk.registration_required and self.conference.subscriptions_open:
|
# # })
|
||||||
# links += mark_safe("""
|
# # registration = """
|
||||||
# <link tag="registration">%(link)s</link>""" % {
|
# # <attendees_max>%(max)s</attendees_max>
|
||||||
# 'link': reverse('register-for-a-talk', args=[talk.slug]),
|
# # <attendees_remain>%(remain)s</attendees_remain>""" % {
|
||||||
# })
|
# # 'max': talk.attendees_limit,
|
||||||
# registration = """
|
# # 'remain': talk.remaining_attendees or 0,
|
||||||
# <attendees_max>%(max)s</attendees_max>
|
# # }
|
||||||
# <attendees_remain>%(remain)s</attendees_remain>""" % {
|
# #if talk.materials:
|
||||||
# 'max': talk.attendees_limit,
|
# # links += mark_safe("""
|
||||||
# 'remain': talk.remaining_attendees or 0,
|
# # <link tag="slides">%(link)s</link>""" % {
|
||||||
# }
|
# # 'link': talk.materials.url,
|
||||||
#if talk.materials:
|
# # })
|
||||||
# links += mark_safe("""
|
# #if talk.video:
|
||||||
# <link tag="slides">%(link)s</link>""" % {
|
# # links += mark_safe("""
|
||||||
# 'link': talk.materials.url,
|
# # <link tag="video">%(link)s</link>""" % {
|
||||||
# })
|
# # 'link': talk.video,
|
||||||
#if talk.video:
|
# # })
|
||||||
# links += mark_safe("""
|
elt = ET.SubElement(talk_elt, 'start')
|
||||||
# <link tag="video">%(link)s</link>""" % {
|
elt.text = localtime(talk.start_date).strftime('%H:%M')
|
||||||
# 'link': talk.video,
|
elt = ET.SubElement(talk_elt, 'duration')
|
||||||
# })
|
elt.text = '%02d:%02d' % (talk.estimated_duration / 60, talk.estimated_duration % 60)
|
||||||
days_xml += """ <event id="%(id)s">
|
elt = ET.SubElement(talk_elt, 'room')
|
||||||
<start>%(start)s</start>
|
elt.text = room.name
|
||||||
<duration>%(duration)s</duration>
|
elt = ET.SubElement(talk_elt, 'slug')
|
||||||
<room>%(room)s</room>
|
elt.text = talk.slug
|
||||||
<slug>%(slug)s</slug>
|
elt = ET.SubElement(talk_elt, 'title')
|
||||||
<title>%(title)s</title>
|
elt.text = talk.title
|
||||||
<subtitle/>
|
elt = ET.SubElement(talk_elt, 'subtitle')
|
||||||
<track>%(track)s</track>
|
elt = ET.SubElement(talk_elt, 'track')
|
||||||
<type>%(type)s</type>
|
elt.text = talk.track or ''
|
||||||
<language/>
|
elt = ET.SubElement(talk_elt, 'type')
|
||||||
<description>%(description)s</description>
|
elt.text = talk.category.label
|
||||||
<persons>
|
elt = ET.SubElement(talk_elt, 'language')
|
||||||
%(persons)s </persons>
|
elt = ET.SubElement(talk_elt, 'description')
|
||||||
<links>%(links)s
|
elt.text = talk.description
|
||||||
</links>%(registration)s
|
elt = ET.SubElement(talk_elt, 'links')
|
||||||
</event>\n""" % {
|
|
||||||
'id': talk.id,
|
|
||||||
'start': localtime(talk.start_date).strftime('%H:%M'),
|
|
||||||
'duration': '%02d:%02d' % (talk.estimated_duration / 60, talk.estimated_duration % 60),
|
|
||||||
'room': escape(room.name),
|
|
||||||
'slug': escape(talk.slug),
|
|
||||||
'title': escape(talk.title),
|
|
||||||
'track': escape(talk.track or ''),
|
|
||||||
'type': escape(talk.category.label),
|
|
||||||
'description': escape(talk.description),
|
|
||||||
'persons': persons,
|
|
||||||
'links': links,
|
|
||||||
'registration': registration,
|
|
||||||
}
|
|
||||||
days_xml += ' </room>\n'
|
|
||||||
days_xml += '</day>\n'
|
|
||||||
|
|
||||||
return result % {
|
return ET.tostring(schedule)
|
||||||
'conference': '\n'.join(map(lambda x: ' ' + x, conference_xml.split('\n'))),
|
|
||||||
'days': '\n'.join(map(lambda x: ' ' + x, days_xml.split('\n'))),
|
|
||||||
}
|
|
||||||
|
|
||||||
def _as_ics(self):
|
def _as_ics(self):
|
||||||
if not self.initialized:
|
if not self.initialized:
|
||||||
|
|
Loading…
Reference in New Issue