From 619a3114878684be965679553630965c27731a32 Mon Sep 17 00:00:00 2001 From: Clinton Roy Date: Sun, 13 Oct 2019 10:29:59 +1100 Subject: [PATCH] basic parsing of talks csv --- .../management/commands/update_schedule.py | 66 +++++++++++++++++-- 1 file changed, 59 insertions(+), 7 deletions(-) diff --git a/pinaxcon/registrasion/management/commands/update_schedule.py b/pinaxcon/registrasion/management/commands/update_schedule.py index e0f2a690..dbfb936c 100644 --- a/pinaxcon/registrasion/management/commands/update_schedule.py +++ b/pinaxcon/registrasion/management/commands/update_schedule.py @@ -1,12 +1,15 @@ from django.core.management.base import BaseCommand +from dateutil.parser import parse + from collections import Counter from pathlib import Path import csv class Command(BaseCommand): known_headers = ["date", "start time", "end time", "kind", "rooms"] - + SLOTS = 'slots' + TALKS = 'talks' help = "Updates the schedule based on two csv files, "\ "one that gives all the talk slots, the other the talks." @@ -16,14 +19,15 @@ class Command(BaseCommand): return (date, start_time, end_time, room) def add_arguments(self, parser): - parser.add_argument('timeslots', type=Path) - parser.add_argument('talks', nargs="?", type=Path, default=None) + parser.add_argument(self.SLOTS, type=Path) + parser.add_argument(self.TALKS, nargs="?", type=Path, default=None) def handle(self, *args, **options): + all_kinds, all_rooms = set(), set() slot = {} slot_type_count = Counter() - with open(options['timeslots']) as csv_file: + with open(options[self.SLOTS]) as csv_file: csv_reader = csv.reader(csv_file) headers = next(csv_reader) @@ -33,12 +37,60 @@ class Command(BaseCommand): for row in csv_reader: assert len(row) == len(self.known_headers) - date, start_time, end_time, kind, rooms = row + rowdate, start_time, end_time, kind, rooms = row + all_kinds.add(kind) + + if rowdate: + date = rowdate + + assert kind, "kind cannot be blank" slot_type_count[(date, kind)] += 1 + slot_name = f"{kind} {slot_type_count[(date, kind)]}" + for room in rooms.split(';'): room = room.strip() - slot[(date, kind, slot_type_count[(date, kind)])] = self.find_or_create_slot( + all_rooms.add(room) + + slot[(date, slot_name, room)] = self.find_or_create_slot( date, start_time, end_time, room) - + print(slot.keys()) + + if options[self.TALKS]: + with open(options[self.TALKS]) as csv_file: + csv_reader = csv.reader(csv_file) + + used_rooms = next(csv_reader) + + assert used_rooms[0] == '', "Cell (1, 1) must be empty" + + for room in used_rooms[1:]: + assert room in all_rooms, f"Unknown room: {room}" + + for row in csv_reader: + cell = row[0] + + if cell.rsplit(' ', 1)[0] in all_kinds: + kind = cell + + print('used_rooms', used_rooms) + + for i, room in enumerate(used_rooms[1:], 1): + talk_id = row[i] + + if not talk_id: + continue + + assert (date, kind, room) in slot, f"Slot ({date}, '{kind}', '{room}') not found" + + # TODO set the talk slot to the associated talk + # place_talk(slot[(date, slot_name)].talk = int(talk_id) + + else: + assert parse(row[0]), "Not a date: {row[0]}" + + date = row[0] + + for col in row[1:]: + assert col == '', f"All other columns must be empty: {date}"