Hot-keys 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-web.
6otaku-info-web 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-web 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-web. 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 puffotter.flask.base import db
24from otaku_info_web.db.TelegramChatId import TelegramChatId
25from otaku_info_web.utils.enums import NotificationType
26from otaku_info_web.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.value
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_telegram_chat_id", methods=["POST"])
62 @login_required
63 def set_telegram_chat_id():
64 """
65 Sets the Telegram chat ID
66 :return: Redirect to notifications page
67 """
68 chat_id = request.form["telegram_chat_id"]
69 db_chat_id = TelegramChatId.query.filter_by(user=current_user).first()
71 if db_chat_id is None:
72 db_chat_id = TelegramChatId(
73 user=current_user,
74 chat_id=chat_id
75 )
76 db.session.add(db_chat_id)
77 else:
78 db_chat_id.chat_id = chat_id
80 db.session.commit()
81 return redirect(url_for("notifications.notifications"))
83 @blueprint.route(f"/telegram_test", methods=["GET"])
84 @login_required
85 def telegram_test():
86 """
87 Can be used to test if telegram messages are sent out correctly
88 :return: Redirect to the notifications page
89 """
90 db_chat_id = TelegramChatId.query.filter_by(user=current_user).first()
91 db_chat_id.send_message("Test")
92 return redirect(url_for("notifications.notifications"))
94 @blueprint.route("/set_notification_settings", methods=["POST"])
95 @login_required
96 def set_notification_settings():
97 """
98 Sets the notification settings
99 :return: Redirect to notifications page
100 """
101 existing_settings = {
102 x.notification_type: x
103 for x in
104 NotificationSetting.query.filter_by(user_id=current_user.id).all()
105 }
106 for notification_type in NotificationType:
107 form_data = request.form.get(notification_type.value, "off")
108 form_value = form_data == "on"
110 setting = existing_settings.get(notification_type)
111 if setting is None:
112 setting = NotificationSetting(
113 user=current_user, notification_type=notification_type
114 )
115 db.session.add(setting)
117 setting.value = form_value
118 db.session.commit()
120 return redirect(url_for("notifications.notifications"))
122 return blueprint