Coverage for bundesliga_tippspiel/db/match_data/Team.py: 100%

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

33 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 List, TYPE_CHECKING 

21 

22from flask import url_for 

23from jerrycan.base import db 

24from jerrycan.db.ModelMixin import ModelMixin 

25from bundesliga_tippspiel.db.match_data.Match import Match 

26if TYPE_CHECKING: # pragma: no cover 

27 from bundesliga_tippspiel.db.match_data.Player import Player 

28 

29 

30class Team(ModelMixin, db.Model): 

31 """ 

32 Model that describes the 'teams' SQL table 

33 A Team is the most basic data for a match, it relies on no other data, 

34 only primitives 

35 """ 

36 

37 def __init__(self, *args, **kwargs): 

38 """ 

39 Initializes the Model 

40 :param args: The constructor arguments 

41 :param kwargs: The constructor keyword arguments 

42 """ 

43 super().__init__(*args, **kwargs) 

44 

45 __tablename__ = "teams" 

46 

47 abbreviation: str = db.Column(db.String(3), primary_key=True) 

48 name: str = db.Column(db.String(50), nullable=False, unique=True) 

49 short_name: str = db.Column(db.String(16), nullable=False, unique=True) 

50 icon_svg: str = db.Column(db.String(255), nullable=False) 

51 icon_png: str = db.Column(db.String(255), nullable=False) 

52 

53 players: List["Player"] = db.relationship("Player", cascade="all, delete") 

54 

55 @property 

56 def home_matches(self) -> List[Match]: 

57 """ 

58 :return: A list of home matches for the team 

59 """ 

60 return Match.query.filter_by( 

61 home_team_abbreviation=self.abbreviation 

62 ).all() 

63 

64 @property 

65 def away_matches(self) -> List[Match]: 

66 """ 

67 :return: A list of away matches for the team 

68 """ 

69 return Match.query.filter_by( 

70 away_team_abbreviation=self.abbreviation 

71 ).all() 

72 

73 @property 

74 def matches(self) -> List[Match]: 

75 """ 

76 :return: A list of matches for the team 

77 """ 

78 return self.home_matches + self.away_matches 

79 

80 @property 

81 def url(self) -> str: 

82 """ 

83 :return: The URL for this teams's info page 

84 """ 

85 return url_for("info.team", team_abbreviation=self.abbreviation) 

86 

87 @classmethod 

88 def get_teams_for_season(cls, league: str, season: int) -> List["Team"]: 

89 """ 

90 Retrieves a list of all teams in a particular season 

91 :param league: The league in which to search for teams 

92 :param season: The season in which to search for teams 

93 :return: The list of teams 

94 """ 

95 

96 match_samples = Match.query.filter_by( 

97 league=league, season=season, matchday=1 

98 ).all() 

99 home_teams = [x.home_team for x in match_samples] 

100 away_teams = [x.away_team for x in match_samples] 

101 return home_teams + away_teams