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.TALKS, type=Path)
def handle(self, *args, **options):
date_strs = set()
slotkind_names = set()
room_names = set()
slot_details = {}
slot_type_count = Counter()
def parse_slots(self, options):
self.date_strs = set()
self.room_names = set()
self.slotkind_names = set()
self.slot_details = {}
conf = current_conference()
section = Section.objects.filter(conference=conf, slug="main").all().first()
schedule, _created = Schedule.objects.get_or_create(section=section)
slot_type_count = Counter()
with open(options[self.SLOTS]) as csv_file:
csv_reader = csv.reader(csv_file)
@ -47,11 +44,11 @@ class Command(BaseCommand):
assert len(row) == len(self.known_headers)
rowdate, start_time, end_time, kind, rooms = row
slotkind_names.add(kind)
self.slotkind_names.add(kind)
if rowdate:
date = rowdate
date_strs.add(date)
self.date_strs.add(date)
assert kind, "kind cannot be blank"
@ -60,36 +57,39 @@ class Command(BaseCommand):
for room in rooms.split(';'):
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:
csv_reader = csv.reader(csv_file)
used_rooms = next(csv_reader)
talks = {}
self.used_rooms = next(csv_reader)
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:]:
assert room in room_names, f"Unknown room: {room}"
for room in self.used_rooms[1:]:
assert room in self.room_names, f"Unknown room: {room}"
for row in csv_reader:
cell = row[0]
if cell.rsplit(' ', 1)[0] in slotkind_names:
if cell.rsplit(' ', 1)[0] in self.slotkind_names:
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]
if not talk_id:
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:
assert parse(row[0]), "Not a date: {row[0]}"
@ -99,28 +99,33 @@ class Command(BaseCommand):
for col in row[1:]:
assert col == '', f"All other columns must be empty: {date}"
days = {}
for date in date_strs:
def create_simple_models(self):
self.days = {}
for date in self.date_strs:
day, _created = Day.objects.get_or_create(
schedule=schedule, date=date)
days[date] = day
schedule=self.schedule, date=date)
self.days[date] = day
rooms = {}
for room_name in room_names:
self.rooms = {}
for room_name in self.room_names:
room, _created = Room.objects.get_or_create(
schedule=schedule, name=room_name, order=used_rooms.index(room_name))
rooms[room_name] = room
schedule=self.schedule,
name=room_name,
order=self.used_rooms.index(room_name))
self.rooms[room_name] = room
slotkinds = {}
for slotkind_name in slotkind_names:
slotkind, _created = SlotKind.objects.get_or_create(schedule=schedule, label=slotkind_name)
slotkinds[slotkind_name] = slotkind
self.slotkinds = {}
for slotkind_name in self.slotkind_names:
slotkind, _created = SlotKind.objects.get_or_create(
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
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()
@ -135,13 +140,26 @@ class Command(BaseCommand):
# 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],
preso.slot = Slot.objects.create(
day=self.days[date],
kind=self.slotkinds[kind_name],
start=start_time,
end=end_time,
exclusive=exclusive)
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()