From 8f5f64ad33a092ca3c7a5b71003ad2d9bb3dd7f0 Mon Sep 17 00:00:00 2001 From: Devin Matte Date: Thu, 15 Aug 2019 14:46:07 -0400 Subject: [PATCH 1/5] Phase 2: Notification on signing --- config.env.py | 1 + packet/__init__.py | 6 ++++++ packet/notifications.py | 29 +++++++++++++++++++++++++++++ packet/routes/api.py | 4 ++++ 4 files changed, 40 insertions(+) create mode 100644 packet/notifications.py diff --git a/config.env.py b/config.env.py index 0eb3f4e6..786fe437 100644 --- a/config.env.py +++ b/config.env.py @@ -9,6 +9,7 @@ DEBUG = False IP = environ.get("PACKET_IP", "localhost") PORT = environ.get("PACKET_PORT", "8000") +PROTOCOL = environ.get("PACKET_PROTOCOL", "https://") SERVER_NAME = environ.get("PACKET_SERVER_NAME", IP + ":" + PORT) SECRET_KEY = environ.get("PACKET_SECRET_KEY", "PLEASE_REPLACE_ME") diff --git a/packet/__init__.py b/packet/__init__.py index f68c9184..93610e24 100644 --- a/packet/__init__.py +++ b/packet/__init__.py @@ -7,6 +7,7 @@ import json import csh_ldap +import onesignal from flask import Flask from flask_gzip import Gzip from flask_migrate import Migrate @@ -44,6 +45,11 @@ client_metadata=ClientMetadata(app.config["OIDC_CLIENT_ID"], app.config["OIDC_CLIENT_SECRET"])) +# Initialize Onesignal Notification apps +onesignal_client = onesignal.Client(user_auth_key=app.config["ONESIGNAL_USER_AUTH_KEY"], + app_auth_key=app.config["ONESIGNAL_APP_AUTH_KEY"], + app_id=app.config["ONESIGNAL_APP_ID"]) + auth = OIDCAuthentication({'app': APP_CONFIG}, app) # LDAP diff --git a/packet/notifications.py b/packet/notifications.py new file mode 100644 index 00000000..10becabc --- /dev/null +++ b/packet/notifications.py @@ -0,0 +1,29 @@ +import onesignal +from . import app, onesignal_client + +post_body = { + "content": {"en": "Default message"}, + "headings": {"en": "Default Title"}, + "included_segments": ["Active Users", "Inactive Users"], + "chrome_web_icon": app.config["PROTOCOL"] + app.config["SERVER_NAME"] + "/static/android-chrome-512x512.png", + "chrome_web_badge": app.config["PROTOCOL"] + app.config["SERVER_NAME"] + "/static/android-chrome-512x512.png", + "url": app.config["PROTOCOL"] + app.config["SERVER_NAME"] +} + + +def packet_signed_notification(packet, signer): + subscriptions = NotificationSubscription.query.filter_by(freshman_username=packet.freshman_username) + if subscriptions: + tokens = list(filter(lambda subscription: subscription.token, subscriptions)) + + notification = onesignal.Notification(post_body=post_body) + notification.post_body["content"]["en"] = signer + ' signed your packet! Congrats or I\'m Sorry' + notification.post_body["headings"]["en"] = 'New Packet Signature!' + notification.post_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + signer + notification.post_body["include_player_ids"] = tokens + + onesignal_response = onesignal_client.send_notification(notification) + print(onesignal_response.status_code) + print(onesignal_response.json()) + if onesignal_response.status_code == 200: + app.logger.info("The notification ({}) sent out successfully".format(notification.post_body)) diff --git a/packet/routes/api.py b/packet/routes/api.py index 5e0bc576..db9fae7c 100644 --- a/packet/routes/api.py +++ b/packet/routes/api.py @@ -8,6 +8,7 @@ from packet.mail import send_report_mail from packet.utils import before_request, packet_auth, notify_slack from packet.models import Packet, MiscSignature, NotificationSubscription +from packet.notifications import packet_signed_notification @app.route("/api/v1/sign//", methods=["POST"]) @@ -23,17 +24,20 @@ def sign(packet_id, info): for sig in filter(lambda sig: sig.member == info["uid"], packet.upper_signatures): sig.signed = True app.logger.info("Member {} signed packet {} as an upperclassman".format(info["uid"], packet_id)) + packet_signed_notification(packet, info["uid"]) return commit_sig(packet, was_100) # The CSHer is a misc so add a new row db.session.add(MiscSignature(packet=packet, member=info["uid"])) app.logger.info("Member {} signed packet {} as a misc".format(info["uid"], packet_id)) + packet_signed_notification(packet, info["uid"]) return commit_sig(packet, was_100) else: # Check if the freshman is onfloor and if so, sign that row for sig in filter(lambda sig: sig.freshman_username == info["uid"], packet.fresh_signatures): sig.signed = True app.logger.info("Freshman {} signed packet {}".format(info["uid"], packet_id)) + packet_signed_notification(packet, info["uid"]) return commit_sig(packet, was_100) app.logger.warn("Failed to add {}'s signature to packet {}".format(info["uid"], packet_id)) From 7ea68b079e4700ec5b05259fd425c163a9501817 Mon Sep 17 00:00:00 2001 From: Devin Matte Date: Thu, 15 Aug 2019 20:34:06 -0400 Subject: [PATCH 2/5] Adding 100 notification --- packet/notifications.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/packet/notifications.py b/packet/notifications.py index 10becabc..2eb8d871 100644 --- a/packet/notifications.py +++ b/packet/notifications.py @@ -27,3 +27,22 @@ def packet_signed_notification(packet, signer): print(onesignal_response.json()) if onesignal_response.status_code == 200: app.logger.info("The notification ({}) sent out successfully".format(notification.post_body)) + + +def packet_100_percent_notification(packet): + subscriptions = NotificationSubscription.query.all() + if subscriptions: + tokens = list(filter(lambda subscription: subscription.token, subscriptions)) + + notification = onesignal.Notification(post_body=post_body) + notification.post_body["content"]["en"] = packet.freshman.name + ' got 💯 on packet!' + notification.post_body["headings"]["en"] = 'New 100% on Packet!' + # TODO: Issue #156 + notification.post_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + packet.freshman_username + notification.post_body["include_player_ids"] = tokens + + onesignal_response = onesignal_client.send_notification(notification) + print(onesignal_response.status_code) + print(onesignal_response.json()) + if onesignal_response.status_code == 200: + app.logger.info("The notification ({}) sent out successfully".format(notification.post_body)) From fbb54824a2ceeb8a299433ab77d35a387cb46230 Mon Sep 17 00:00:00 2001 From: Devin Matte Date: Thu, 15 Aug 2019 22:10:43 -0400 Subject: [PATCH 3/5] Completed Notifications --- packet/notifications.py | 10 +++++---- packet/templates/include/head.html | 34 +++++++++++++++--------------- 2 files changed, 23 insertions(+), 21 deletions(-) diff --git a/packet/notifications.py b/packet/notifications.py index 2eb8d871..69436690 100644 --- a/packet/notifications.py +++ b/packet/notifications.py @@ -1,12 +1,14 @@ import onesignal -from . import app, onesignal_client + +from packet import app, onesignal_client +from packet.models import NotificationSubscription post_body = { "content": {"en": "Default message"}, "headings": {"en": "Default Title"}, "included_segments": ["Active Users", "Inactive Users"], - "chrome_web_icon": app.config["PROTOCOL"] + app.config["SERVER_NAME"] + "/static/android-chrome-512x512.png", - "chrome_web_badge": app.config["PROTOCOL"] + app.config["SERVER_NAME"] + "/static/android-chrome-512x512.png", + "chrome_web_icon": app.config["PROTOCOL"] + app.config["SERVER_NAME"] + "/static/android-chrome-512x512.png", + "chrome_web_badge": app.config["PROTOCOL"] + app.config["SERVER_NAME"] + "/static/android-chrome-512x512.png", "url": app.config["PROTOCOL"] + app.config["SERVER_NAME"] } @@ -40,7 +42,7 @@ def packet_100_percent_notification(packet): # TODO: Issue #156 notification.post_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + packet.freshman_username notification.post_body["include_player_ids"] = tokens - + onesignal_response = onesignal_client.send_notification(notification) print(onesignal_response.status_code) print(onesignal_response.json()) diff --git a/packet/templates/include/head.html b/packet/templates/include/head.html index 2b394dae..acdd27b8 100644 --- a/packet/templates/include/head.html +++ b/packet/templates/include/head.html @@ -38,27 +38,27 @@ From 6cdf8be435373e4263c6dd980101c6f67dce9d2d Mon Sep 17 00:00:00 2001 From: Devin Matte Date: Thu, 15 Aug 2019 22:14:14 -0400 Subject: [PATCH 4/5] Send 100% notification --- packet/routes/api.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packet/routes/api.py b/packet/routes/api.py index db9fae7c..2e772df0 100644 --- a/packet/routes/api.py +++ b/packet/routes/api.py @@ -8,7 +8,7 @@ from packet.mail import send_report_mail from packet.utils import before_request, packet_auth, notify_slack from packet.models import Packet, MiscSignature, NotificationSubscription -from packet.notifications import packet_signed_notification +from packet.notifications import packet_signed_notification, packet_100_percent_notification @app.route("/api/v1/sign//", methods=["POST"]) @@ -70,6 +70,7 @@ def report(info): def commit_sig(packet, was_100): db.session.commit() if not was_100 and packet.is_100(): + packet_100_percent_notification(packet) notify_slack(packet.freshman.name) return "Success: Signed Packet: " + packet.freshman_username From ebb19c6006597baf03bc8243b49099a81d67f853 Mon Sep 17 00:00:00 2001 From: Devin Matte Date: Thu, 15 Aug 2019 22:42:34 -0400 Subject: [PATCH 5/5] Removing prints --- packet/notifications.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/packet/notifications.py b/packet/notifications.py index 69436690..51ae12a9 100644 --- a/packet/notifications.py +++ b/packet/notifications.py @@ -25,8 +25,6 @@ def packet_signed_notification(packet, signer): notification.post_body["include_player_ids"] = tokens onesignal_response = onesignal_client.send_notification(notification) - print(onesignal_response.status_code) - print(onesignal_response.json()) if onesignal_response.status_code == 200: app.logger.info("The notification ({}) sent out successfully".format(notification.post_body)) @@ -44,7 +42,5 @@ def packet_100_percent_notification(packet): notification.post_body["include_player_ids"] = tokens onesignal_response = onesignal_client.send_notification(notification) - print(onesignal_response.status_code) - print(onesignal_response.json()) if onesignal_response.status_code == 200: app.logger.info("The notification ({}) sent out successfully".format(notification.post_body))