def event_join(host, guest): online_students = StudentRecord.online_students() ## Key error is possible guest_record = online_students[int(guest)] host_record = online_students[int(host)] host_channel = sse.current_channel(host) guest_channel = sse.current_channel(guest) sse.listen_to(host, guest) m = dict(cid=guest, host_cid=host, board_status=host_record.open_board) if host!=guest and int(host)!=int(host_channel): # host is elsewhere host_of_host = online_students[int(host_channel)] m.update(notice = "%s is visiting %s's sandbox" % (host_record.username, host_of_host.username)) if guest_record.is_teacher: pids = sorted(int(i) for i in red.smembers('published-problems')) scores = [ [p, host_record.scores.get(p,0)] for p in pids ] m.update(scores=scores, brownies=host_record.brownies) sse.notify( { guest : m } , event='join') ## Inform old channel and new channel of updated listeners list guest_listeners = [ online_students[int(i)].username for i in sse.listening_clients(guest_channel) ] m = dict(host_cid=guest_channel, listeners=guest_listeners) sse.broadcast(guest_channel, m, m, event='listeners-update') host_listeners = [ online_students[int(i)].username for i in sse.listening_clients(host) ] m = dict(host_cid=host, listeners=host_listeners) sse.broadcast(host, m, m, event='listeners-update')
def index(): logged_in_user = auth.get_logged_in_user() user_record = StudentRecord(logged_in_user.id) user_record.online = True user_record.save() # if user is running on another client, close the other. sse.close(user_record.id) current_channel = sse.current_channel(user_record.id) or user_record.id all_users = auth.User.select() online_students = StudentRecord.online_students() listeners=[ online_students[int(i)].username \ for i in sse.listening_clients(current_channel) if int(i) in online_students ] # notify those who can view boards that the client is online messages = {} for c, r in online_students.items(): if user_record.id != c and (r.is_teacher or user_record.open_board): messages[c] = dict(cid=user_record.id, board_status=user_record.open_board) sse.notify(messages, event='online') problem_ids = sorted(int(i) for i in red.smembers('published-problems')) return render_template('sandbox.html', sum=sum, enumerate=enumerate, current_channel = current_channel, problem_ids=problem_ids, user_record = user_record, online_students = online_students, all_users = all_users, listeners = listeners)
def event_chat(message, cid): chatter = StudentRecord(cid) channel = sse.current_channel(cid) teachers = StudentRecord.all_students(lambda v: v.is_teacher == True) m = dict(cid=channel, chat=message, chat_id=chat_id(), uid=chatter.id, username=chatter.username) sse.broadcast(channel, m, m, additional_channels=teachers.keys(), event='chat')
def event_join(host, guest): online_students = StudentRecord.online_students() ## Key error is possible guest_record = online_students[int(guest)] host_record = online_students[int(host)] host_channel = sse.current_channel(host) guest_channel = sse.current_channel(guest) sse.listen_to(host, guest) m = dict(cid=guest, host_cid=host, board_status=host_record.open_board) if host != guest and int(host) != int(host_channel): # host is elsewhere host_of_host = online_students[int(host_channel)] m.update(notice="%s is visiting %s's sandbox" % (host_record.username, host_of_host.username)) if guest_record.is_teacher: pids = sorted(int(i) for i in red.smembers('published-problems')) scores = [[p, host_record.scores.get(p, 0)] for p in pids] m.update(scores=scores, brownies=host_record.brownies) sse.notify({guest: m}, event='join') ## Inform old channel and new channel of updated listeners list guest_listeners = [ online_students[int(i)].username for i in sse.listening_clients(guest_channel) ] m = dict(host_cid=guest_channel, listeners=guest_listeners) sse.broadcast(guest_channel, m, m, event='listeners-update') host_listeners = [ online_students[int(i)].username for i in sse.listening_clients(host) ] m = dict(host_cid=host, listeners=host_listeners) sse.broadcast(host, m, m, event='listeners-update')
def publish_toggle(pid): if not red.sismember("published-problems", pid): red.sadd("published-problems", pid) else: red.srem("published-problems", pid) all_records = StudentRecord.online_students() pids = sorted(int(p) for p in red.smembers("published-problems")) messages = {} for k, v in all_records.items(): uid = sse.current_channel(k) if v.is_teacher else k scores = [[pid, all_records[uid].scores.get(pid, 0)] for pid in pids] messages[k] = dict(cid=uid, scores=scores) sse.notify(messages, event="problems-updated") return redirect(url_for("problem.index"))
def publish_toggle(pid): if not red.sismember('published-problems', pid): red.sadd('published-problems', pid) else: red.srem('published-problems', pid) all_records = StudentRecord.online_students() pids = sorted(int(p) for p in red.smembers('published-problems')) messages = {} for k, v in all_records.items(): uid = sse.current_channel(k) if v.is_teacher else k scores = [[pid, all_records[uid].scores.get(pid, 0)] for pid in pids] messages[k] = dict(cid=uid, scores=scores) sse.notify(messages, event="problems-updated") return redirect(url_for('problem.index'))
def index(): logged_in_user = auth.get_logged_in_user() user_record = StudentRecord(logged_in_user.id) user_record.online = True user_record.save() # if user is running on another client, close the other. sse.close(user_record.id) current_channel = sse.current_channel(user_record.id) or user_record.id all_users = auth.User.select() online_students = StudentRecord.online_students() listeners=[ online_students[int(i)].username \ for i in sse.listening_clients(current_channel) if int(i) in online_students ] # notify those who can view boards that the client is online messages = {} for c, r in online_students.items(): if user_record.id != c and (r.is_teacher or user_record.open_board): messages[c] = dict(cid=user_record.id, board_status=user_record.open_board) sse.notify(messages, event='online') problem_ids = sorted(int(i) for i in red.smembers('published-problems')) return render_template('sandbox.html', sum=sum, enumerate=enumerate, current_channel=current_channel, problem_ids=problem_ids, user_record=user_record, online_students=online_students, all_users=all_users, listeners=listeners)
def event_chat(message, cid): chatter = StudentRecord(cid) channel = sse.current_channel(cid) teachers = StudentRecord.all_students( lambda v: v.is_teacher == True) m = dict(cid=channel, chat=message, chat_id=chat_id(), uid=chatter.id, username=chatter.username) sse.broadcast(channel, m, m, additional_channels=teachers.keys(), event='chat')