Coverage for bundesliga_tippspiel/routes/settings.py: 54%

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

62 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 flask import Blueprint, request, flash, redirect, url_for, \ 

21 make_response, abort 

22from flask_login import login_required, current_user 

23from jerrycan.base import db, app 

24from jerrycan.enums import AlertSeverity 

25from bundesliga_tippspiel.Config import Config 

26from bundesliga_tippspiel.db import Team, UserProfile 

27from bundesliga_tippspiel.db.settings.DisplayBotsSettings import \ 

28 DisplayBotsSettings 

29from bundesliga_tippspiel.db.settings.ReminderSettings import \ 

30 ReminderSettings 

31from bundesliga_tippspiel.enums import ReminderType 

32 

33 

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

35 """ 

36 Defines the blueprint for this route 

37 :param blueprint_name: The name of the blueprint 

38 :return: The blueprint 

39 """ 

40 blueprint = Blueprint(blueprint_name, __name__) 

41 

42 @blueprint.route("/misc_settings", methods=["POST"]) 

43 @login_required 

44 def misc_settings(): 

45 """ 

46 Allows the user to change their miscellaneous settings 

47 :return: The response 

48 """ 

49 setting = DisplayBotsSettings( 

50 user_id=current_user.id, 

51 display_bots=request.form.get("display_bots", "off") == "on" 

52 ) 

53 db.session.merge(setting) 

54 db.session.commit() 

55 

56 flash("Einstellungen gespeichert", AlertSeverity.SUCCESS.value) 

57 return redirect(url_for("user_management.profile")) 

58 

59 @blueprint.route("/set_reminder", methods=["POST"]) 

60 @login_required 

61 def set_reminder(): 

62 """ 

63 Allows the user to set an email reminder 

64 :return: The response 

65 """ 

66 hours = int(request.form["hours"]) 

67 reminder_states = { 

68 reminder_type: 

69 request.form.get(reminder_type.value) in ["on", True] 

70 for reminder_type in ReminderType 

71 } 

72 

73 if not 0 < hours < 49: 

74 flash("Ungültige Anzahl Stunden eingegeben", "danger") 

75 else: 

76 for reminder_type, reminder_state in reminder_states.items(): 

77 setting = ReminderSettings( 

78 user_id=current_user.id, 

79 reminder_type=reminder_type, 

80 active=reminder_state, 

81 reminder_time=hours 

82 ) 

83 db.session.merge(setting) 

84 db.session.commit() 

85 flash("Erinnerungseinstellungen gespeichert", "success") 

86 

87 return redirect(url_for("user_management.profile")) 

88 

89 @blueprint.route("/change_league", methods=["GET"]) 

90 @login_required 

91 def change_league(): 

92 """ 

93 Changes the user's currently displayed league by storing these 

94 values in a cookie 

95 :return: None 

96 """ 

97 try: 

98 league = request.args.get("league", Config.OPENLIGADB_LEAGUE) 

99 season = request.args.get("season", Config.OPENLIGADB_SEASON) 

100 int(season) 

101 except ValueError: 

102 return abort(400) 

103 app.logger.info(request.referrer) 

104 response = make_response(redirect(url_for("betting.get_current_bets"))) 

105 response.set_cookie("league", league) 

106 response.set_cookie("season", season) 

107 return response 

108 

109 @blueprint.route("/set_profile_info", methods=["POST"]) 

110 @login_required 

111 def set_profile_info(): 

112 """ 

113 Sets the profile info for a user 

114 :return: The response 

115 """ 

116 team_names = [x.abbreviation for x in Team.query.all()] 

117 

118 description = request.form.get("about_me") 

119 favourite_team = request.form.get("favourite_team") 

120 if not description: 

121 description = None 

122 if favourite_team not in team_names: 

123 favourite_team = None 

124 country = None 

125 

126 profile_info = UserProfile( 

127 user_id=current_user.id, 

128 description=description, 

129 favourite_team_abbreviation=favourite_team, 

130 country=country 

131 ) 

132 db.session.merge(profile_info) 

133 db.session.commit() 

134 return redirect(url_for("user_management.profile")) 

135 

136 return blueprint