def round_by_id(round_id): score_round = Rounds.query.get_or_404(round_id) categories_shot = db.session.query(Scores.bow_type.distinct().label('bow_type'), Archers.gender.label('gender'), (Archers.gender + ' ' + BowTypes.name).label('div_name')).filter( Scores.round_id == round_id).join(Scores.archer).join(Scores.bow).order_by(db.desc(Scores.bow_type)).order_by( Archers.gender).all() categories = [] for cat in categories_shot: category = { 'bow_type': cat.bow_type, 'div_name': cat.div_name, 'scores': Scores.query.filter(Scores.round_id == round_id).filter(Scores.bow_type == cat.bow_type).filter( Archers.gender == cat.gender).join(Scores.archer).order_by(db.desc(Scores.score)).order_by( db.desc(Scores.num_hits)).order_by(db.desc(Scores.num_golds)).order_by(db.desc(Scores.num_xs)).all(), 'max_score': score_round.max_score } for score in category['scores']: classification = Classifications.query.get((score.round_id, score.bow_type, cat.gender)) score.classification = classification.get_class(score.score, score.round.r_type) if classification is not None else None categories.append(category) bow_types = BowTypes.query.order_by(db.desc(BowTypes.name)).all() return render_template('site/round.html', score_round=score_round, categories=categories, bow_types=bow_types)
def archer_by_id(archer_id): archer = Archers.query.get_or_404(archer_id) categories_shot = db.session.query(Scores.round_id.distinct().label('round_id'), Scores.bow_type.label('bow_type'), (Rounds.name + u' ' + BowTypes.name).label('div_name')).filter( Scores.archer_id == archer_id).join(Scores.bow).join(Scores.round).order_by('div_name').all() categories = [] for cat in categories_shot: category = { 'round_id': cat.round_id, 'bow_type': cat.bow_type, 'div_name': cat.div_name, 'scores': Scores.query.filter(Scores.archer_id == archer_id).filter(Scores.round_id == cat.round_id).filter( Scores.bow_type == cat.bow_type).order_by(db.desc(Scores.score)).order_by( db.desc(Scores.num_hits)).order_by(db.desc(Scores.num_golds)).order_by(db.desc(Scores.num_xs)).all(), 'max_score': Rounds.query.get(cat.round_id).max_score } for score in category['scores']: classification = Classifications.query.get((score.round_id, score.bow_type, archer.gender)) score.classification = classification.get_class(score.score, score.round.r_type) if classification is not None else None categories.append(category) bow_types = BowTypes.query.order_by(db.desc(BowTypes.name)).all() return render_template('site/archer.html', archer=archer, categories=categories, bow_types=bow_types)
def event_by_id(event_id): event = Events.query.get_or_404(event_id) categories_shot = db.session.query(Scores.date.distinct().label('date')).filter( Scores.event_id == event_id).order_by(db.desc(Scores.date)).all() categories = [] for cat in categories_shot: category = { 'date': cat.date } categories.append(category) bow_types = BowTypes.query.order_by(db.desc(BowTypes.name)).all() return render_template('site/event.html', event=event, categories=categories, bow_types=bow_types)
def get_missions_by_project_api_concurrent(project, api_name, concurrent): if concurrent.isdigit(): missions = Mission.query.filter_by(project=project, api_name=api_name, concurrent=concurrent, status=0).order_by( db.desc("id")).all() else: missions = Mission.query.filter_by(project=project, api_name=api_name, gradient=concurrent, status=0).order_by( db.desc("id")).all() return [i.to_json() for i in missions if i.has_report]
def search(): if request.form['search_data']: data = json.loads(request.form['search_data']) if not data['data']: return abort(400) else: s_type = data['data']['s_type'] s_id = data['data']['id'] if not s_type or not s_id: return abort(400) else: return redirect('/records/{}/{}'.format(s_type, s_id)) else: query = request.form['search'] if not query: return redirect('/records/400') archers = sorted( Archers.query.filter((Archers.first_name + u' ' + Archers.last_name).ilike(u'%{}%'.format(query))).all(), key=get_key, cmp=lambda x, y: lev.distance(x.lower(), query.lower()) - lev.distance(y.lower(), query.lower())) rounds = sorted(Rounds.query.filter(Rounds.name.ilike(u'%{}%'.format(query))).all(), key=get_key, cmp=lambda x, y: lev.distance(x.lower(), query.lower()) - lev.distance(y.lower(), query.lower())) events = sorted(Events.query.filter(Events.name.ilike(u'%{}%'.format(query))).all(), key=get_key, cmp=lambda x, y: lev.distance(x.lower(), query.lower()) - lev.distance(y.lower(), query.lower())) bow_types = BowTypes.query.order_by(db.desc(BowTypes.name)).all() return render_template('site/search-clarify.html', query=query, archers=archers, rounds=rounds, events=events, bow_types=bow_types)
def test_delete(self): """ DELETE /api/users/<pk> must be return status code 204 <No Content>. """ user = User.query.order_by(db.desc(User.fb_id)).first() url = self.base_url.format("/user/{}".format(user.id)) resp = requests.delete(url) self.assertEqual(204, resp.status_code)
def list_reviews(): page = get_int_or_abort(request.args.get("page"), 1) num = min(40, get_int_or_abort(request.args.get("n"), 100)) pagination = PackageReview.query.order_by(db.desc( PackageReview.created_at)).paginate(page, num, True) return render_template("packages/reviews_list.html", pagination=pagination, reviews=pagination.items)
def landing_page(): """ Gets top posts based on Karma for subreddits user is subscribed to """ top_posts = Post.query.join(User).join(Subreddit).order_by( db.desc(Post.karma)) print(top_posts) top_post_list = [post.to_joined_dict() for (post) in top_posts] return {'posts': top_post_list}
def archer_by_id(archer_id): archer = Archers.query.get_or_404(archer_id) categories_shot = ( db.session.query( Scores.round_id.distinct().label("round_id"), Scores.bow_type.label("bow_type"), (Rounds.name + u" " + BowTypes.name).label("div_name"), ) .filter(Scores.archer_id == archer_id) .join(Scores.bow) .join(Scores.round) .order_by("div_name") .all() ) categories = [] for cat in categories_shot: category = { "round_id": cat.round_id, "bow_type": cat.bow_type, "div_name": cat.div_name, "scores": Scores.query.filter(Scores.archer_id == archer_id) .filter(Scores.round_id == cat.round_id) .filter(Scores.bow_type == cat.bow_type) .order_by(db.desc(Scores.score)) .order_by(db.desc(Scores.num_hits)) .order_by(db.desc(Scores.num_golds)) .order_by(db.desc(Scores.num_xs)) .all(), "max_score": Rounds.query.get(cat.round_id).max_score, } for score in category["scores"]: classification = Classifications.query.get((score.round_id, score.bow_type, archer.gender)) score.classification = ( classification.get_class(score.score, score.round.r_type) if classification is not None else None ) categories.append(category) bow_types = BowTypes.query.order_by(db.desc(BowTypes.name)).all() return render_template("site/archer.html", archer=archer, categories=categories, bow_types=bow_types)
def sidebar_info(user): """ Gets top subreddits for user on sidebar """ top_subreddits = db.session.query( Subreddit, db.func.count(Subreddit.subscribers).label("number_of_subs")).join( Subreddit.subscribers).group_by(Subreddit.id).order_by( db.desc("number_of_subs")).limit(5).all() top_list = [subreddit.to_dict() for (subreddit, rank) in top_subreddits] return {'top_subreddits': top_list}
def event_by_id_date(event_id, event_date): event = Events.query.get_or_404(event_id) categories_shot = db.session.query(Scores.round_id.distinct().label('round_id'), Scores.bow_type.label('bow_type'), (Rounds.name + ' ' + BowTypes.name).label('div_name')).join(Scores.round).join( Scores.bow).filter(Scores.event_id == event_id).filter(Scores.date == event_date).order_by( db.desc(Scores.bow_type)).all() categories = [] for cat in categories_shot: category = { 'round_id': cat.round_id, 'bow_type': cat.bow_type, 'div_name': cat.div_name, 'scores': Scores.query.filter(Scores.round_id == cat.round_id).filter( Scores.bow_type == cat.bow_type).filter(Scores.event_id == event_id).filter(Scores.date == event_date) .order_by(db.desc(Scores.score)).order_by(db.desc(Scores.num_hits)).order_by( db.desc(Scores.num_golds)).order_by(db.desc(Scores.num_xs)).all(), 'max_score': Rounds.query.get(cat.round_id).max_score } for score in category['scores']: gender = Archers.query.get(score.archer_id).gender classification = Classifications.query.get((score.round_id, score.bow_type, gender)) score.classification = classification.get_class(score.score, score.round.r_type) if classification is not None else None categories.append(category) bow_types = BowTypes.query.order_by(db.desc(BowTypes.name)).all() return render_template('site/event-detail.html', event=event, date=event_date.strftime('%Y-%m-%d'), categories=categories, bow_types=bow_types)
def home(): indoor_categories_shot = db.session.query(IndividualRecords.bow_type.distinct().label('bow_type')).filter( db.or_(IndividualRecords.round_type == 'Indoors', IndividualRecords.round_type == 'WA Indoors')).order_by( db.desc(IndividualRecords.bow_type)).all() indoor_categories = [] for cat in indoor_categories_shot: category = { 'name': cat.bow_type, 'scores': IndividualRecords.query.filter(db.or_(IndividualRecords.round_type == 'Indoors', IndividualRecords.round_type == 'WA Indoors')).filter( IndividualRecords.bow_type == cat.bow_type).order_by(IndividualRecords.round_name, db.desc(IndividualRecords.category), db.desc(IndividualRecords.score), db.desc(IndividualRecords.num_golds)).all() } indoor_categories.append(category) outdoor_categories_shot = db.session.query( IndividualRecords.bow_type.distinct().label('bow_type')).filter( db.not_( db.or_(IndividualRecords.round_type == 'Indoors', IndividualRecords.round_type == 'WA Indoors'))).order_by( db.desc(IndividualRecords.bow_type)).all() outdoor_categories = [] for cat in outdoor_categories_shot: category = { 'name': cat.bow_type, 'scores': IndividualRecords.query.filter(db.not_(db.or_(IndividualRecords.round_type == 'Indoors', IndividualRecords.round_type == 'WA Indoors'))).filter( IndividualRecords.bow_type == cat.bow_type).order_by(IndividualRecords.round_name, db.desc(IndividualRecords.category), db.desc(IndividualRecords.score), db.desc(IndividualRecords.num_golds)).all() } outdoor_categories.append(category) bow_types = BowTypes.query.order_by(db.desc(BowTypes.name)).all() return render_template('site/search.html', bow_types=bow_types, indoor_categories=indoor_categories, outdoor_categories=outdoor_categories)
def home(): sort = request.args.get('sort', 'total') total_value_calc = db.func.coalesce( db.func.sum(BankerCurrency.amount * Currency.value), 0) sorted_standings_q = db.session.query( Banker, total_value_calc.label('total_value')).outerjoin( BankerCurrency, BankerCurrency.banker_id == Banker.id).outerjoin( Currency, Currency.id == BankerCurrency.currency_id).options( db.joinedload(Banker.currencies)).group_by(Banker.id) if sort == 'total': sorted_standings_q = sorted_standings_q.order_by( db.desc(total_value_calc), Banker.id) else: sort_by_currency_id = int(sort) sorting_subq = db.session.query( Banker.id.label('banker_id'), db.func.coalesce(BankerCurrency.amount, 0).label('sorted_column_amount')).outerjoin( BankerCurrency, db.and_( BankerCurrency.banker_id == Banker.id, BankerCurrency.currency_id == sort_by_currency_id)).subquery() sorted_standings_q = sorted_standings_q.join( sorting_subq, sorting_subq.c.banker_id == Banker.id).order_by( db.desc(db.func.max(sorting_subq.c.sorted_column_amount)), Banker.id) sorted_standings = sorted_standings_q.limit(20).all() return render_template('ct_conomy/home.html', sorted_standings=sorted_standings)
def advisors_mnactivity_feed(): try: recent_activity = db.session.query(MNActivity).order_by( db.desc(MNActivity.date_created)).limit(5).all() recent_activity = [activity.to_json() for activity in recent_activity] return jsonify({'mnActivity': recent_activity}) except: db.session.rollback() db.session.close() e = sys.exc_info()[0] v = sys.exc_info()[1] print(str(e)) print(str(v)) return jsonify({'error': str(e), 'value': str(v)}), Status.BAD
def advisors_recent_projects(): try: projects = db.session.query(Project).order_by( db.desc(Project.date_created)).limit(3).all() recent_projects = [project.to_json() for project in projects] return jsonify({'recentProjects': recent_projects}) except: db.session.rollback() db.session.close() e = sys.exc_info()[0] v = sys.exc_info()[1] print(str(e)) print(str(v)) return jsonify({'error': str(e), 'value': str(v)}), Status.BAD
def advisors_recent_updates(): try: updates = db.session.query(Update).order_by( db.desc(Update.date_created)).limit(5).all() recent_updates = [update.to_json() for update in updates] return jsonify({'recentUpdates': recent_updates}) except: db.session.rollback() db.session.close() e = sys.exc_info()[0] v = sys.exc_info()[1] print(str(e)) print(str(v)) return jsonify({'error': str(e), 'value': str(v)}), Status.BAD
def get_sorted_article_query(): """Get sorted query object of article. Judging by its sort_key [default: date] and order. Returns: object: Article.query (sorted) """ sort_key = request.args.get('s', 'date') order = request.args.get('o', 'desc') article_query = None if sort_key: if order == 'asc': article_query = Article.query.order_by(db.asc(sort_key)) else: article_query = Article.query.order_by(db.desc(sort_key)) else: article_query = Article.query return article_query
def audit(): page = get_int_or_abort(request.args.get("page"), 1) num = min(40, get_int_or_abort(request.args.get("n"), 100)) query = AuditLogEntry.query.order_by(db.desc(AuditLogEntry.created_at)) if "username" in request.args: user = User.query.filter_by( username=request.args.get("username")).first() if not user: abort(404) query = query.filter_by(causer=user) pagination = query.paginate(page, num, True) return render_template("admin/audit.html", log=pagination.items, pagination=pagination)
def event_by_id_date(event_id, event_date): event = Events.query.get_or_404(event_id) categories_shot = ( db.session.query( Scores.round_id.distinct().label("round_id"), Scores.bow_type.label("bow_type"), (Rounds.name + " " + BowTypes.name).label("div_name"), ) .join(Scores.round) .join(Scores.bow) .filter(Scores.event_id == event_id) .filter(Scores.date == event_date) .order_by(db.desc(Scores.bow_type)) .all() ) categories = [] for cat in categories_shot: category = { "round_id": cat.round_id, "bow_type": cat.bow_type, "div_name": cat.div_name, "scores": Scores.query.filter(Scores.round_id == cat.round_id) .filter(Scores.bow_type == cat.bow_type) .filter(Scores.event_id == event_id) .filter(Scores.date == event_date) .order_by(db.desc(Scores.score)) .order_by(db.desc(Scores.num_hits)) .order_by(db.desc(Scores.num_golds)) .order_by(db.desc(Scores.num_xs)) .all(), "max_score": Rounds.query.get(cat.round_id).max_score, } for score in category["scores"]: gender = Archers.query.get(score.archer_id).gender classification = Classifications.query.get((score.round_id, score.bow_type, gender)) score.classification = ( classification.get_class(score.score, score.round.r_type) if classification is not None else None ) categories.append(category) bow_types = BowTypes.query.order_by(db.desc(BowTypes.name)).all() return render_template( "site/event-detail.html", event=event, date=event_date.strftime("%Y-%m-%d"), categories=categories, bow_types=bow_types, )
def list_all_releases(): query = PackageRelease.query.filter_by(approved=True) \ .filter(PackageRelease.package.has(state=PackageState.APPROVED)) \ .order_by(db.desc(PackageRelease.releaseDate)) if "author" in request.args: author = User.query.filter_by(username=request.args["author"]).first() if author is None: error(404, "Author not found") query = query.filter(PackageRelease.package.has(author=author)) if "maintainer" in request.args: maintainer = User.query.filter_by( username=request.args["maintainer"]).first() if maintainer is None: error(404, "Maintainer not found") query = query.join(Package) query = query.filter(Package.maintainers.any(id=maintainer.id)) return jsonify( [rel.getLongAsDictionary() for rel in query.limit(30).all()])
def search(): if request.form["search_data"]: data = json.loads(request.form["search_data"]) if not data["data"]: return abort(400) else: s_type = data["data"]["s_type"] s_id = data["data"]["id"] if not s_type or not s_id: return abort(400) else: return redirect("/records/{}/{}".format(s_type, s_id)) else: query = request.form["search"] if not query: return redirect("/records/400") archers = sorted( Archers.query.filter((Archers.first_name + u" " + Archers.last_name).ilike(u"%{}%".format(query))).all(), key=get_key, cmp=lambda x, y: lev.distance(x.lower(), query.lower()) - lev.distance(y.lower(), query.lower()), ) rounds = sorted( Rounds.query.filter(Rounds.name.ilike(u"%{}%".format(query))).all(), key=get_key, cmp=lambda x, y: lev.distance(x.lower(), query.lower()) - lev.distance(y.lower(), query.lower()), ) events = sorted( Events.query.filter(Events.name.ilike(u"%{}%".format(query))).all(), key=get_key, cmp=lambda x, y: lev.distance(x.lower(), query.lower()) - lev.distance(y.lower(), query.lower()), ) bow_types = BowTypes.query.order_by(db.desc(BowTypes.name)).all() return render_template( "site/search-clarify.html", query=query, archers=archers, rounds=rounds, events=events, bow_types=bow_types )
def homepage(): query = Package.query.filter_by(state=PackageState.APPROVED) count = query.count() featured = query.filter(Package.tags.any(name="featured")).order_by( func.random()).limit(6).all() new = query.order_by(db.desc(Package.approved_at)).limit(4).all() pop_mod = query.filter_by(type=PackageType.MOD).order_by( db.desc(Package.score)).limit(8).all() pop_gam = query.filter_by(type=PackageType.GAME).order_by( db.desc(Package.score)).limit(8).all() pop_txp = query.filter_by(type=PackageType.TXP).order_by( db.desc(Package.score)).limit(8).all() high_reviewed = query.order_by(db.desc(Package.score - Package.score_downloads)) \ .filter(Package.reviews.any()).limit(4).all() updated = db.session.query(Package).select_from(PackageRelease).join(Package) \ .filter_by(state=PackageState.APPROVED) \ .order_by(db.desc(PackageRelease.releaseDate)) \ .limit(20).all() updated = updated[:4] downloads_result = db.session.query(func.sum( Package.downloads)).one_or_none() downloads = 0 if not downloads_result or not downloads_result[ 0] else downloads_result[0] def mapPackages(packages): return [pkg.getAsDictionaryKey() for pkg in packages] return { "count": count, "downloads": downloads, "featured": mapPackages(featured), "new": mapPackages(new), "updated": mapPackages(updated), "pop_mod": mapPackages(pop_mod), "pop_txp": mapPackages(pop_txp), "pop_game": mapPackages(pop_gam), "high_reviewed": mapPackages(high_reviewed) }
def list_reviews(): reviews = PackageReview.query.order_by(db.desc( PackageReview.created_at)).all() return render_template("packages/reviews_list.html", reviews=reviews)
def view_round(game_id, round_id): game = Game.query.options(db.joinedload(Game.teams)).get(game_id) game_round = Round.query.options(db.joinedload(Round.turns)).get(round_id) if game_round.completed_at: return redirect(url_for('salad_bowl.view_game', game_id=game_id)) for turn in game_round.turns: if turn.started_at and not turn.completed_at: return redirect( url_for('salad_bowl.view_turn', game_id=game_id, round_id=round_id, turn_id=turn.id)) turn_length = game_round.seconds_per_turn seconds_remaining = turn_length if not game_round.turns: # we are starting a new round if game_round.round_number == 1: # start of game, pick first team next_team = next( (team for team in game.teams if team.turn_order == 0)) else: # see who was going at the end of the previous round previous_turn_q = Turn.query previous_turn_q = previous_turn_q.join(Round) previous_turn_q = previous_turn_q.filter( Round.game_id == game_id, Round.round_number == game_round.round_number - 1) previous_turn_q = previous_turn_q.order_by(db.desc( Turn.started_at)) previous_turn = previous_turn_q.first() next_team = next((team for team in game.teams if team.id == previous_turn.team_id)) seconds_remaining = turn_length - int( (previous_turn.expected_complete_at - previous_turn.completed_at).total_seconds()) else: # weve already had teams go this round previous_turn = None turn_order_index = None for turn in sorted(game_round.turns, key=lambda x: x.started_at): previous_turn = turn for team in game.teams: if team.id == previous_turn.team_id: turn_order_index = (team.turn_order + 1) % len(game.teams) next_team = next((team for team in game.teams if team.turn_order == turn_order_index)) current_players_team_id_q = db.session.query(PlayerTeam.team_id) current_players_team_id_q = current_players_team_id_q.join(Team) current_players_team_id = current_players_team_id_q.filter( Team.game_id == game_id, PlayerTeam.player_id == g.current_player.id).scalar() can_start_next_turn = next_team.id == current_players_team_id unguessed_words_q = SaladBowlWord.query unguessed_words_q = unguessed_words_q.join( GuessedWord, db.and_(GuessedWord.round_id == round_id, GuessedWord.word_id == SaladBowlWord.id), isouter=True) unguessed_words_q = unguessed_words_q.filter( SaladBowlWord.game_id == game_id) unguessed_words_q = unguessed_words_q.filter( GuessedWord.round_id.is_(None)) unguessed_word_count = unguessed_words_q.count() return render_template('salad_bowl/view_round.html', game=game, game_round=game_round, next_team=next_team, seconds_remaining=seconds_remaining, unguessed_word_count=unguessed_word_count, can_start_next_turn=can_start_next_turn)
def rss(): articles = Article.query.order_by(db.desc('date')).limit(10) rss = render_template('rss.xml', articles=articles) response = make_response(rss) response.headers['Content-Type'] = 'application/xml' return response
def audit(): log = AuditLogEntry.query.order_by(db.desc(AuditLogEntry.created_at)).all() return render_template("admin/audit.html", log=log)
def home(): indoor_categories_shot = ( db.session.query(IndividualRecords.bow_type.distinct().label("bow_type")) .filter(db.or_(IndividualRecords.round_type == "Indoors", IndividualRecords.round_type == "WA Indoors")) .order_by(db.desc(IndividualRecords.bow_type)) .all() ) indoor_categories = [] for cat in indoor_categories_shot: category = { "name": cat.bow_type, "scores": IndividualRecords.query.filter( db.or_(IndividualRecords.round_type == "Indoors", IndividualRecords.round_type == "WA Indoors") ) .filter(IndividualRecords.bow_type == cat.bow_type) .order_by( IndividualRecords.round_name, db.desc(IndividualRecords.category), db.desc(IndividualRecords.score), db.desc(IndividualRecords.num_golds), ) .all(), } indoor_categories.append(category) outdoor_categories_shot = ( db.session.query(IndividualRecords.bow_type.distinct().label("bow_type")) .filter( db.not_(db.or_(IndividualRecords.round_type == "Indoors", IndividualRecords.round_type == "WA Indoors")) ) .order_by(db.desc(IndividualRecords.bow_type)) .all() ) outdoor_categories = [] for cat in outdoor_categories_shot: category = { "name": cat.bow_type, "scores": IndividualRecords.query.filter( db.not_(db.or_(IndividualRecords.round_type == "Indoors", IndividualRecords.round_type == "WA Indoors")) ) .filter(IndividualRecords.bow_type == cat.bow_type) .order_by( IndividualRecords.round_name, db.desc(IndividualRecords.category), db.desc(IndividualRecords.score), db.desc(IndividualRecords.num_golds), ) .all(), } outdoor_categories.append(category) bow_types = BowTypes.query.order_by(db.desc(BowTypes.name)).all() return render_template( "site/search.html", bow_types=bow_types, indoor_categories=indoor_categories, outdoor_categories=outdoor_categories, )
def get_all_missions(): logger.info("[get_all_missions]未命中缓存") missions = [ m.json_detail for m in Mission.query.order_by(db.desc("id")).all() ] return missions