Example #1
0
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)
Example #2
0
def award_brownie(uid, tid, chat_id):
    student = StudentRecord(uid)
    student.brownies += 1
    student.save()

    message = dict(cid=student.id, chat_id=chat_id, brownies=student.brownies)
    sse.notify({uid: message, tid: message}, event="brownie-updated")

    q = Brownie.update(points=Brownie.points + 1).where(Brownie.user == uid)
    if q.execute() == 0:
        q = Brownie.create(user=uid, points=1)

    return "A brownie has been awarded to %s." % student.username
Example #3
0
def award_brownie(uid, tid, chat_id):
    student = StudentRecord(uid)
    student.brownies += 1
    student.save()

    message = dict(cid=student.id, chat_id=chat_id, brownies=student.brownies)
    sse.notify({uid: message, tid: message}, event="brownie-updated")

    q = Brownie.update(points=Brownie.points + 1).where(Brownie.user == uid)
    if q.execute() == 0:
        q = Brownie.create(user=uid, points=1)

    return 'A brownie has been awarded to %s.' % student.username
Example #4
0
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')
Example #5
0
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')
Example #6
0
def grade_history(uid=None):
    logged_in_user = auth.get_logged_in_user()
    if uid is not None:
        if logged_in_user.role != 'teacher' and logged_in_user.id != uid:
            return 'not allowed'
    else:
        uid = logged_in_user.id

    student = StudentRecord(uid)

    try:
        scores = Score.select().where(Score.user == uid)
    except Score.DoesNotExist:
        scores = []

    try:
        brownie = Brownie.get(Brownie.user == uid)
    except Brownie.DoesNotExist:
        brownie = None

    published_pids = red.smembers('published-problems')
    published_pids = sorted(int(p) for p in published_pids)

    return render_template('problem/grade_history.html',
                           scores=scores,
                           brownie=brownie,
                           student=student,
                           published_pids=published_pids)
Example #7
0
def logout_and_cleanup(uid=None, next_url=None, logout=False):
    online_students = StudentRecord.online_students()

    if uid is None:
        user = auth.get_logged_in_user()
        auth.logout_user(user)
    else:
        user = auth.User.get(auth.User.id == uid)
        auth.logout_user(user, self_logout=False)

    user_record = StudentRecord(user.id)
    user_record.open_board = False
    user_record.online = False
    user_record.save()

    listening_clients = sse.listening_clients(user.id)

    # Turn off menu/tabs of all listeners and tell them to go home
    mesg = {}
    for cid in online_students:
        mesg[cid] = dict(cid=user.id)
        if cid in listening_clients or cid == user.id:
            mesg[cid].update(home_cid=cid)
            sse.listen_to(cid, cid)
    sse.notify(mesg, event="log-out")

    sse.close(user_record.id, logout)

    return redirect(next_url or url_for('index'))
Example #8
0
def init_user():
    logged_in_user = auth.get_logged_in_user()
    user_record = StudentRecord(logged_in_user.id)
    user_record.username = logged_in_user.username
    if logged_in_user.role == 'teacher':
        user_record.open_board = True
        user_record.is_teacher = True
    user_record.save()
Example #9
0
def event_toggle_board(message, cid):
   user = StudentRecord(cid)
   if user.username:
      record = StudentRecord(user.id)
      record.open_board = not record.open_board
      record.save()

      all_records = StudentRecord.online_students()

      message_to_all = {}
      listening_clients = sse.listening_clients(cid)
      for c, r in all_records.items():
         m = dict(cid=cid, board_status=record.open_board)
         if record.open_board == False and int(cid) != int(c):
            if not r.is_teacher and c in listening_clients:
               m.update(back_to_homeboard=True)
               sse.listen_to(c, c)
         message_to_all[c] = m

      sse.notify(message_to_all, event="toggle-board")
Example #10
0
def logout_and_cleanup(uid=None, next_url=None, logout=False):
   online_students = StudentRecord.online_students()

   if uid is None:
      user = auth.get_logged_in_user()
      auth.logout_user(user)
   else:
      user = auth.User.get(auth.User.id == uid)
      auth.logout_user(user, self_logout=False)

   user_record = StudentRecord(user.id)
   user_record.open_board = False
   user_record.online = False
   user_record.save()

   listening_clients = sse.listening_clients(user.id)

   # Turn off menu/tabs of all listeners and tell them to go home
   mesg = {}
   for cid in online_students:
      mesg[cid] = dict(cid=user.id)
      if cid in listening_clients or cid==user.id:
         mesg[cid].update(home_cid = cid)
         sse.listen_to(cid, cid)
   sse.notify(mesg, event="log-out")

   sse.close(user_record.id, logout)

   return redirect( next_url or url_for('index') )
