2019-10-12 03:47:33 +00:00
|
|
|
from django.core.management.base import BaseCommand
|
|
|
|
|
2019-10-13 04:41:21 +00:00
|
|
|
from symposion.conference.models import Section, current_conference
|
2019-10-13 00:54:03 +00:00
|
|
|
|
2019-10-13 04:52:51 +00:00
|
|
|
from symposion.schedule.models import (Day, Presentation, Room, SlotKind, Schedule, Slot, SlotRoom)
|
2019-10-13 00:54:03 +00:00
|
|
|
|
2019-10-13 04:41:21 +00:00
|
|
|
from symposion.proposals.models import ProposalBase
|
2019-10-13 00:54:03 +00:00
|
|
|
|
2019-10-12 23:29:59 +00:00
|
|
|
from dateutil.parser import parse
|
|
|
|
|
2019-10-13 04:41:21 +00:00
|
|
|
from collections import Counter
|
2019-10-12 03:47:33 +00:00
|
|
|
from pathlib import Path
|
|
|
|
import csv
|
|
|
|
|
2019-10-13 03:54:07 +00:00
|
|
|
|
2019-10-12 03:47:33 +00:00
|
|
|
class Command(BaseCommand):
|
|
|
|
known_headers = ["date", "start time", "end time", "kind", "rooms"]
|
2019-10-12 23:29:59 +00:00
|
|
|
SLOTS = 'slots'
|
|
|
|
TALKS = 'talks'
|
2019-10-12 03:47:33 +00:00
|
|
|
|
|
|
|
help = "Updates the schedule based on two csv files, "\
|
|
|
|
"one that gives all the talk slots, the other the talks."
|
|
|
|
|
|
|
|
def add_arguments(self, parser):
|
2019-10-12 23:29:59 +00:00
|
|
|
parser.add_argument(self.SLOTS, type=Path)
|
2019-10-13 00:54:03 +00:00
|
|
|
parser.add_argument(self.TALKS, type=Path)
|
2019-10-12 03:47:33 +00:00
|
|
|
|
|
|
|
def handle(self, *args, **options):
|
2019-10-13 00:54:03 +00:00
|
|
|
date_strs = set()
|
|
|
|
slotkind_names = set()
|
|
|
|
room_names = set()
|
2019-10-13 03:54:07 +00:00
|
|
|
slot_details = {}
|
2019-10-12 03:47:33 +00:00
|
|
|
slot_type_count = Counter()
|
|
|
|
|
2019-10-13 00:54:03 +00:00
|
|
|
conf = current_conference()
|
|
|
|
section = Section.objects.filter(conference=conf, slug="main").all().first()
|
|
|
|
schedule, _created = Schedule.objects.get_or_create(section=section)
|
|
|
|
|
2019-10-12 23:29:59 +00:00
|
|
|
with open(options[self.SLOTS]) as csv_file:
|
2019-10-12 03:47:33 +00:00
|
|
|
csv_reader = csv.reader(csv_file)
|
|
|
|
|
|
|
|
headers = next(csv_reader)
|
|
|
|
|
|
|
|
assert headers == self.known_headers
|
|
|
|
|
|
|
|
for row in csv_reader:
|
|
|
|
assert len(row) == len(self.known_headers)
|
|
|
|
|
2019-10-12 23:29:59 +00:00
|
|
|
rowdate, start_time, end_time, kind, rooms = row
|
2019-10-13 00:54:03 +00:00
|
|
|
slotkind_names.add(kind)
|
2019-10-12 23:29:59 +00:00
|
|
|
|
|
|
|
if rowdate:
|
|
|
|
date = rowdate
|
2019-10-13 00:54:03 +00:00
|
|
|
date_strs.add(date)
|
2019-10-12 23:29:59 +00:00
|
|
|
|
|
|
|
assert kind, "kind cannot be blank"
|
2019-10-12 03:47:33 +00:00
|
|
|
|
|
|
|
slot_type_count[(date, kind)] += 1
|
2019-10-13 03:54:07 +00:00
|
|
|
kindslot = f"{kind} {slot_type_count[(date, kind)]}"
|
2019-10-12 23:29:59 +00:00
|
|
|
|
2019-10-12 03:47:33 +00:00
|
|
|
for room in rooms.split(';'):
|
|
|
|
room = room.strip()
|
2019-10-13 00:54:03 +00:00
|
|
|
room_names.add(room)
|
2019-10-12 23:29:59 +00:00
|
|
|
|
2019-10-13 03:54:07 +00:00
|
|
|
slot_details[(date, kindslot, room)] = (start_time, end_time)
|
2019-10-12 03:47:33 +00:00
|
|
|
|
2019-10-13 00:54:03 +00:00
|
|
|
with open(options[self.TALKS]) as csv_file:
|
|
|
|
csv_reader = csv.reader(csv_file)
|
|
|
|
|
|
|
|
used_rooms = next(csv_reader)
|
2019-10-13 04:41:21 +00:00
|
|
|
talks = {}
|
2019-10-12 23:29:59 +00:00
|
|
|
|
2019-10-13 00:54:03 +00:00
|
|
|
assert used_rooms[0] == '', "Cell (1, 1) must be empty"
|
2019-10-12 23:29:59 +00:00
|
|
|
|
2019-10-13 00:54:03 +00:00
|
|
|
for room in used_rooms[1:]:
|
|
|
|
assert room in room_names, f"Unknown room: {room}"
|
|
|
|
|
|
|
|
for row in csv_reader:
|
|
|
|
cell = row[0]
|
2019-10-12 23:29:59 +00:00
|
|
|
|
2019-10-13 00:54:03 +00:00
|
|
|
if cell.rsplit(' ', 1)[0] in slotkind_names:
|
2019-10-13 03:54:07 +00:00
|
|
|
kindslot = cell
|
2019-10-12 23:29:59 +00:00
|
|
|
|
2019-10-13 00:54:03 +00:00
|
|
|
for i, room in enumerate(used_rooms[1:], 1):
|
|
|
|
talk_id = row[i]
|
2019-10-12 23:29:59 +00:00
|
|
|
|
2019-10-13 00:54:03 +00:00
|
|
|
if not talk_id:
|
|
|
|
continue
|
2019-10-12 23:29:59 +00:00
|
|
|
|
2019-10-13 03:54:07 +00:00
|
|
|
assert (date, kindslot, room) in slot_details, f"Slot ({date}, '{kindslot}', '{room}') not found"
|
2019-10-12 23:29:59 +00:00
|
|
|
|
2019-10-13 04:41:21 +00:00
|
|
|
talks[(date, kindslot, room)] = int(talk_id)
|
2019-10-12 23:29:59 +00:00
|
|
|
|
2019-10-13 00:54:03 +00:00
|
|
|
else:
|
|
|
|
assert parse(row[0]), "Not a date: {row[0]}"
|
2019-10-12 23:29:59 +00:00
|
|
|
|
2019-10-13 00:54:03 +00:00
|
|
|
date = row[0]
|
2019-10-12 23:29:59 +00:00
|
|
|
|
2019-10-13 00:54:03 +00:00
|
|
|
for col in row[1:]:
|
|
|
|
assert col == '', f"All other columns must be empty: {date}"
|
2019-10-12 23:29:59 +00:00
|
|
|
|
2019-10-13 00:54:03 +00:00
|
|
|
days = {}
|
|
|
|
for date in date_strs:
|
|
|
|
day, _created = Day.objects.get_or_create(
|
|
|
|
schedule=schedule, date=date)
|
2019-10-13 03:54:07 +00:00
|
|
|
days[date] = day
|
|
|
|
|
2019-10-13 00:54:03 +00:00
|
|
|
rooms = {}
|
|
|
|
for room_name in room_names:
|
|
|
|
room, _created = Room.objects.get_or_create(
|
|
|
|
schedule=schedule, name=room_name, order=used_rooms.index(room_name))
|
2019-10-13 04:52:51 +00:00
|
|
|
rooms[room_name] = room
|
2019-10-12 23:29:59 +00:00
|
|
|
|
2019-10-13 03:54:07 +00:00
|
|
|
slotkinds = {}
|
2019-10-13 00:54:03 +00:00
|
|
|
for slotkind_name in slotkind_names:
|
|
|
|
slotkind, _created = SlotKind.objects.get_or_create(schedule=schedule, label=slotkind_name)
|
2019-10-13 03:54:07 +00:00
|
|
|
slotkinds[slotkind_name] = slotkind
|
|
|
|
|
2019-10-13 04:41:21 +00:00
|
|
|
for details, talk_id in talks.items():
|
2019-10-13 08:53:26 +00:00
|
|
|
date, kindslot, room_name = details
|
2019-10-13 04:41:21 +00:00
|
|
|
kind_name = kindslot.rsplit(' ', 1)[0]
|
|
|
|
|
2019-10-13 08:53:26 +00:00
|
|
|
(start_time, end_time) = slot_details[(date, kindslot, room_name)]
|
2019-10-13 04:52:51 +00:00
|
|
|
|
2019-10-13 04:41:21 +00:00
|
|
|
proposal = ProposalBase.objects.filter(pk=talk_id).first()
|
|
|
|
|
|
|
|
assert proposal, f"Could not find proposal {talk_id}"
|
|
|
|
|
|
|
|
preso = Presentation.objects.filter(proposal_base=proposal).first()
|
|
|
|
|
2019-10-13 08:53:26 +00:00
|
|
|
assert preso, f"Could not find Presentation for talk {talk_id}"
|
|
|
|
|
|
|
|
if not preso.slot:
|
|
|
|
|
|
|
|
# TODO the exclusive list should not be hard coded, another csv file maybe.
|
|
|
|
exclusive = kind_name in ["plenary", "morning tea", "lunch", "afternoon tea"]
|
|
|
|
|
|
|
|
preso.slot, _created = Slot.objects.get_or_create(
|
|
|
|
day=days[date],
|
|
|
|
kind=slotkinds[kind_name],
|
|
|
|
start=start_time,
|
|
|
|
end=end_time,
|
|
|
|
exclusive=exclusive)
|
2019-10-13 04:41:21 +00:00
|
|
|
|
|
|
|
preso.save()
|
2019-10-13 08:53:26 +00:00
|
|
|
|
|
|
|
slotroom, _create = SlotRoom.objects.get_or_create(slot=preso.slot, room=rooms[room_name])
|