Ejemplo n.º 1
0
def get_timetable():

    q = request.query_string.decode()
    if not q: return json.jsonify({'status': 'error'})

    if ',' in q:
        calds = None
        codes = q.split(',')
    else:
        calds = q
        codes = rd.smembers('group:%s'%q)

    locations = rd.hgetall('locations')

    sct = []
    cal = Calendar()

    def get_location(lesson):
        return "%s (%s)" % (locations.get(lesson, "TBD"), lesson)

    def get_calendar_event(lesson):
        e = {
            'summary': lesson.title,
            'description': str(lesson),
            'location': get_location(lesson.location),
            'dtstart': lesson.start, 'dtend': lesson.end,
        }

        event = Event()
        for k, v in e.items(): event.add(k, v)
        return event

    for cn in codes:
        try:
            cn = int(cn)
        except ValueError:
            continue

        section = Section.query.get(cn)
        if not section: continue

        schedule = Lesson.query.filter_by(class_no=cn).all()
        for lesson in schedule: cal.add_component(get_calendar_event(lesson))

        sct.append(str(section))

    caldict = {
        'prodid': '-//SUTD Timetable Calendar//randName//EN',
        'version': '2.0',
        'calscale': 'GREGORIAN',
        'x-wr-timezone': 'Asia/Singapore',
        'x-wr-calname': 'Timetable',
        'x-wr-caldesc': 'Timetable for ' + calds if calds else ', '.join(sct),
    }

    for k, v in caldict.items(): cal.add(k, v)

    return cal.to_ical(), 200, {'content-type': 'text/calendar'}
Ejemplo n.º 2
0
def get_locations():
    return json.jsonify( rd.hgetall('locations') )