Coverage for bundesliga_tippspiel/Config.py: 94%
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"""
20import os
21from typing import Type, List, Dict, Optional, Tuple
22from jerrycan.Config import Config as BaseConfig
25class Config(BaseConfig):
26 """
27 Configuration for the flask application
28 """
29 OPENLIGADB_SEASON: str
30 """
31 The default openligadb season to use
32 """
34 OPENLIGADB_LEAGUE: str
35 """
36 The default openligadb league to use
37 """
39 OPENLIGADB_EXTRA_LEAGUES: List[Tuple[str, int]]
40 """
41 Extra openligadb seasons and leagues
42 """
44 @classmethod
45 def league_name(cls, league: Optional[str] = None) -> str:
46 """
47 Generates a name for the openligadb league
48 :param league: The league string (like 'bl1')
49 :return: The league name (like 'Bundesliga')
50 """
51 if league is None: 51 ↛ 52line 51 didn't jump to line 52, because the condition on line 51 was never true
52 league = cls.OPENLIGADB_LEAGUE
53 return {
54 "bl1": "Bundesliga",
55 "bl2": "2. Bundesliga",
56 "bl3": "3. Liga"
57 }.get(league, league)
59 @classmethod
60 def season_string(cls, year: Optional[int] = None) -> str:
61 """
62 :return: The season string, e.g. 2020/21 for 2020
63 """
64 if year is None:
65 year = int(cls.OPENLIGADB_SEASON)
66 second = str(year + 1)[-2:]
67 return f"{year}/{second}"
69 @classmethod
70 def league_string(cls, league: str, season: int) -> str:
71 """
72 Creates a league string, like "Bundesliga 2020/21"
73 :param league: The league for which to generate the string
74 :param season: The season for which to generate the string
75 :return: The generated string
76 """
77 season_string = cls.season_string(season)
78 league_name = cls.league_name(league)
79 return f"{league_name} {season_string}"
81 @classmethod
82 def season(cls) -> int:
83 """
84 :return: The current season
85 """
86 return int(Config.OPENLIGADB_SEASON)
88 @classmethod
89 def all_leagues(cls) -> List[Tuple[str, int]]:
90 """
91 :return: A list of all leagues
92 """
93 leagues = [(cls.OPENLIGADB_LEAGUE, cls.season())] + \
94 cls.OPENLIGADB_EXTRA_LEAGUES
95 leagues.sort(key=lambda x: x[0])
96 leagues.sort(key=lambda x: x[1])
97 return leagues
99 @classmethod
100 def environment_variables(cls) -> Dict[str, List[str]]:
101 """
102 Specifies required and optional environment variables
103 :return: The specified environment variables in two lists in
104 a dictionary, grouped by whether the variables are
105 required or optional
106 """
107 base = super().environment_variables()
108 base["optional"] += [
109 "OPENLIGADB_LEAGUE",
110 "OPENLIGADB_SEASON",
111 "OPENLIGADB_EXTRA_LEAGUES"
112 ]
113 return base
115 @classmethod
116 def _load_extras(cls, parent: Type[BaseConfig]):
117 """
118 Loads non-standard configuration variables
119 :param parent: The base configuration
120 :return: None
121 """
122 Config.OPENLIGADB_SEASON = os.environ.get("OPENLIGADB_SEASON", "2021")
123 Config.OPENLIGADB_LEAGUE = os.environ.get("OPENLIGADB_LEAGUE", "bl1")
124 Config.OPENLIGADB_EXTRA_LEAGUES = []
125 openligdb_extras = \
126 os.environ.get("OPENLIGADB_EXTRA_LEAGUES", "").strip().split(",")
127 for entry in openligdb_extras:
128 try:
129 league, _season = entry.split(":")
130 season = int(_season)
131 Config.OPENLIGADB_EXTRA_LEAGUES.append((league, season))
132 except ValueError:
133 pass
134 from bundesliga_tippspiel.template_extras import profile_extras
135 parent.API_VERSION = "3"
136 parent.STRINGS.update({
137 "401_message": "Du bist nicht angemeldet. Bitte melde dich an.",
138 "500_message": "The server encountered an internal error and "
139 "was unable to complete your request. "
140 "Either the server is overloaded or there "
141 "is an error in the application.",
142 "user_does_not_exist": "Dieser Nutzer existiert nicht",
143 "user_already_logged_in": "Du bist bereits angemeldet.",
144 "user_already_confirmed": "Dieser Nutzer ist bereits "
145 "bestätigt worden.",
146 "user_is_not_confirmed": "Dieser Nutzer wurde "
147 "noch nicht bestätigt",
148 "invalid_password": "Das angegebene Passwort ist inkorrekt.",
149 "logged_in": "Du hast dich erfolgreich angemeldet",
150 "logged_out": "Erfolgreich ausgeloggt",
151 "username_length": "Username zu lang: Der Username muss zwischen "
152 "{} und {} Zeichen lang sein.",
153 "passwords_do_not_match": "Die angegebenen Passwörter stimmen "
154 "nicht miteinander überein.",
155 "email_already_in_use": "Die gewählte Email-Address wird bereits "
156 "verwendet.",
157 "username_already_exists": "Der ausgewählte Username existiert "
158 "bereits.",
159 "recaptcha_incorrect": "Bitte löse das ReCaptcha.",
160 "registration_successful": "Siehe in deiner Email-Inbox nach, "
161 "um die Registrierung abzuschließen.",
162 "registration_email_title": "Tippspiel Registrierung",
163 "confirmation_key_invalid": "Der angegebene Bestätigungsschlüssel "
164 "ist inkorrekt.",
165 "user_confirmed_successfully": "Benutzer wurde erfolgreich "
166 "registriert. "
167 "Du kannst dich jetzt anmelden.",
168 "password_reset_email_title": "Password Zurücksetzen",
169 "password_was_reset": "Passwort erfolgreich zurückgesetzt. "
170 "Sehe in deinem Email-Postfach nach.",
171 "password_changed": "Dein Passwort wurde erfolgreich geändert.",
172 "user_was_deleted": "Dein Account wurde erfolgreich gelöscht",
173 "telegram_chat_id_set": "Telegram Chat ID wurde erfolgreich "
174 "gesetzt"
175 })
176 parent.TEMPLATE_EXTRAS.update({
177 "profile": profile_extras
178 })