Coverage for otaku_info/routes/notifications.py: 40%
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 2020 Hermann Krumrey <hermann@krumreyh.com>
4This file is part of otaku-info.
6otaku-info 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.
11otaku-info 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 otaku-info. If not, see <http://www.gnu.org/licenses/>.
18LICENSE"""
20from flask import request, render_template, redirect, url_for
21from flask.blueprints import Blueprint
22from flask_login import current_user, login_required
23from jerrycan.base import db
24from jerrycan.db.TelegramChatId import TelegramChatId
25from otaku_info.enums import NotificationType
26from otaku_info.db.NotificationSetting import NotificationSetting
29def define_blueprint(blueprint_name: str) -> Blueprint:
30 """
31 Defines a Blueprint that handles notifications for users
32 :param blueprint_name: The name of the blueprint
33 :return: The blueprint
34 """
35 blueprint = Blueprint(blueprint_name, __name__)
37 @blueprint.route("/notifications", methods=["GET"])
38 @login_required
39 def notifications():
40 """
41 Displays the notification settings page
42 :return: The response
43 """
44 telegram_chat_id = \
45 TelegramChatId.query.filter_by(user=current_user).first()
46 settings = {
47 x: False
48 for x in NotificationType
49 }
50 settings.update({
51 x.notification_type: x
52 for x in
53 NotificationSetting.query.filter_by(user_id=current_user.id).all()
54 })
55 return render_template(
56 "user_management/notifications.html",
57 telegram_chat_id=telegram_chat_id,
58 notification_settings=settings
59 )
61 @blueprint.route("/set_notification_settings", methods=["POST"])
62 @login_required
63 def set_notification_settings():
64 """
65 Sets the notification settings
66 :return: Redirect to notifications page
67 """
68 for notification_type in NotificationType:
69 active_input = request.form.get(notification_type.value, "off")
70 active_value = active_input == "on"
72 min_score_text = request.form.get(
73 notification_type.value + "_min_score",
74 "0"
75 )
76 try:
77 min_score = int(min_score_text)
78 except IndexError:
79 min_score = 0
81 setting = NotificationSetting(
82 user_id=current_user.id,
83 notification_type=notification_type,
84 minimum_score=min_score,
85 value=active_value
86 )
87 setting = db.session.merge(setting)
89 setting.value = active_value
90 setting.minimum_score = min_score
91 db.session.commit()
93 return redirect(url_for("notifications.notifications"))
95 return blueprint