Exemplo n.º 1
0
def get_subscriber_ids(id: IntLike):
    key = KEYS.official_account_subscribers.format(id)
    ids = rd.smembers(key)
    if not ids:
        _cache_account_subscribers(id)
        ids = rd.smembers(key)
    rd.expire(key, KEYS.official_account_subscribers_expire)
    ids = [t.decode() for t in ids]
    return ids
Exemplo n.º 2
0
def get_follower_ids(id: IntLike):
    key = Keys.user_followers.format(id)
    # returns an empty set if key not exists
    ids = rd.smembers(key)
    if not ids:
        _cache_followers(id)
        ids = rd.smembers(key)
    rd.expire(key, Keys.user_followers_expire)
    ids = [t.decode() for t in ids]
    return ids
Exemplo n.º 3
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'}
Exemplo n.º 4
0
def get_groups():

    def group(g):
        return {
            'name': g,
            'sections': sorted(rd.smembers('group:%s' % g)),
        }

    return json.jsonify({
        'groups': sorted(
            (group(g) for g in rd.smembers('groups')),
            key=lambda x: x['name']
        )
    })
Exemplo n.º 5
0
def get_timetable():
    q = request.query_string.decode()
    if not q:
        return '', 200, {'content-type': 'text/calendar'}

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

    sct = []
    all_cn = []

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

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

        all_cn.append(cn)
        sct.append(str(section))

    if calds is None:
        calds = ', '.join(sct)

    cal = Calendar(**{
        '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 %s' % calds,
    })

    cal.subcomponents = tuple(
        l.event for l in Lesson.query.filter(Lesson.class_no.in_(all_cn)).all()
    )

    return cal.to_ical(), 200, {'content-type': 'text/calendar'}
Exemplo n.º 6
0
def get_group_sections():

    q = request.query_string.decode()
    if not q: return json.jsonify({'status': 'error'})
    codes = rd.smembers('group:%s'%q)
    all_cn = []

    for cn in codes:
        try:
            all_cn.insert(0, int(cn))
        except ValueError:
            continue

    schedule = tuple(
        format_event(lesson)
        for lesson in Lesson.query.filter(Lesson.class_no.in_(all_cn))
        .order_by(Lesson.start).all()
    )

    return json.jsonify({
            'status': 'ok', 'events': schedule
        })
Exemplo n.º 7
0
 def group(g):
     return {
         'name': g,
         'sections': sorted(rd.smembers('group:%s' % g)),
     }
Exemplo n.º 8
0
def get_tgrp():
    return json.jsonify({
        g:tuple(rd.smembers('tgrp:%s'%g)) for g in rd.smembers('tgrps')
    })
Exemplo n.º 9
0
def get_groups():
    return json.jsonify({
        g:tuple(rd.smembers('group:%s'%g)) for g in rd.smembers('groups')
    })
Exemplo n.º 10
0
def groupadd(name, members):
    """Add to group"""
    rd.sadd('groups', name)
    if len(members) == 1 and ':' in members[0]:
        members = tuple(rd.smembers(members[0]))
    rd.sadd('group:%s'%name, *members)
Exemplo n.º 11
0
def sect_list(t, g):
    click.echo("%s\t%s" % (g, ' '.join(sorted(rd.smembers('%s:%s' % (t, g))))))
Exemplo n.º 12
0
def grouplist(t, name):
    """List groups"""
    if name:
        sect_list(t, name)
    else:
        for gp in sorted(rd.smembers(t+'s')): sect_list(t, gp)