Coverage for bundesliga_tippspiel/routes/stats.py: 90%

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

36 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, abort, Blueprint 

22from flask_login import login_required, current_user 

23from jerrycan.db.User import User 

24from bundesliga_tippspiel.db import DisplayBotsSettings 

25from bundesliga_tippspiel.utils.matchday import validate_matchday 

26from bundesliga_tippspiel.utils.collections.StatsGenerator import \ 

27 StatsGenerator 

28from bundesliga_tippspiel.utils.collections.UserStatsGenerator import \ 

29 UserStatsGenerator 

30 

31 

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

33 """ 

34 Defines the blueprint for this route 

35 :param blueprint_name: The name of the blueprint 

36 :return: The blueprint 

37 """ 

38 blueprint = Blueprint(blueprint_name, __name__) 

39 

40 @blueprint.route("/user/<int:user_id>") 

41 @blueprint.route("/user/<int:user_id>/" 

42 "<string:league>/<int:season>/<int:matchday>") 

43 @login_required 

44 def user( 

45 user_id: int, 

46 league: Optional[str] = None, 

47 season: Optional[int] = None, 

48 matchday: Optional[int] = None 

49 ): 

50 """ 

51 Shows a page describing a user + statistics 

52 :param league: The league to display 

53 :param season: The season to display 

54 :param matchday: The matchday to display 

55 :param user_id: The ID of the user to display 

56 :return: The request response 

57 """ 

58 validated = validate_matchday(league, season, matchday) 

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

60 return abort(404) 

61 league, season, matchday = validated 

62 

63 user_data = User.query.get(user_id) 

64 if user_data is None: 

65 return abort(404) 

66 show_bots = DisplayBotsSettings.get_state(current_user) or \ 

67 DisplayBotsSettings.bot_symbol() in user_data.username 

68 

69 user_stats = UserStatsGenerator( 

70 league, season, matchday, user_data, show_bots 

71 ) 

72 

73 return render_template( 

74 "stats/user.html", 

75 user=user_data, 

76 user_stats=user_stats 

77 ) 

78 

79 @blueprint.route("/stats", methods=["GET"]) 

80 @blueprint.route("/stats/" 

81 "<string:league>/<int:season>/<int:matchday>") 

82 @login_required 

83 def stats( 

84 league: Optional[str] = None, 

85 season: Optional[int] = None, 

86 matchday: Optional[int] = None, 

87 ): 

88 """ 

89 Displays various global statistics 

90 :param league: The league to display 

91 :param season: The season to display 

92 :param matchday: The matchday to display 

93 :return: None 

94 """ 

95 validated = validate_matchday(league, season, matchday) 

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

97 return abort(404) 

98 league, season, matchday = validated 

99 

100 show_bots = DisplayBotsSettings.get_state(current_user) 

101 global_stats = StatsGenerator(league, season, matchday, show_bots) 

102 

103 return render_template( 

104 "stats/stats.html", 

105 stats=global_stats 

106 ) 

107 

108 return blueprint