Skip to content

Commit 8f5f64a

Browse files
committed
Phase 2: Notification on signing
1 parent ee28245 commit 8f5f64a

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

config.env.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
DEBUG = False
1010
IP = environ.get("PACKET_IP", "localhost")
1111
PORT = environ.get("PACKET_PORT", "8000")
12+
PROTOCOL = environ.get("PACKET_PROTOCOL", "https://")
1213
SERVER_NAME = environ.get("PACKET_SERVER_NAME", IP + ":" + PORT)
1314
SECRET_KEY = environ.get("PACKET_SECRET_KEY", "PLEASE_REPLACE_ME")
1415

packet/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import json
88

99
import csh_ldap
10+
import onesignal
1011
from flask import Flask
1112
from flask_gzip import Gzip
1213
from flask_migrate import Migrate
@@ -44,6 +45,11 @@
4445
client_metadata=ClientMetadata(app.config["OIDC_CLIENT_ID"],
4546
app.config["OIDC_CLIENT_SECRET"]))
4647

48+
# Initialize Onesignal Notification apps
49+
onesignal_client = onesignal.Client(user_auth_key=app.config["ONESIGNAL_USER_AUTH_KEY"],
50+
app_auth_key=app.config["ONESIGNAL_APP_AUTH_KEY"],
51+
app_id=app.config["ONESIGNAL_APP_ID"])
52+
4753
auth = OIDCAuthentication({'app': APP_CONFIG}, app)
4854

4955
# LDAP

packet/notifications.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import onesignal
2+
from . import app, onesignal_client
3+
4+
post_body = {
5+
"content": {"en": "Default message"},
6+
"headings": {"en": "Default Title"},
7+
"included_segments": ["Active Users", "Inactive Users"],
8+
"chrome_web_icon": app.config["PROTOCOL"] + app.config["SERVER_NAME"] + "/static/android-chrome-512x512.png",
9+
"chrome_web_badge": app.config["PROTOCOL"] + app.config["SERVER_NAME"] + "/static/android-chrome-512x512.png",
10+
"url": app.config["PROTOCOL"] + app.config["SERVER_NAME"]
11+
}
12+
13+
14+
def packet_signed_notification(packet, signer):
15+
subscriptions = NotificationSubscription.query.filter_by(freshman_username=packet.freshman_username)
16+
if subscriptions:
17+
tokens = list(filter(lambda subscription: subscription.token, subscriptions))
18+
19+
notification = onesignal.Notification(post_body=post_body)
20+
notification.post_body["content"]["en"] = signer + ' signed your packet! Congrats or I\'m Sorry'
21+
notification.post_body["headings"]["en"] = 'New Packet Signature!'
22+
notification.post_body["chrome_web_icon"] = 'https://profiles.csh.rit.edu/image/' + signer
23+
notification.post_body["include_player_ids"] = tokens
24+
25+
onesignal_response = onesignal_client.send_notification(notification)
26+
print(onesignal_response.status_code)
27+
print(onesignal_response.json())
28+
if onesignal_response.status_code == 200:
29+
app.logger.info("The notification ({}) sent out successfully".format(notification.post_body))

packet/routes/api.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from packet.mail import send_report_mail
99
from packet.utils import before_request, packet_auth, notify_slack
1010
from packet.models import Packet, MiscSignature, NotificationSubscription
11+
from packet.notifications import packet_signed_notification
1112

1213

1314
@app.route("/api/v1/sign/<packet_id>/", methods=["POST"])
@@ -23,17 +24,20 @@ def sign(packet_id, info):
2324
for sig in filter(lambda sig: sig.member == info["uid"], packet.upper_signatures):
2425
sig.signed = True
2526
app.logger.info("Member {} signed packet {} as an upperclassman".format(info["uid"], packet_id))
27+
packet_signed_notification(packet, info["uid"])
2628
return commit_sig(packet, was_100)
2729

2830
# The CSHer is a misc so add a new row
2931
db.session.add(MiscSignature(packet=packet, member=info["uid"]))
3032
app.logger.info("Member {} signed packet {} as a misc".format(info["uid"], packet_id))
33+
packet_signed_notification(packet, info["uid"])
3134
return commit_sig(packet, was_100)
3235
else:
3336
# Check if the freshman is onfloor and if so, sign that row
3437
for sig in filter(lambda sig: sig.freshman_username == info["uid"], packet.fresh_signatures):
3538
sig.signed = True
3639
app.logger.info("Freshman {} signed packet {}".format(info["uid"], packet_id))
40+
packet_signed_notification(packet, info["uid"])
3741
return commit_sig(packet, was_100)
3842

3943
app.logger.warn("Failed to add {}'s signature to packet {}".format(info["uid"], packet_id))

0 commit comments

Comments
 (0)