Skip to content

Instantly share code, notes, and snippets.

@codersquid
Created May 16, 2014 18:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codersquid/3074cb52aabfcbcd5b49 to your computer and use it in GitHub Desktop.
Save codersquid/3074cb52aabfcbcd5b49 to your computer and use it in GitHub Desktop.
one-off script to load up a lot of room slots with breaks
#!/usr/bin/env python
import os
import argparse
import csv
from datetime import datetime
import time
"""
This is a lazy one-off to get a bunch of Breaks listed in the schedule grid.
The current site allows one to upload a csv file to clobber things, but we already
have a grid. It needs a lot of work. I don't want to throw together a bunch of
new views and templates and crap. So, script ahoy!
This script is like running manage.py. The DJANGO_SETTINGS_MODULE environment
variable needs to be defined, or the default value of "scipy2014.settings"
must be in the python path.
In order for this to work a Schedule, Room, and SlotKind must already exist.
It will take a file like so:
date,time_start,time_end
7/8/2014,7:00,7:30
7/8/2014,7:30,8:00
7/8/2014,8:00,8:30
7/8/2014,15:00,15:15
7/8/2014,15:15,15:30
Which you will pass to this:
python sched.py --csv talkbreaks.csv --room_name 'Room 204'
This is sloppy. It is not atomic. Backup everything before doing anything.
"""
def parse_row(row):
date = datetime.strptime(row['date'], "%m/%d/%Y")
start = get_time(row['time_start'])
end = get_time(row['time_end'])
return date, start, end
def get_time(timestring):
time_obj = time.strptime(timestring, '%H:%M')
time_obj = datetime(100, 1, 1, time_obj.tm_hour, time_obj.tm_min, 00)
return time_obj.time()
def build_parser():
parser = argparse.ArgumentParser(description="""
""")
parser.add_argument('--csv', type=file, help='schedule csv')
parser.add_argument('--schedule_id', default=2)
parser.add_argument('--room_name', default='Grand Ballroom')
parser.add_argument('--slotkind', default='Break')
return parser
if __name__ == '__main__':
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "scipy2014.settings")
from symposion.schedule.models import Day, Room, Schedule, Slot, SlotKind, SlotRoom
parser = build_parser()
args = parser.parse_args()
schedule = Schedule.objects.get(pk=args.schedule_id)
room = Room.objects.get(schedule=schedule, name=args.room_name)
slotkind = SlotKind.objects.get(schedule=schedule, label=args.slotkind)
reader = csv.DictReader(args.csv)
data = [dict((k.strip(), v.strip()) for k, v in x.items()) for x in reader]
for row in data:
date, start, end = parse_row(row)
day = Day.objects.get(schedule=schedule, date=date)
slot = Slot.objects.create(
kind=slotkind,
day=day,
start=start,
end=end,
)
slotroom = SlotRoom.objects.create(slot=slot, room=room)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment