Skip to content

Commit a843974

Browse files
authored
Merge pull request #69 from snowflake-app/notification-api
Notification API
2 parents 2a7f284 + 0c8b821 commit a843974

File tree

5 files changed

+116
-75
lines changed

5 files changed

+116
-75
lines changed

Pipfile.lock

Lines changed: 64 additions & 63 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
1-
from flask import Blueprint, jsonify
1+
from flask import Blueprint, jsonify, request
22
from flask_login import login_required, current_user
33

4+
from .request import requires_json
45
from .response import not_found
56
from ...db import db
67
from ...models import Notification
7-
from ...schemas.notification import NotificationSchema
8+
from ...schemas.notification import NotificationSchema, UpdateNotificationSchema
89

910
blueprint = Blueprint('api.notifications', __name__)
1011

1112
schema = NotificationSchema()
13+
update_notification_schema = UpdateNotificationSchema()
1214

1315

1416
@blueprint.route('/_count')
@@ -17,21 +19,25 @@ def notification_count():
1719
return jsonify(Notification.count_unread_by_user(current_user))
1820

1921

20-
@blueprint.route('/<_id>/mark_as_read', methods=['POST'])
22+
@blueprint.route('/<_id>', methods=['PATCH'])
2123
@login_required
22-
def mark_as_read(_id):
23-
n = Notification.get(_id)
24-
if not n:
24+
@requires_json
25+
def update_notification(_id):
26+
notification = Notification.get(_id)
27+
if not notification:
2528
return not_found()
2629

27-
n.read = True
28-
db.session.add(n)
30+
req_body = update_notification_schema.load(request.json)
31+
32+
notification.read = req_body['read']
33+
34+
db.session.add(notification)
2935
db.session.commit()
3036

31-
return schema.jsonify(n)
37+
return schema.jsonify(notification)
3238

3339

34-
@blueprint.route('/introspect', methods=['GET'])
40+
@blueprint.route('/', methods=['GET'])
3541
@login_required
36-
def notification():
42+
def get_all_notifications():
3743
return schema.jsonify(Notification.get_by_user(current_user), many=True)
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
from flask import request
2+
3+
from .response import bad_request
4+
5+
6+
def requires_json(wrapped_func):
7+
def func(*args, **kwargs):
8+
if not request.is_json:
9+
return bad_request()
10+
11+
return wrapped_func(*args, **kwargs)
12+
13+
return func

snowflake/schemas/notification.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,17 @@
1-
from .base import BaseSQLAlchemyAutoSchema
1+
from marshmallow.fields import Boolean
2+
from marshmallow_sqlalchemy.fields import Nested
3+
4+
from .base import BaseSQLAlchemyAutoSchema, BaseSchema
5+
from .object import ObjectSchema
26
from ..models import Notification
37

48

59
class NotificationSchema(BaseSQLAlchemyAutoSchema):
610
class Meta:
711
model = Notification
12+
13+
object = Nested(ObjectSchema)
14+
15+
16+
class UpdateNotificationSchema(BaseSchema):
17+
read: Boolean()

snowflake/schemas/object.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
from marshmallow import Schema
2+
from marshmallow.fields import Nested, DateTime
3+
4+
from .user import UserSchema
5+
from ..marshmallow import marshmallow
6+
7+
8+
class ObjectSchema(Schema):
9+
id: marshmallow.auto_field()
10+
created_at = DateTime()
11+
created_by = Nested(UserSchema)

0 commit comments

Comments
 (0)