Skip to content

Commit 1a6ce5e

Browse files
committed
feat(notifications): setup notific ations
- add new model for adminnotifications [Delivers CON-71]
1 parent 0820432 commit 1a6ce5e

13 files changed

+117
-31
lines changed

.coveragerc

Lines changed: 0 additions & 17 deletions
This file was deleted.
File renamed without changes.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
from flask_socketio import send
2+
from admin_notifications.models import AdminNotification
3+
from manage import socketio
4+
5+
def create_notification(title, message, location_id):
6+
notification = AdminNotification(
7+
title=title,
8+
message=message,
9+
location_id=location_id,
10+
status="unread"
11+
)
12+
notification.save()
13+
14+
@socketio.on('notification')
15+
def send_notification():
16+
notification = { "title": title, "message": message }
17+
return send(notification, broadcast=True)
18+
19+
20+
21+
22+
23+
def create_notif_lol():
24+
return create_notification(title="twre3r", message="rerf3ef3rf3rf", location_id=1)

admin_notifications/models.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from sqlalchemy import (Column, String, Enum, Integer, ForeignKey)
2+
from helpers.database import Base
3+
from utilities.utility import Utility, StatusType
4+
5+
6+
class AdminNotification(Base, Utility):
7+
__tablename__ = 'admin_notifications'
8+
9+
id = Column(Integer, primary_key=True) # noqa
10+
title = Column(String, nullable=True)
11+
message = Column(String, nullable=True)
12+
date_received = Column(String, nullable=True)
13+
date_read = Column(String, nullable=True)
14+
status = Column(Enum(StatusType), default="unread")
15+
location_id = Column(
16+
Integer,
17+
ForeignKey('locations.id', ondelete="CASCADE"),
18+
nullable=True
19+
)

admin_notifications/socket_handler.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
from flask_socketio import send
2+
from admin_notifications.models import AdminNotification
3+
from flask import jsonify
4+
5+
def serialize_message(notification):
6+
return {
7+
"title": notification.title,
8+
"message": notification.message,
9+
}
10+
11+
def send_notifications():
12+
query = AdminNotification.query
13+
notifications = query.filter_by(status="unread").all()
14+
notifications = [serialize_message(notification) for notification in notifications]
15+
return send(notifications, broadcast=True)
16+
17+
def send_single_notification(title, message):
18+
notification = { "title": title, "message": message }
19+
return send(notification, broadcast=True)

alembic/env.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
from api.response.models import Response
4646
from api.tag.models import Tag
4747
from api.structure.models import Structure
48-
48+
from admin_notifications.models import AdminNotification
4949

5050
target_metadata = Base.metadata
5151

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
"""add admin notifications table
2+
3+
Revision ID: c65718cd4ed8
4+
Revises: a5a4841351d7
5+
Create Date: 2019-06-21 08:38:07.110402
6+
7+
"""
8+
from alembic import op
9+
import sqlalchemy as sa
10+
11+
12+
# revision identifiers, used by Alembic.
13+
revision = 'c65718cd4ed8'
14+
down_revision = 'a5a4841351d7'
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('admin_notifications',
22+
sa.Column('id', sa.Integer(), nullable=False),
23+
sa.Column('title', sa.String(), nullable=True),
24+
sa.Column('message', sa.String(), nullable=True),
25+
sa.Column('date_received', sa.String(), nullable=True),
26+
sa.Column('date_read', sa.String(), nullable=True),
27+
sa.Column('status', sa.Enum('read', 'unread', name='statustype'), nullable=True),
28+
sa.Column('location_id', sa.Integer(), nullable=True),
29+
sa.ForeignKeyConstraint(['location_id'], ['locations.id'], ondelete='CASCADE'),
30+
sa.PrimaryKeyConstraint('id')
31+
)
32+
# ### end Alembic commands ###
33+
34+
35+
def downgrade():
36+
# ### commands auto generated by Alembic - please adjust! ###
37+
op.drop_table('admin_notifications')
38+
# ### end Alembic commands ###

app.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
from flask_graphql import GraphQLView
44
from flask_cors import CORS
55
from flask_json import FlaskJSON
6-
from flask_socketio import SocketIO
76

87
from flask_mail import Mail
98
from config import config
@@ -12,15 +11,13 @@
1211
from healthcheck_schema import healthcheck_schema
1312
from helpers.auth.authentication import Auth
1413
from api.analytics.analytics_request import AnalyticsRequest
15-
1614
mail = Mail()
1715

1816

1917
def create_app(config_name):
2018
app = Flask(__name__)
2119
CORS(app)
2220
FlaskJSON(app)
23-
SocketIO(app)
2421
app.config.from_object(config[config_name])
2522
config[config_name].init_app(app)
2623
mail.init_app(app)

manage.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import bugsnag
44
from flask_script import Manager, Shell
55
from bugsnag.flask import handle_exceptions
6-
6+
from flask_socketio import SocketIO
77

88
# Configure bugnsag
99
bugsnag.configure(
@@ -18,16 +18,23 @@
1818
app = create_app(os.getenv('APP_SETTINGS') or 'default')
1919
handle_exceptions(app)
2020
manager = Manager(app)
21+
socketio = SocketIO(app)
2122

2223

2324
def make_shell_context():
2425
return dict(app=app)
2526

2627

28+
@socketio.on('message')
29+
def hanldleMessage(msg):
30+
from admin_notifications.socket_handler import send_notifications
31+
return send_notifications()
32+
33+
2734
manager.add_command(
2835
"shell", Shell(
2936
make_context=make_shell_context))
3037

31-
3238
if __name__ == '__main__':
39+
socketio.run(app, debug=True)
3340
manager.run()

requirements.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,15 @@ coverage==4.5.1
1616
coveralls==1.8.0
1717
decorator==4.4.0
1818
docopt==0.6.2
19+
eventlet==0.25.0
1920
flake8==3.5.0
2021
Flask==0.12.2
2122
Flask-Cors==3.0.4
2223
Flask-GraphQL==1.4.1
2324
Flask-JSON==0.3.2
2425
Flask-Mail==0.9.1
2526
Flask-Script==2.0.6
26-
Flask-SocketIO==4.1.0
27+
flask-socketio==4.1.0
2728
Flask-Testing==0.7.1
2829
google-api-python-client==1.6.7
2930
graphene==2.1

setup.cfg

Lines changed: 0 additions & 7 deletions
This file was deleted.

utilities/utility.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,3 +86,8 @@ class QuestionType(enum.Enum):
8686
rate = "rate"
8787
check = "check"
8888
textarea = "textarea"
89+
missingitem = "missing_items"
90+
91+
class StatusType(enum.Enum):
92+
read = "read"
93+
unread = "unread"

0 commit comments

Comments
 (0)