mirror of
https://github.com/simple-login/app.git
synced 2025-02-23 07:13:18 +08:00
Return whether there's more notification in GET /api/notifications
This commit is contained in:
parent
c47fb44c1e
commit
1e03f26cfa
3 changed files with 41 additions and 19 deletions
27
README.md
27
README.md
|
@ -1229,11 +1229,28 @@ Input:
|
|||
- page in url: the page number, starts at 0
|
||||
|
||||
Output:
|
||||
List of notification, each notification has:
|
||||
- id
|
||||
- message: the message in html
|
||||
- read: whether the user has read the notification
|
||||
- created_at: when the notification is created
|
||||
- more: whether there's more notifications
|
||||
- notifications: list of notification, each notification has:
|
||||
- id
|
||||
- message: the message in html
|
||||
- read: whether the user has read the notification
|
||||
- created_at: when the notification is created
|
||||
|
||||
For example
|
||||
|
||||
```json
|
||||
{
|
||||
"more": false,
|
||||
"notifications": [
|
||||
{
|
||||
"created_at": "2 minutes ago",
|
||||
"id": 1,
|
||||
"message": "Hey!",
|
||||
"read": false
|
||||
}
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
#### POST /api/notifications/:notification_id
|
||||
|
||||
|
|
|
@ -1,5 +1,3 @@
|
|||
from time import sleep
|
||||
|
||||
from flask import g
|
||||
from flask import jsonify
|
||||
from flask import request
|
||||
|
@ -21,11 +19,13 @@ def get_notifications():
|
|||
Input:
|
||||
- page: in url. Starts at 0
|
||||
|
||||
Output: list of notifications. Each notification has the following field:
|
||||
- id
|
||||
- message
|
||||
- read
|
||||
- created_at
|
||||
Output:
|
||||
- more: boolean. Whether there's more notification to load
|
||||
- notifications: list of notifications.
|
||||
- id
|
||||
- message
|
||||
- read
|
||||
- created_at
|
||||
"""
|
||||
user = g.user
|
||||
try:
|
||||
|
@ -36,22 +36,25 @@ def get_notifications():
|
|||
notifications = (
|
||||
Notification.query.filter_by(user_id=user.id)
|
||||
.order_by(Notification.read, Notification.created_at.desc())
|
||||
.limit(PAGE_LIMIT)
|
||||
.limit(PAGE_LIMIT + 1) # load a record more to know whether there's more
|
||||
.offset(page * PAGE_LIMIT)
|
||||
.all()
|
||||
)
|
||||
|
||||
have_more = len(notifications) > PAGE_LIMIT
|
||||
|
||||
return (
|
||||
jsonify(
|
||||
[
|
||||
more=have_more,
|
||||
notifications=[
|
||||
{
|
||||
"id": notification.id,
|
||||
"message": notification.message,
|
||||
"read": notification.read,
|
||||
"created_at": notification.created_at.humanize(),
|
||||
}
|
||||
for notification in notifications
|
||||
]
|
||||
for notification in notifications[:PAGE_LIMIT]
|
||||
],
|
||||
),
|
||||
200,
|
||||
)
|
||||
|
|
|
@ -25,8 +25,9 @@ def test_get_notifications(flask_client):
|
|||
)
|
||||
|
||||
assert r.status_code == 200
|
||||
assert len(r.json) == 2
|
||||
for n in r.json:
|
||||
assert r.json["more"] is False
|
||||
assert len(r.json["notifications"]) == 2
|
||||
for n in r.json["notifications"]:
|
||||
assert n["id"] > 0
|
||||
assert n["message"]
|
||||
assert n["read"] is False
|
||||
|
@ -37,7 +38,8 @@ def test_get_notifications(flask_client):
|
|||
url_for("api.get_notifications", page=1),
|
||||
headers={"Authentication": api_key.code},
|
||||
)
|
||||
assert len(r.json) == 0
|
||||
assert r.json["more"] is False
|
||||
assert len(r.json["notifications"]) == 0
|
||||
|
||||
|
||||
def test_mark_notification_as_read(flask_client):
|
||||
|
|
Loading…
Reference in a new issue