Skip to content

Commit ee28245

Browse files
committed
Adding model to collect subscriptions
1 parent 23e7c20 commit ee28245

File tree

5 files changed

+67
-4
lines changed

5 files changed

+67
-4
lines changed
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
"""notifications
2+
3+
Revision ID: 53768f0a4850
4+
Revises: eecf30892d0e
5+
Create Date: 2019-08-06 22:15:04.400982
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = '53768f0a4850'
14+
down_revision = 'eecf30892d0e'
15+
branch_labels = None
16+
depends_on = None
17+
18+
19+
def upgrade():
20+
# ### commands auto generated by Alembic - please adjust! ###
21+
op.create_table('notification_subscriptions',
22+
sa.Column('member', sa.String(length=36), nullable=True),
23+
sa.Column('freshman_username', sa.String(length=10), nullable=True),
24+
sa.Column('token', sa.String(length=256), nullable=False),
25+
sa.ForeignKeyConstraint(['freshman_username'], ['freshman.rit_username'], ),
26+
sa.PrimaryKeyConstraint('token')
27+
)
28+
# ### end Alembic commands ###
29+
30+
31+
def downgrade():
32+
# ### commands auto generated by Alembic - please adjust! ###
33+
op.drop_table('notification_subscriptions')
34+
# ### end Alembic commands ###
35+

packet/models.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,10 @@ class MiscSignature(db.Model):
154154
updated = Column(DateTime, default=datetime.now, onupdate=datetime.now, nullable=False)
155155

156156
packet = relationship("Packet", back_populates="misc_signatures")
157+
158+
159+
class NotificationSubscription(db.Model):
160+
__tablename__ = "notification_subscriptions"
161+
member = Column(String(36), nullable=True)
162+
freshman_username = Column(ForeignKey("freshman.rit_username"), nullable=True)
163+
token = Column(String(256), primary_key=True, nullable=False)

packet/routes/api.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from packet.context_processors import get_rit_name
88
from packet.mail import send_report_mail
99
from packet.utils import before_request, packet_auth, notify_slack
10-
from packet.models import Packet, MiscSignature
10+
from packet.models import Packet, MiscSignature, NotificationSubscription
1111

1212

1313
@app.route("/api/v1/sign/<packet_id>/", methods=["POST"])
@@ -40,6 +40,20 @@ def sign(packet_id, info):
4040
return "Error: Signature not valid. Reason: Unknown"
4141

4242

43+
@app.route("/api/v1/subscribe/", methods=["POST"])
44+
@packet_auth
45+
@before_request
46+
def subscribe(info):
47+
data = request.form
48+
if app.config["REALM"] == "csh":
49+
subscription = NotificationSubscription(token=data['token'], member=info["uid"])
50+
else:
51+
subscription = NotificationSubscription(token=data['token'], freshman_username=info["uid"])
52+
db.session.add(subscription)
53+
db.session.commit()
54+
return "Token subscribed for " + info["uid"]
55+
56+
4357
@app.route("/api/v1/report/", methods=["POST"])
4458
@packet_auth
4559
@before_request

packet/routes/shared.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,3 @@ def service_worker():
8080
@app.route('/OneSignalSDKUpdaterWorker.js', methods=['GET'])
8181
def update_service_worker():
8282
return app.send_static_file('js/update-sw.js')
83-

packet/templates/include/head.html

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,16 @@
4747
OneSignal.showNativePrompt();
4848
OneSignal.on("subscriptionChange", function(){
4949
OneSignal.getUserId().then(function(result){
50-
// TODO: Send to backend
51-
console.log(result);
50+
$.ajax({
51+
url: "/api/v1/subscribe/",
52+
method: "POST",
53+
data: {
54+
token: result
55+
},
56+
success: function (data) {
57+
console.log(data);
58+
}
59+
});
5260
});
5361
});
5462
});

0 commit comments

Comments
 (0)