def leaderboard(): user = authenticate() form = PageForm(params()) if not form.validate(): return errors(form.errors) total = User.query.count() pager = Pager(total=total, **form.data) users = User.query.order_by(User.username).slice(*pager.slice) user_map = {} for user in users: user_map[user.id] = user for user_id, count in db.session.query(Attempt.user_id, db.func.count(Attempt.user_id)).group_by(Attempt.user_id).filter_by(successful=True).all(): user_map[user_id].score = count return jsonify(leaderboard_view(pager, users))
def leaderboard(): user = authenticate() form = PageForm(params()) if not form.validate(): return errors(form.errors) total = User.query.count() pager = Pager(total=total, **form.data) users = User.query.order_by(User.username).slice(*pager.slice) user_map = {} for user in users: user_map[user.id] = user for user_id, count in db.session.query(Attempt.user_id, db.func.count(Attempt.user_id)).group_by(Attempt.user_id).filter_by(successful=True).all(): if user_id in user_map: user_map[user_id].score = count return jsonify(leaderboard_view(pager, users))
def riddles(): user = authenticate() total = Riddle.query.count() form = PageForm(params()) if not form.validate(): return errors(form.errors) pager = Pager(total=total, **form.data) riddles_map = {} riddles = Riddle.query.order_by(Riddle.created_at).all() for riddle in riddles: riddles_map[riddle.id] = riddle for (id, ) in db.session.query(Attempt.riddle_id).filter_by(successful=True).filter_by(user_id=user.id).all(): riddles_map[id].solved = True for id, attempted_by in db.session.query(Attempt.riddle_id, func.count(Attempt.riddle_id)).group_by(Attempt.riddle_id).all(): riddles_map[id].attempted_by = attempted_by for id, solved_by in db.session.query(Attempt.riddle_id, func.count(Attempt.riddle_id)).group_by(Attempt.riddle_id).filter_by(successful=True).all(): riddles_map[id].solved_by = solved_by return jsonify(riddles_listing_view(pager, riddles))