def add_score_and_list_controller(args): new_score = create_entry() order = Score.score.desc() if args['sort'] == 'ascending': order = Score.score del args['user_id'] # we don't want to filter based on this args['end_date'] = datetime.datetime.utcnow( ) # so that the end date is after the new score's date if args['filter_tag'] not in (None, args['tag']): raise InvalidUsage( "filter_tag must be either null or the same as the new score's tag" ) if args['radius'] is None: raise InvalidUsage("radius is required") q = Score.query.order_by(order).filter(construct_and_(args)) q_results = q.all() new_score_index = (i for i, row in enumerate(q_results) if row.id == new_score.id).next() offset = max(0, new_score_index - args['radius']) return scores_from_query( result_set=q.offset(offset).limit(2 * args['radius'] + 1), args=args, endpoint='add_score_and_list', )
def leaderboards_controller(args): order = Score.score.desc() if args['sort'] == 'ascending': order = Score.score res_set = Score.query.order_by(order).filter(construct_and_(args)) return scores_from_query( result_set=res_set.offset(args['offset'] - 1).limit(args['page_size']), args=args, endpoint='leaderboards', count=res_set.count(), )
def add_score_and_list_controller(args): new_score = create_entry() order = Score.score.desc() if args['sort'] == 'ascending': order = Score.score del args['user_id'] # we don't want to filter based on this args['end_date'] = datetime.datetime.utcnow() # so that the end date is after the new score's date if args['filter_tag'] not in (None, args['tag']): raise InvalidUsage("filter_tag must be either null or the same as the new score's tag") if args['radius'] is None: raise InvalidUsage("radius is required") q = Score.query.order_by(order).filter(construct_and_(args)) q_results = q.all() new_score_index = (i for i, row in enumerate(q_results) if row.id == new_score.id).next() offset = max(0, new_score_index - args['radius']) return scores_from_query( result_set=q.offset(offset).limit(2 * args['radius'] + 1), args=args, endpoint='add_score_and_list', )