Skip to content

Commit 1465ee5

Browse files
committed
Migrate notifications api to use marshmallow
1 parent 9c67e54 commit 1465ee5

File tree

8 files changed

+63
-29
lines changed

8 files changed

+63
-29
lines changed

Pipfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ gunicorn = "*"
1919
python-dotenv = "*"
2020
markupsafe = "*"
2121
flask-sqlalchemy = "*"
22+
flask-marshmallow = "*"
23+
marshmallow-sqlalchemy = "*"
2224

2325
[requires]
2426
python_version = "3.8"

Pipfile.lock

Lines changed: 25 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

snowflake/app.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from . import filters, settings, logger
88
from .controllers import api, login, register, profile, index, one_on_one, appreciation, logout, notifications
99
from .db import db
10+
from .marshmallow import marshmallow
1011
from .models import User
1112

1213
load_dotenv()
@@ -16,6 +17,7 @@
1617

1718
db.init_app(app)
1819
settings.init_app(app)
20+
marshmallow.init_app(app)
1921

2022
login_manager = LoginManager()
2123
login_manager.init_app(app)
Lines changed: 11 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,14 @@
11
from flask import Blueprint, jsonify
22
from flask_login import login_required, current_user
33

4-
from snowflake.controllers.api.response import not_found
5-
from snowflake.db import db
6-
from snowflake.models import User
7-
from snowflake.models.notification import Notification
4+
from .response import not_found
5+
from ...db import db
6+
from ...models import Notification
7+
from ...schemas.notification import NotificationSchema
88

99
blueprint = Blueprint('api.notifications', __name__)
1010

11-
12-
def user_to_json(user: User):
13-
return {
14-
'id': user.id,
15-
'username': user.username,
16-
'name': user.name
17-
}
18-
19-
20-
def to_json(n):
21-
return {
22-
'id': n.id,
23-
'created_at': n.created_at,
24-
'user': user_to_json(n.user),
25-
'type': n.type,
26-
'object_id': n.object_id,
27-
'read': n.read
28-
}
11+
schema = NotificationSchema()
2912

3013

3114
@login_required
@@ -37,18 +20,18 @@ def notification_count():
3720
@login_required
3821
@blueprint.route('/<_id>/mark_as_read', methods=['POST'])
3922
def mark_as_read(_id):
40-
notification = Notification.get(_id)
41-
if not notification:
23+
n = Notification.get(_id)
24+
if not n:
4225
return not_found()
4326

44-
notification.read = True
45-
db.session.add(notification)
27+
n.read = True
28+
db.session.add(n)
4629
db.session.commit()
4730

48-
return to_json(notification)
31+
return schema.jsonify(n)
4932

5033

5134
@login_required
5235
@blueprint.route('')
5336
def notification():
54-
return jsonify([to_json(n) for n in Notification.get_by_user(current_user)])
37+
return schema.jsonify(Notification.get_by_user(current_user), many=True)

snowflake/marshmallow.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from flask_marshmallow import Marshmallow
2+
3+
marshmallow = Marshmallow()

snowflake/schemas/__init__.py

Whitespace-only changes.

snowflake/schemas/notification.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from marshmallow_sqlalchemy.fields import Nested
2+
3+
from .user import UserSchema
4+
from ..marshmallow import marshmallow
5+
from ..models import Notification
6+
7+
8+
class NotificationSchema(marshmallow.SQLAlchemyAutoSchema):
9+
class Meta:
10+
model = Notification
11+
12+
user = Nested(UserSchema)

snowflake/schemas/user.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from ..marshmallow import marshmallow
2+
from ..models import User
3+
4+
5+
class UserSchema(marshmallow.SQLAlchemyAutoSchema):
6+
class Meta:
7+
model = User
8+
exclude = ("id",)

0 commit comments

Comments
 (0)