Coverage for bundesliga_tippspiel/utils/matchday.py: 83%

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

42 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 Tuple, Optional 

21from flask import request 

22from datetime import datetime 

23from bundesliga_tippspiel.Config import Config 

24from bundesliga_tippspiel.db.match_data.Match import Match 

25 

26 

27def get_matchday_info(league: str, season: int) -> Tuple[int, int]: 

28 """ 

29 Retrieves information on matchdays 

30 :param league: The league for which to retrieve the information 

31 :param season: The season for which to retrieve the information 

32 :return: The current matchday as well as the maximum matchday 

33 """ 

34 all_matches = Match.query.filter_by( 

35 season=season, 

36 league=league 

37 ).all() 

38 max_matchday = max(all_matches, key=lambda x: x.matchday).matchday 

39 

40 started = [x for x in all_matches if x.has_started] 

41 if len(started) == 0: 41 ↛ 42line 41 didn't jump to line 42, because the condition on line 41 was never true

42 current_matchday = 1 

43 else: 

44 now = datetime.utcnow() 

45 latest_match: Match = max(started, key=lambda x: x.kickoff) 

46 latest_matchday_match: Match = max(started, key=lambda x: x.matchday) 

47 

48 if latest_matchday_match.matchday > latest_match.matchday + 1: 48 ↛ 50line 48 didn't jump to line 50, because the condition on line 48 was never true

49 # Show delayed matches for up to 2 matchdays 

50 latest_match = latest_matchday_match 

51 

52 current_matchday = latest_match.matchday 

53 if (now - latest_match.kickoff_datetime).days >= 1: 53 ↛ 56line 53 didn't jump to line 56, because the condition on line 53 was never false

54 current_matchday = min(max_matchday, current_matchday + 1) 

55 

56 return current_matchday, max_matchday 

57 

58 

59def validate_matchday( 

60 league: Optional[str], 

61 season: Optional[int], 

62 matchday: Optional[int] 

63) -> Optional[Tuple[str, int, int]]: 

64 """ 

65 Performs checks that a league/season/matchday combination is valid. 

66 Can also fill these values with default values 

67 :param league: The league to check 

68 :param season: The season to check 

69 :param matchday: The matchday to check 

70 :return: league, season, matchday if valid, None if invalid 

71 """ 

72 try: 

73 default_league, default_season = get_selected_league() 

74 except RuntimeError: 

75 default_league, default_season = \ 

76 Config.OPENLIGADB_LEAGUE, Config.season() 

77 

78 if league is None: 

79 league = default_league 

80 if season is None: 

81 season = default_season 

82 current_matchday, max_matchday = get_matchday_info(league, season) 

83 if matchday is None: 

84 matchday = current_matchday 

85 

86 if not 1 <= matchday <= max_matchday: 86 ↛ 87line 86 didn't jump to line 87, because the condition on line 86 was never true

87 return None 

88 else: 

89 return league, season, matchday 

90 

91 

92def get_selected_league() -> Tuple[str, int]: 

93 """ 

94 :return: The league currently selected by the user by means of cookies 

95 """ 

96 league = str(request.cookies.get("league", Config.OPENLIGADB_LEAGUE)) 

97 try: 

98 season = int(request.cookies.get("season", Config.season())) 

99 except ValueError: 

100 season = Config.season() 

101 return league, season