refactor a bit.

This commit is contained in:
Clinton Roy 2019-10-13 21:17:29 +11:00 committed by Joel Addison
parent 20469223cb
commit 0d380cd8fc

View file

@ -25,16 +25,13 @@ class Command(BaseCommand):
parser.add_argument(self.SLOTS, type=Path) parser.add_argument(self.SLOTS, type=Path)
parser.add_argument(self.TALKS, type=Path) parser.add_argument(self.TALKS, type=Path)
def handle(self, *args, **options): def parse_slots(self, options):
date_strs = set() self.date_strs = set()
slotkind_names = set() self.room_names = set()
room_names = set() self.slotkind_names = set()
slot_details = {} self.slot_details = {}
slot_type_count = Counter()
conf = current_conference() slot_type_count = Counter()
section = Section.objects.filter(conference=conf, slug="main").all().first()
schedule, _created = Schedule.objects.get_or_create(section=section)
with open(options[self.SLOTS]) as csv_file: with open(options[self.SLOTS]) as csv_file:
csv_reader = csv.reader(csv_file) csv_reader = csv.reader(csv_file)
@ -47,11 +44,11 @@ class Command(BaseCommand):
assert len(row) == len(self.known_headers) assert len(row) == len(self.known_headers)
rowdate, start_time, end_time, kind, rooms = row rowdate, start_time, end_time, kind, rooms = row
slotkind_names.add(kind) self.slotkind_names.add(kind)
if rowdate: if rowdate:
date = rowdate date = rowdate
date_strs.add(date) self.date_strs.add(date)
assert kind, "kind cannot be blank" assert kind, "kind cannot be blank"
@ -60,36 +57,39 @@ class Command(BaseCommand):
for room in rooms.split(';'): for room in rooms.split(';'):
room = room.strip() room = room.strip()
room_names.add(room) self.room_names.add(room)
slot_details[(date, kindslot, room)] = (start_time, end_time) self.slot_details[(date, kindslot, room)] = (start_time, end_time)
def parse_talks(self, options):
self.talks = {}
with open(options[self.TALKS]) as csv_file: with open(options[self.TALKS]) as csv_file:
csv_reader = csv.reader(csv_file) csv_reader = csv.reader(csv_file)
used_rooms = next(csv_reader) self.used_rooms = next(csv_reader)
talks = {}
assert used_rooms[0] == '', "Cell (1, 1) must be empty" assert self.used_rooms[0] == '', "Cell (1, 1) must be empty"
for room in used_rooms[1:]: for room in self.used_rooms[1:]:
assert room in room_names, f"Unknown room: {room}" assert room in self.room_names, f"Unknown room: {room}"
for row in csv_reader: for row in csv_reader:
cell = row[0] cell = row[0]
if cell.rsplit(' ', 1)[0] in slotkind_names: if cell.rsplit(' ', 1)[0] in self.slotkind_names:
kindslot = cell kindslot = cell
for i, room in enumerate(used_rooms[1:], 1): for i, room in enumerate(self.used_rooms[1:], 1):
talk_id = row[i] talk_id = row[i]
if not talk_id: if not talk_id:
continue continue
assert (date, kindslot, room) in slot_details, f"Slot ({date}, '{kindslot}', '{room}') not found" assert (date, kindslot, room) in self.slot_details,\
f"Slot ({date}, '{kindslot}', '{room}') not found"
talks[(date, kindslot, room)] = int(talk_id) self.talks[(date, kindslot, room)] = int(talk_id)
else: else:
assert parse(row[0]), "Not a date: {row[0]}" assert parse(row[0]), "Not a date: {row[0]}"
@ -99,28 +99,33 @@ class Command(BaseCommand):
for col in row[1:]: for col in row[1:]:
assert col == '', f"All other columns must be empty: {date}" assert col == '', f"All other columns must be empty: {date}"
days = {} def create_simple_models(self):
for date in date_strs: self.days = {}
for date in self.date_strs:
day, _created = Day.objects.get_or_create( day, _created = Day.objects.get_or_create(
schedule=schedule, date=date) schedule=self.schedule, date=date)
days[date] = day self.days[date] = day
rooms = {} self.rooms = {}
for room_name in room_names: for room_name in self.room_names:
room, _created = Room.objects.get_or_create( room, _created = Room.objects.get_or_create(
schedule=schedule, name=room_name, order=used_rooms.index(room_name)) schedule=self.schedule,
rooms[room_name] = room name=room_name,
order=self.used_rooms.index(room_name))
self.rooms[room_name] = room
slotkinds = {} self.slotkinds = {}
for slotkind_name in slotkind_names: for slotkind_name in self.slotkind_names:
slotkind, _created = SlotKind.objects.get_or_create(schedule=schedule, label=slotkind_name) slotkind, _created = SlotKind.objects.get_or_create(
slotkinds[slotkind_name] = slotkind schedule=self.schedule, label=slotkind_name)
self.slotkinds[slotkind_name] = slotkind
for details, talk_id in talks.items(): def create_complex_models(self):
for details, talk_id in self.talks.items():
date, kindslot, room_name = details date, kindslot, room_name = details
kind_name = kindslot.rsplit(' ', 1)[0] kind_name = kindslot.rsplit(' ', 1)[0]
(start_time, end_time) = slot_details[(date, kindslot, room_name)] (start_time, end_time) = self.slot_details[(date, kindslot, room_name)]
proposal = ProposalBase.objects.filter(pk=talk_id).first() proposal = ProposalBase.objects.filter(pk=talk_id).first()
@ -135,13 +140,26 @@ class Command(BaseCommand):
# TODO the exclusive list should not be hard coded, another csv file maybe. # TODO the exclusive list should not be hard coded, another csv file maybe.
exclusive = kind_name in ["plenary", "morning tea", "lunch", "afternoon tea"] exclusive = kind_name in ["plenary", "morning tea", "lunch", "afternoon tea"]
preso.slot, _created = Slot.objects.get_or_create( preso.slot = Slot.objects.create(
day=days[date], day=self.days[date],
kind=slotkinds[kind_name], kind=self.slotkinds[kind_name],
start=start_time, start=start_time,
end=end_time, end=end_time,
exclusive=exclusive) exclusive=exclusive)
preso.save() preso.save()
slotroom, _create = SlotRoom.objects.get_or_create(slot=preso.slot, room=rooms[room_name]) slotroom, _create = SlotRoom.objects.get_or_create(
slot=preso.slot,
room=self.rooms[room_name])
def handle(self, *args, **options):
self.parse_slots(options)
self.parse_talks(options)
conf = current_conference()
section = Section.objects.filter(conference=conf, slug="main").all().first()
self.schedule, _created = Schedule.objects.get_or_create(section=section)
self.create_simple_models()
self.create_complex_models()