Coverage for bundesliga_tippspiel/routes/api/info.py: 48%
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"""
19from typing import Optional
21from flask import Blueprint, request, abort
22from flask_login import login_required
23from jerrycan.routes.decorators import api, api_login_required
24from bundesliga_tippspiel.Config import Config
25from bundesliga_tippspiel.db import Match
26from bundesliga_tippspiel.utils.collections.LeagueTable import LeagueTable
27from bundesliga_tippspiel.utils.matchday import validate_matchday, \
28 get_matchday_info
31def define_blueprint(blueprint_name: str) -> Blueprint:
32 """
33 Defines the blueprint for this route
34 :param blueprint_name: The name of the blueprint
35 :return: The blueprint
36 """
37 blueprint = Blueprint(blueprint_name, __name__)
38 api_base_path = f"/api/v{Config.API_VERSION}"
40 @blueprint.route(f"{api_base_path}/leagues", methods=["GET"])
41 @api
42 def api_get_leagues():
43 """
44 Retrieves a list of available leagues
45 :return: The list of available leagues
46 """
47 return {"leagues": Config.all_leagues()}
49 @blueprint.route(f"{api_base_path}/matchday_info", methods=["GET"])
50 @api
51 def api_matchday_info():
52 """
53 Retrieves the current matchday and the maximum matchday for a league
54 Requires a "league" and "season" parameter in the query
55 :return: The list of available leagues
56 """
57 current, max_matchday = get_matchday_info(
58 request.args["league"], int(request.args["season"])
59 )
60 return {
61 "current": current,
62 "max": max_matchday
63 }
65 @blueprint.route(f"{api_base_path}/matchday/<string:league>/<int:season>",
66 methods=["GET"])
67 @blueprint.route(f"{api_base_path}/matchday/"
68 f"<string:league>/<int:season>/<int:matchday>",
69 methods=["GET"])
70 @api_login_required
71 @login_required
72 @api
73 def api_matches(
74 league: str,
75 season: int,
76 matchday: Optional[int] = None
77 ):
78 """
79 Retrieves the matches for a particular matchday
80 :return: The list of available leagues
81 """
82 validated = validate_matchday(league, season, matchday)
83 if validated is None:
84 return abort(404)
85 league, season, matchday = validated
87 matches = Match.query.filter_by(
88 league=league, season=season, matchday=matchday
89 ).all()
90 matches.sort(key=lambda x: x.kickoff)
91 matches_json = [
92 match.__json__(include_children=False) for match in matches
93 ]
94 return {"matches": matches_json}
96 @blueprint.route(f"{api_base_path}/league_table/"
97 f"<string:league>/<int:season>", methods=["GET"])
98 @blueprint.route(f"{api_base_path}/league_table/"
99 f"<string:league>/<int:season>/<int:matchday>",
100 methods=["GET"])
101 @api
102 def api_get_league_table(
103 league: str,
104 season: int,
105 matchday: Optional[int] = None
106 ):
107 """
108 Retrieves a league table
109 :param league: The league for which to retrieve the table
110 :param season: The season for which to retrieve the table
111 :param matchday: The matchday for which to retrieve the table
112 :return: The leaderboard table
113 """
114 validated = validate_matchday(league, season, matchday)
115 if validated is None:
116 return abort(404)
117 league, season, matchday = validated
119 table_data = LeagueTable(league, season, matchday, None)\
120 .calculate_table()
121 league_table = []
122 for entry in table_data:
123 league_table.append((
124 entry[0],
125 entry[1].__json__(),
126 entry[2],
127 entry[3],
128 entry[4],
129 entry[5],
130 entry[6],
131 entry[7],
132 entry[8],
133 entry[9]
134 ))
136 return {
137 "league_table": league_table
138 }
140 return blueprint