2016-10-16 21:36:03 +00:00
|
|
|
|
from django.db.models import Q
|
2016-10-16 21:50:18 +00:00
|
|
|
|
from django.utils.safestring import mark_safe
|
|
|
|
|
from django.utils.html import escape
|
2016-10-16 21:36:03 +00:00
|
|
|
|
from django.utils.timezone import localtime
|
2016-10-31 22:42:07 +00:00
|
|
|
|
from django.core.cache import cache
|
2016-11-08 23:41:54 +00:00
|
|
|
|
from django.core.urlresolvers import reverse
|
2016-10-16 21:36:03 +00:00
|
|
|
|
|
|
|
|
|
from datetime import datetime, timedelta
|
|
|
|
|
from copy import deepcopy
|
|
|
|
|
from collections import OrderedDict, namedtuple
|
|
|
|
|
from itertools import islice
|
|
|
|
|
|
|
|
|
|
from .models import Room
|
|
|
|
|
|
2016-10-24 20:35:56 +00:00
|
|
|
|
from proposals.models import Conference, Talk
|
2016-10-16 21:36:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Event = namedtuple('Event', ['talk', 'row', 'rowcount'])
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class Program:
|
2016-10-31 22:42:07 +00:00
|
|
|
|
def __init__(self, site, pending=False, cache=True):
|
2016-10-24 20:35:56 +00:00
|
|
|
|
self.site = site
|
2016-10-31 22:42:07 +00:00
|
|
|
|
self.pending = pending
|
|
|
|
|
self.cache = cache
|
|
|
|
|
self.initialized = False
|
|
|
|
|
|
|
|
|
|
def _lazy_init(self):
|
2016-10-24 21:24:33 +00:00
|
|
|
|
self.conference = Conference.objects.get(site=self.site)
|
2016-10-16 21:36:03 +00:00
|
|
|
|
self.talks = Talk.objects.\
|
2016-10-31 12:23:02 +00:00
|
|
|
|
exclude(event__label__exact='').\
|
2016-10-31 22:42:07 +00:00
|
|
|
|
filter(site=self.site, room__isnull=False, start_date__isnull=False).\
|
2016-10-24 20:59:44 +00:00
|
|
|
|
filter(Q(duration__gt=0) | Q(event__duration__gt=0))
|
|
|
|
|
|
2016-10-31 22:42:07 +00:00
|
|
|
|
if self.pending:
|
2016-10-24 20:59:44 +00:00
|
|
|
|
self.talks = self.talks.exclude(accepted=False)
|
|
|
|
|
else:
|
|
|
|
|
self.talks = self.talks.filter(accepted=True)
|
|
|
|
|
|
|
|
|
|
self.talks = self.talks.order_by('start_date')
|
2016-10-16 21:36:03 +00:00
|
|
|
|
|
2016-10-31 22:42:07 +00:00
|
|
|
|
self.rooms = Room.objects.filter(talk__in=self.talks.all()).order_by('name').distinct()
|
2016-10-17 12:22:34 +00:00
|
|
|
|
|
2016-10-18 21:52:33 +00:00
|
|
|
|
self.days = {}
|
2016-10-16 21:36:03 +00:00
|
|
|
|
for talk in self.talks.all():
|
2016-10-19 12:02:54 +00:00
|
|
|
|
duration = talk.estimated_duration
|
2016-10-16 21:36:03 +00:00
|
|
|
|
assert(duration)
|
2016-10-18 21:52:33 +00:00
|
|
|
|
dt1 = talk.start_date
|
|
|
|
|
d1 = localtime(dt1).date()
|
|
|
|
|
if d1 not in self.days.keys():
|
|
|
|
|
self.days[d1] = {'timeslots': []}
|
|
|
|
|
dt2 = dt1 + timedelta(minutes=duration)
|
|
|
|
|
d2 = localtime(dt2).date()
|
|
|
|
|
if d2 not in self.days.keys():
|
|
|
|
|
self.days[d2] = {'timeslots': []}
|
|
|
|
|
if dt1 not in self.days[d1]['timeslots']:
|
|
|
|
|
self.days[d1]['timeslots'].append(dt1)
|
|
|
|
|
if dt2 not in self.days[d2]['timeslots']:
|
|
|
|
|
self.days[d2]['timeslots'].append(dt2)
|
2016-10-16 21:36:03 +00:00
|
|
|
|
|
2016-10-31 22:42:07 +00:00
|
|
|
|
self.cols = OrderedDict([(room, 1) for room in self.rooms])
|
|
|
|
|
for day in self.days.keys():
|
|
|
|
|
self.days[day]['timeslots'] = sorted(self.days[day]['timeslots'])
|
|
|
|
|
self.days[day]['rows'] = OrderedDict([(timeslot, OrderedDict([(room, []) for room in self.rooms])) for timeslot in self.days[day]['timeslots'][:-1]])
|
|
|
|
|
|
|
|
|
|
for talk in self.talks.exclude(plenary=True).all():
|
|
|
|
|
self._add_talk(talk)
|
|
|
|
|
|
|
|
|
|
for talk in self.talks.filter(plenary=True).all():
|
|
|
|
|
self._add_talk(talk)
|
|
|
|
|
|
|
|
|
|
self.initialized = True
|
|
|
|
|
|
2016-10-16 21:36:03 +00:00
|
|
|
|
def _add_talk(self, talk):
|
|
|
|
|
room = talk.room
|
2016-10-18 21:52:33 +00:00
|
|
|
|
dt1 = talk.start_date
|
|
|
|
|
d1 = localtime(dt1).date()
|
2016-10-19 12:02:54 +00:00
|
|
|
|
dt2 = talk.start_date + timedelta(minutes=talk.estimated_duration)
|
2016-10-18 21:52:33 +00:00
|
|
|
|
d2 = localtime(dt2).date()
|
|
|
|
|
assert(d1 == d2) # this is a current limitation
|
|
|
|
|
dt1 = self.days[d1]['timeslots'].index(dt1)
|
|
|
|
|
dt2 = self.days[d1]['timeslots'].index(dt2)
|
2016-10-16 21:36:03 +00:00
|
|
|
|
col = None
|
2016-10-18 21:52:33 +00:00
|
|
|
|
for row, timeslot in enumerate(islice(self.days[d1]['timeslots'], dt1, dt2)):
|
2016-10-16 21:36:03 +00:00
|
|
|
|
if col is None:
|
|
|
|
|
col = 0
|
2016-10-18 21:52:33 +00:00
|
|
|
|
while col < len(self.days[d1]['rows'][timeslot][room]) and self.days[d1]['rows'][timeslot][room][col]:
|
2016-10-16 21:36:03 +00:00
|
|
|
|
col += 1
|
|
|
|
|
self.cols[room] = max(self.cols[room], col+1)
|
2016-10-18 21:52:33 +00:00
|
|
|
|
event = Event(talk=talk, row=row, rowcount=dt2-dt1)
|
|
|
|
|
while len(self.days[d1]['rows'][timeslot][room]) <= col:
|
|
|
|
|
self.days[d1]['rows'][timeslot][room].append(None)
|
|
|
|
|
self.days[d1]['rows'][timeslot][room][col] = event
|
2016-10-16 21:36:03 +00:00
|
|
|
|
|
2016-10-31 22:42:07 +00:00
|
|
|
|
def _html_header(self):
|
2016-10-16 21:36:03 +00:00
|
|
|
|
output = '<td>Room</td>'
|
|
|
|
|
room_cell = '<td%(options)s>%(name)s<br><b>%(label)s</b></td>'
|
|
|
|
|
for room, colspan in self.cols.items():
|
2016-10-16 21:55:27 +00:00
|
|
|
|
options = ' style="min-width: 100px;" colspan="%d"' % colspan
|
2016-10-16 21:49:18 +00:00
|
|
|
|
output += room_cell % {'name': escape(room.name), 'label': escape(room.label), 'options': options}
|
2016-10-16 21:36:03 +00:00
|
|
|
|
return '<tr>%s</tr>' % output
|
|
|
|
|
|
2016-10-31 22:42:07 +00:00
|
|
|
|
def _html_body(self):
|
2016-10-18 21:52:33 +00:00
|
|
|
|
output = ''
|
2016-10-18 22:01:10 +00:00
|
|
|
|
for day in sorted(self.days.keys()):
|
2016-10-31 22:42:07 +00:00
|
|
|
|
output += self._html_day_header(day)
|
|
|
|
|
output += self._html_day(day)
|
2016-10-18 21:52:33 +00:00
|
|
|
|
return output
|
|
|
|
|
|
2016-10-31 22:42:07 +00:00
|
|
|
|
def _html_day_header(self, day):
|
2016-10-18 21:52:33 +00:00
|
|
|
|
row = '<tr><td colspan="%(colcount)s"><h3>%(day)s</h3></td></tr>'
|
|
|
|
|
colcount = 1
|
|
|
|
|
for room, col in self.cols.items():
|
|
|
|
|
colcount += col
|
|
|
|
|
return row % {
|
|
|
|
|
'colcount': colcount,
|
|
|
|
|
'day': datetime.strftime(day, '%A %d %B'),
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-31 22:42:07 +00:00
|
|
|
|
def _html_day(self, day):
|
2016-10-16 21:36:03 +00:00
|
|
|
|
output = []
|
2016-10-18 21:52:33 +00:00
|
|
|
|
rows = self.days[day]['rows']
|
|
|
|
|
for ts, rooms in rows.items():
|
2016-10-31 22:42:07 +00:00
|
|
|
|
output.append(self._html_row(day, ts, rooms))
|
2016-10-16 21:36:03 +00:00
|
|
|
|
return '\n'.join(output)
|
|
|
|
|
|
2016-10-31 22:42:07 +00:00
|
|
|
|
def _html_row(self, day, ts, rooms):
|
2016-10-18 21:52:33 +00:00
|
|
|
|
row = '<tr style="%(style)s">%(timeslot)s%(content)s</tr>'
|
|
|
|
|
cell = '<td%(options)s>%(content)s</td>'
|
|
|
|
|
content = ''
|
|
|
|
|
for room, events in rooms.items():
|
|
|
|
|
colspan = 1
|
|
|
|
|
for i in range(self.cols[room]):
|
|
|
|
|
options = ' colspan="%d"' % colspan
|
|
|
|
|
cellcontent = ''
|
|
|
|
|
if i < len(events) and events[i]:
|
|
|
|
|
event = events[i]
|
|
|
|
|
if event.row != 0:
|
|
|
|
|
continue
|
|
|
|
|
options = ' rowspan="%d" bgcolor="%s"' % (event.rowcount, event.talk.event.color)
|
2016-10-18 22:09:31 +00:00
|
|
|
|
cellcontent = escape(str(event.talk)) + '<br><em>' + escape(event.talk.get_speakers_str()) + '</em>'
|
2016-10-18 21:52:33 +00:00
|
|
|
|
elif (i+1 > len(events) or not events[i+1]) and i+1 < self.cols[room]:
|
|
|
|
|
colspan += 1
|
|
|
|
|
continue
|
|
|
|
|
colspan = 1
|
2016-10-18 22:09:31 +00:00
|
|
|
|
content += cell % {'options': options, 'content': mark_safe(cellcontent)}
|
2016-10-31 22:42:07 +00:00
|
|
|
|
style, timeslot = self._html_timeslot(day, ts)
|
2016-10-18 21:52:33 +00:00
|
|
|
|
return row % {
|
|
|
|
|
'style': style,
|
|
|
|
|
'timeslot': timeslot,
|
|
|
|
|
'content': content,
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-31 22:42:07 +00:00
|
|
|
|
def _html_timeslot(self, day, ts):
|
2016-10-16 21:36:03 +00:00
|
|
|
|
template = '<td>%(content)s</td>'
|
|
|
|
|
start = ts
|
2016-10-18 21:52:33 +00:00
|
|
|
|
end = self.days[day]['timeslots'][self.days[day]['timeslots'].index(ts)+1]
|
2016-10-16 21:36:03 +00:00
|
|
|
|
duration = (end - start).seconds / 60
|
|
|
|
|
date_to_string = lambda date: datetime.strftime(localtime(date), '%H:%M')
|
2016-10-16 21:49:18 +00:00
|
|
|
|
style = 'height: %dpx;' % int(duration * 1.2)
|
2016-10-16 21:36:03 +00:00
|
|
|
|
timeslot = '<td>%s – %s</td>' % tuple(map(date_to_string, [start, end]))
|
|
|
|
|
return style, timeslot
|
|
|
|
|
|
2016-10-31 22:42:07 +00:00
|
|
|
|
def _as_html(self):
|
2016-10-16 21:36:03 +00:00
|
|
|
|
template = """<table class="table table-bordered text-center">\n%(header)s\n%(body)s\n</table>"""
|
2016-10-31 22:42:07 +00:00
|
|
|
|
if not self.initialized:
|
|
|
|
|
self._lazy_init()
|
2016-10-31 22:51:06 +00:00
|
|
|
|
return template % {
|
2016-10-31 22:42:07 +00:00
|
|
|
|
'header': self._html_header(),
|
|
|
|
|
'body': self._html_body(),
|
2016-10-31 22:51:06 +00:00
|
|
|
|
}
|
2016-10-24 20:35:56 +00:00
|
|
|
|
|
2016-10-31 22:42:07 +00:00
|
|
|
|
def _as_xml(self):
|
|
|
|
|
if not self.initialized:
|
|
|
|
|
self._lazy_init()
|
2016-10-24 20:35:56 +00:00
|
|
|
|
result = """<?xml version="1.0" encoding="UTF-8"?>
|
|
|
|
|
<schedule>
|
|
|
|
|
%(conference)s
|
|
|
|
|
%(days)s
|
|
|
|
|
</schedule>
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
if not len(self.days):
|
|
|
|
|
return result % {'conference': '', 'days': ''}
|
|
|
|
|
|
|
|
|
|
conference_xml = """<conference>
|
|
|
|
|
<title>%(title)s</title>
|
|
|
|
|
<subtitle></subtitle>
|
|
|
|
|
<venue>%(venue)s</venue>
|
|
|
|
|
<city>%(city)s</city>
|
|
|
|
|
<start>%(start_date)s</start>
|
|
|
|
|
<end>%(end_date)s</end>
|
|
|
|
|
<days>%(days_count)s</days>
|
|
|
|
|
<day_change>09:00:00</day_change>
|
|
|
|
|
<timeslot_duration>00:05:00</timeslot_duration>
|
|
|
|
|
</conference>
|
|
|
|
|
""" % {
|
|
|
|
|
'title': self.site.name,
|
2016-10-24 21:24:33 +00:00
|
|
|
|
'venue': ', '.join(map(lambda x: x.strip(), self.conference.venue.split('\n'))),
|
|
|
|
|
'city': self.conference.city,
|
2016-10-24 20:35:56 +00:00
|
|
|
|
'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())):
|
|
|
|
|
days_xml += '<day index="%(index)s" date="%(date)s">\n' % {
|
|
|
|
|
'index': index + 1,
|
|
|
|
|
'date': day.strftime('%Y-%m-%d'),
|
|
|
|
|
}
|
|
|
|
|
for room in self.rooms.all():
|
|
|
|
|
days_xml += ' <room name="%s">\n' % room.name
|
|
|
|
|
for talk in self.talks.filter(room=room).order_by('start_date'):
|
|
|
|
|
if localtime(talk.start_date).date() != day:
|
|
|
|
|
continue
|
|
|
|
|
duration = talk.estimated_duration
|
|
|
|
|
persons = ''
|
|
|
|
|
for speaker in talk.speakers.all():
|
|
|
|
|
persons += ' <person id="%(person_id)s">%(person)s</person>\n' % {
|
|
|
|
|
'person_id': speaker.id,
|
2016-10-31 12:34:02 +00:00
|
|
|
|
'person': str(speaker.profile),
|
2016-10-24 20:35:56 +00:00
|
|
|
|
}
|
2016-11-08 23:41:54 +00:00
|
|
|
|
links = ''
|
|
|
|
|
registration = ''
|
2016-11-08 23:44:30 +00:00
|
|
|
|
if talk.registration_required and self.conference.subscriptions_open:
|
2016-11-08 23:41:54 +00:00
|
|
|
|
links += mark_safe("""
|
|
|
|
|
<link tag="registration">%(link)s</link>""" % {
|
2016-11-12 12:56:21 +00:00
|
|
|
|
'link': reverse('register-for-a-talk', args=[talk.slug]),
|
2016-11-08 23:41:54 +00:00
|
|
|
|
})
|
|
|
|
|
registration = """
|
|
|
|
|
<attendees_max>%(max)s</attendees_max>
|
|
|
|
|
<attendees_remain>%(remain)s</attendees_remain>""" % {
|
|
|
|
|
'max': talk.attendees_limit,
|
|
|
|
|
'remain': talk.remaining_attendees or 0,
|
|
|
|
|
}
|
2017-01-16 22:14:50 +00:00
|
|
|
|
if talk.materials:
|
|
|
|
|
links += mark_safe("""
|
|
|
|
|
<link tag="slides">%(link)s</link>""" % {
|
|
|
|
|
'link': talk.materials.url,
|
|
|
|
|
})
|
|
|
|
|
if talk.video:
|
|
|
|
|
links += mark_safe("""
|
|
|
|
|
<link tag="video">%(link)s</link>""" % {
|
|
|
|
|
'link': talk.video,
|
|
|
|
|
})
|
2016-10-24 20:35:56 +00:00
|
|
|
|
days_xml += """ <event id="%(id)s">
|
|
|
|
|
<start>%(start)s</start>
|
|
|
|
|
<duration>%(duration)s</duration>
|
|
|
|
|
<room>%(room)s</room>
|
|
|
|
|
<slug>%(slug)s</slug>
|
|
|
|
|
<title>%(title)s</title>
|
|
|
|
|
<subtitle/>
|
|
|
|
|
<track>%(track)s</track>
|
|
|
|
|
<type>%(type)s</type>
|
|
|
|
|
<language/>
|
|
|
|
|
<abstract>%(abstract)s</abstract>
|
|
|
|
|
<description>%(description)s</description>
|
|
|
|
|
<persons>
|
|
|
|
|
%(persons)s </persons>
|
2016-11-08 23:41:54 +00:00
|
|
|
|
<links>%(links)s
|
|
|
|
|
</links>%(registration)s
|
2016-10-24 20:35:56 +00:00
|
|
|
|
</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),
|
2016-10-31 12:23:02 +00:00
|
|
|
|
'track': escape(talk.track or ''),
|
|
|
|
|
'type': escape(talk.event.label),
|
2016-10-24 20:35:56 +00:00
|
|
|
|
'abstract': escape(talk.abstract),
|
|
|
|
|
'description': escape(talk.description),
|
|
|
|
|
'persons': persons,
|
2016-11-08 23:41:54 +00:00
|
|
|
|
'links': links,
|
|
|
|
|
'registration': registration,
|
2016-10-24 20:35:56 +00:00
|
|
|
|
}
|
|
|
|
|
days_xml += ' </room>\n'
|
|
|
|
|
days_xml += '</day>\n'
|
|
|
|
|
|
|
|
|
|
return result % {
|
|
|
|
|
'conference': '\n'.join(map(lambda x: ' ' + x, conference_xml.split('\n'))),
|
|
|
|
|
'days': '\n'.join(map(lambda x: ' ' + x, days_xml.split('\n'))),
|
|
|
|
|
}
|
|
|
|
|
|
2016-11-08 16:01:29 +00:00
|
|
|
|
def _as_ics(self):
|
|
|
|
|
if not self.initialized:
|
|
|
|
|
self._lazy_init()
|
|
|
|
|
talks = [ICS_TALK.format(site=self.site, talk=talk) for talk in self.talks]
|
|
|
|
|
return ICS_MAIN.format(site=self.site, talks='\n'.join(talks))
|
|
|
|
|
|
2016-10-31 22:42:07 +00:00
|
|
|
|
def render(self, output='html'):
|
|
|
|
|
if self.cache:
|
|
|
|
|
cache_entry = 'program.%s.%s' % ('pending' if self.pending else 'final', output)
|
|
|
|
|
result = cache.get(cache_entry)
|
|
|
|
|
if not result:
|
|
|
|
|
result = getattr(self, '_as_%s' % output)()
|
|
|
|
|
cache.set(cache_entry, result, 3 * 60 * 60) # 3H
|
2016-10-31 22:51:06 +00:00
|
|
|
|
return mark_safe(result)
|
2016-10-31 22:42:07 +00:00
|
|
|
|
else:
|
2016-10-31 22:51:06 +00:00
|
|
|
|
return mark_safe(getattr(self, '_as_%s' % output)())
|
2016-10-31 22:42:07 +00:00
|
|
|
|
|
2016-10-24 20:35:56 +00:00
|
|
|
|
def __str__(self):
|
2016-10-31 22:42:07 +00:00
|
|
|
|
return self.render()
|
2016-11-08 16:01:29 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# FIXME definitely the wrong place for this, but hey, other templates are already here :P
|
|
|
|
|
|
|
|
|
|
ICS_MAIN = """BEGIN:VCALENDAR
|
|
|
|
|
PRODID:-//{site.domain}//{site.name}//FR
|
|
|
|
|
X-WR-CALNAME:PonyConf
|
|
|
|
|
X-WR-TIMEZONE:Europe/Paris
|
|
|
|
|
VERSION:2.0
|
|
|
|
|
CALSCALE:GREGORIAN
|
|
|
|
|
METHOD:PUBLISH
|
|
|
|
|
{talks}
|
|
|
|
|
END:VCALENDAR"""
|
|
|
|
|
|
|
|
|
|
ICS_TALK = """BEGIN:VEVENT
|
|
|
|
|
DTSTART:{talk.dtstart}
|
|
|
|
|
DTEND:{talk.dtend}
|
2016-11-13 20:21:56 +00:00
|
|
|
|
SUMMARY:{talk.title}
|
2016-11-08 16:01:29 +00:00
|
|
|
|
LOCATION:{talk.room}
|
|
|
|
|
STATUS: CONFIRMED
|
2016-11-13 20:21:56 +00:00
|
|
|
|
DESCRIPTION:{talk.abstract}\n---\n\n{talk.description}
|
2016-11-08 16:01:29 +00:00
|
|
|
|
UID:{site.domain}/{talk.id}
|
|
|
|
|
END:VEVENT
|
|
|
|
|
"""
|