Coverage for bundesliga_tippspiel/routes/info.py: 98%
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
Shortcuts on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1"""LICENSE
2Copyright 2017 Hermann Krumrey <hermann@krumreyh.com>
4This file is part of bundesliga-tippspiel.
6bundesliga-tippspiel is free software: you can redistribute it and/or modify
7it under the terms of the GNU General Public License as published by
8the Free Software Foundation, either version 3 of the License, or
9(at your option) any later version.
11bundesliga-tippspiel is distributed in the hope that it will be useful,
12but WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14GNU General Public License for more details.
16You should have received a copy of the GNU General Public License
17along with bundesliga-tippspiel. If not, see <http://www.gnu.org/licenses/>.
18LICENSE"""
20from typing import List
21from jerrycan.base import db
22from flask import render_template, Blueprint, abort
23from flask_login import login_required, current_user
24from jerrycan.db.User import User
26from bundesliga_tippspiel.db import Team, Player, DisplayBotsSettings, \
27 Bet, Match, UserProfile
30def define_blueprint(blueprint_name: str) -> Blueprint:
31 """
32 Defines the blueprint for this route
33 :param blueprint_name: The name of the blueprint
34 :return: The blueprint
35 """
36 blueprint = Blueprint(blueprint_name, __name__)
38 @blueprint.route("/team/<string:team_abbreviation>")
39 @login_required
40 def team(team_abbreviation: int):
41 """
42 Displays information about a single team
43 :param team_abbreviation: The ID of the team to display
44 :return: The response
45 """
46 team_info = Team.query.filter_by(
47 abbreviation=team_abbreviation
48 ).options(db.joinedload(Team.players)
49 .subqueryload(Player.goals)).first()
50 if team_info is None:
51 return abort(404)
53 recent_matches = [x for x in team_info.matches if x.finished]
54 recent_matches.sort(key=lambda x: x.kickoff)
55 recent_matches = recent_matches[-7:]
57 match_data = []
58 for match_item in recent_matches:
59 if match_item.home_team_abbreviation == team_abbreviation:
60 opponent = match_item.away_team
61 own_score = match_item.home_current_score
62 opponent_score = match_item.away_current_score
63 else:
64 opponent = match_item.home_team
65 own_score = match_item.away_current_score
66 opponent_score = match_item.home_current_score
68 if own_score > opponent_score:
69 result = "win"
70 elif own_score < opponent_score:
71 result = "loss"
72 else:
73 result = "draw"
75 score = "{}:{}".format(own_score, opponent_score)
76 match_data.append((
77 match_item, opponent, score, result
78 ))
80 goal_data = []
81 for player in team_info.players:
82 goals = [x for x in player.goals if not x.own_goal]
83 goal_data.append((player.name, len(goals)))
84 goal_data.sort(key=lambda x: x[1], reverse=True)
86 return render_template(
87 "info/team.html",
88 team=team_info,
89 goals=goal_data,
90 matches=match_data
91 )
93 @blueprint.route("/match/<string:league>/<int:season>/"
94 "<int:matchday>/<string:matchup>",
95 methods=["GET"])
96 @login_required
97 def match(league: str, season: int, matchday: int, matchup: str):
98 """
99 Displays a single match
100 :param league: The league of the match
101 :param season: The season of the match
102 :param matchday: The matchday of the match
103 :param matchup: The matchup string ('hometeam_awayteam')
104 :return: The Response
105 """
106 try:
107 home, away = matchup.split("_")
108 match_item = Match.query.filter_by(
109 league=league,
110 season=season,
111 matchday=matchday,
112 home_team_abbreviation=home,
113 away_team_abbreviation=away
114 ).options(db.joinedload(Match.goals)).first()
115 if match_item is None:
116 raise ValueError()
117 except ValueError:
118 return abort(404)
120 bets: List[Bet] = Bet.query.filter_by(
121 league=league,
122 season=season,
123 matchday=matchday,
124 home_team_abbreviation=home,
125 away_team_abbreviation=away
126 ).options(db.joinedload(Bet.user)
127 .subqueryload(User.profile)
128 .subqueryload(UserProfile.favourite_team)).all()
129 if not DisplayBotsSettings.get_state(current_user): 129 ↛ 134line 129 didn't jump to line 134, because the condition on line 129 was never false
130 bets = [
131 x for x in bets
132 if DisplayBotsSettings.bot_symbol() not in x.user.username
133 ]
134 bets.sort(key=lambda x: x.user_id)
135 if match_item.has_started: 135 ↛ 138line 135 didn't jump to line 138, because the condition on line 135 was never false
136 bets.sort(key=lambda x: x.points, reverse=True)
138 return render_template(
139 "info/match.html",
140 match=match_item,
141 bets=bets
142 )
144 return blueprint