Coverage for bundesliga_tippspiel/routes/tables.py: 59%

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

35 statements  

1"""LICENSE 

2Copyright 2017 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of bundesliga-tippspiel. 

5 

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. 

10 

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. 

15 

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""" 

19 

20from typing import Optional 

21from flask import render_template, Blueprint, request, abort 

22from flask_login import login_required, current_user 

23from bundesliga_tippspiel.db import DisplayBotsSettings 

24from bundesliga_tippspiel.utils.collections.Leaderboard import Leaderboard 

25from bundesliga_tippspiel.utils.collections.LeagueTable import LeagueTable 

26from bundesliga_tippspiel.utils.matchday import validate_matchday 

27 

28 

29def define_blueprint(blueprint_name: str) -> Blueprint: 

30 """ 

31 Defines the blueprint for this route 

32 :param blueprint_name: The name of the blueprint 

33 :return: The blueprint 

34 """ 

35 blueprint = Blueprint(blueprint_name, __name__) 

36 

37 @blueprint.route("/leaderboard", methods=["GET"]) 

38 @blueprint.route( 

39 "/leaderboard/<string:league>/<int:season>/<int:matchday>", 

40 methods=["GET"] 

41 ) 

42 @login_required 

43 def leaderboard( 

44 league: Optional[str] = None, 

45 season: Optional[int] = None, 

46 matchday: Optional[int] = None 

47 ): 

48 """ 

49 Displays a leaderboard. 

50 :param league: The league for which to display the leaderboard 

51 :param season: The season for which to display the leaderboard 

52 :param matchday: The matchday for which to display the leaderboard 

53 :return: The Response 

54 """ 

55 validated = validate_matchday(league, season, matchday) 

56 if validated is None: 56 ↛ 57line 56 didn't jump to line 57, because the condition on line 56 was never true

57 return abort(404) 

58 league, season, matchday = validated 

59 

60 matchday_leaderboard = Leaderboard( 

61 league, 

62 season, 

63 matchday, 

64 DisplayBotsSettings.get_state(current_user) 

65 ) 

66 

67 return render_template( 

68 "ranking/leaderboard.html", 

69 leaderboard=matchday_leaderboard 

70 ) 

71 

72 @blueprint.route("/league_table", methods=["GET"]) 

73 @blueprint.route("/league_table/<string:league>/<int:season>/" 

74 "<int:matchday>", methods=["GET"]) 

75 @login_required 

76 def league_table( 

77 league: Optional[str] = None, 

78 season: Optional[int] = None, 

79 matchday: Optional[int] = None 

80 ): 

81 """ 

82 Calculates the current league table and displays it for the user. 

83 Can also show a league table based on a user's bets if the 

84 GET parameter 'use_bets' is set to 1 

85 :param league: The league to display 

86 :param season: The season to display 

87 :param matchday: The matchday to display 

88 :return: The response 

89 """ 

90 validated = validate_matchday(league, season, matchday) 

91 if validated is None: 

92 return abort(404) 

93 league, season, matchday = validated 

94 

95 user = None 

96 title = "Aktuelle Ligatabelle" 

97 if request.args.get("use_bets") == "1": 

98 user = current_user 

99 title = f"Tabelle nach {current_user.username}'s Tipps" 

100 

101 table = LeagueTable(league, season, matchday, user) 

102 return render_template( 

103 "info/league_table.html", 

104 league_table=table, 

105 title=title 

106 ) 

107 

108 return blueprint