From 29c95fc96723d9d0fff6a3e90ffc7f294035b413 Mon Sep 17 00:00:00 2001 From: Brian Rosner Date: Thu, 21 Feb 2013 11:32:12 -0700 Subject: [PATCH] added create_slot schedule helper --- symposion/schedule/helpers.py | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 symposion/schedule/helpers.py diff --git a/symposion/schedule/helpers.py b/symposion/schedule/helpers.py new file mode 100644 index 00000000..c5fa8953 --- /dev/null +++ b/symposion/schedule/helpers.py @@ -0,0 +1,30 @@ +""" +This file contains functions that are useful to humans at the shell for +manipulating the database in more natural ways. +""" +from django.db import transaction + +from .models import Schedule, Day, Room, Slot, SlotKind, SlotRoom + + +@transaction.commit_on_success +def create_slot(section_slug, date, kind, start, end, rooms): + schedule = Schedule.objects.get(section__slug=section_slug) + slot = Slot() + slot.day = Day.objects.get(schedule=schedule, date=date) + slot.kind = SlotKind.objects.get(schedule=schedule, label=kind) + slot.start = start + slot.end = end + slot.save(force_insert=True) + if rooms == "all": + rooms_qs = Room.objects.filter(schedule=schedule).order_by("order") + else: + rooms_qs = Room.objects.filter(schedule=schedule, name__in=rooms).order_by("order") + if rooms_qs.count() != len(rooms): + raise Exception("input rooms do not match queried rooms; typo?") + for room in rooms_qs: + slot_room = SlotRoom() + slot_room.slot = slot + slot_room.room = room + slot_room.save(force_insert=True) + print "created {} [start={}; end={}]".format(slot.kind.label, slot.start, slot.end)