Example #11
0
def init_user():
   logged_in_user = auth.get_logged_in_user()
   user_record = StudentRecord(logged_in_user.id)
   user_record.username = logged_in_user.username
   if logged_in_user.role == 'teacher':
      user_record.open_board = True
      user_record.is_teacher = True
   user_record.save()
Example #12
0
def grade():
    uid = int(request.form["uid"])
    tid = int(request.form["tid"])
    pid = int(request.form["pid"])
    score = int(request.form["score"])

    # Store in redis
    user_record = StudentRecord(uid)
    user_record.scores[pid] = score
    user_record.save()

    pids = sorted(int(i) for i in red.smembers("published-problems"))
    scores = [[p, user_record.scores.get(p, 0)] for p in pids]
    message = dict(cid=user_record.id, scores=scores, pid=pid, score=score)

    sse.notify({uid: message, tid: message}, event="scores-updated")

    # Store in database
    q = Score.update(points=score).where(Score.problem == pid, Score.user == uid)
    if q.execute() == 0:
        q = Score.create(problem=pid, user=uid, points=score)

    return '<i class="icon-okay"></i>'
Example #13
0
def grade():
    uid = int(request.form['uid'])
    tid = int(request.form['tid'])
    pid = int(request.form['pid'])
    score = int(request.form['score'])

    # Store in redis
    user_record = StudentRecord(uid)
    user_record.scores[pid] = score
    user_record.save()

    pids = sorted(int(i) for i in red.smembers('published-problems'))
    scores = [[p, user_record.scores.get(p, 0)] for p in pids]
    message = dict(cid=user_record.id, scores=scores, pid=pid, score=score)

    sse.notify({uid: message, tid: message}, event="scores-updated")

    # Store in database
    q = Score.update(points=score).where(Score.problem == pid,
                                         Score.user == uid)
    if q.execute() == 0:
        q = Score.create(problem=pid, user=uid, points=score)

    return '<i class="icon-okay"></i>'
Example #14
0
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"))
Example #15
0
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'))
Example #16
0
def view(pid, uid=None):
    try:
        prob = Problem.get(Problem.id == pid)
    except Problem.DoesNotExist:
        flash('Problem %s does not exist' % pid)
        return redirect(url_for('index.html'))

    if uid is None:
        return render_template('problem/view.modal', prob=prob)

    teacher = auth.get_logged_in_user()
    if teacher.role != 'teacher':
        return render_template('problem/view.modal', prob=prob)

    student = StudentRecord(uid)
    score = student.scores.get(pid, None)

    return render_template('problem/view_grade.modal',
                           prob=prob,
                           teacher=teacher,
                           student=student,
                           score=score)
Example #17
0
def event_toggle_board(message, cid):
    user = StudentRecord(cid)
    if user.username:
        record = StudentRecord(user.id)
        record.open_board = not record.open_board
        record.save()

        all_records = StudentRecord.online_students()

        message_to_all = {}
        listening_clients = sse.listening_clients(cid)
        for c, r in all_records.items():
            m = dict(cid=cid, board_status=record.open_board)
            if record.open_board == False and int(cid) != int(c):
                if not r.is_teacher and c in listening_clients:
                    m.update(back_to_homeboard=True)
                    sse.listen_to(c, c)
            message_to_all[c] = m

        sse.notify(message_to_all, event="toggle-board")
Example #18
0
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')
Example #19
0
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)
Example #20
0
def admin():
    user = auth.get_logged_in_user()
    num_online = len(StudentRecord.online_students())
    if not user:
        return redirect(url_for('auth.login'))
    return render_template('admin.html', user=user, num_online=num_online)
Example #21
0
def online():
   students = StudentRecord.online_students()
   return render_template('user/online.html', students=students)
Example #22
0
def online():
    students = StudentRecord.online_students()
    return render_template('user/online.html', students=students)
Example #23
0
def admin():
   user = auth.get_logged_in_user()
   num_online = len(StudentRecord.online_students())
   if not user:
      return redirect(url_for('auth.login'))
   return render_template('admin.html', user=user, num_online=num_online)
Example #24
0
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')