mirror of
https://github.com/GSA/notifications-api.git
synced 2025-12-23 17:01:35 -05:00
Create 'v2' get notifications route
Return multiple notifications for a service.
Choosing a page_size or a page_number is no longer allowed.
Instead, there is a `next` link included with will return the
next {default_page_size} notifications in the sequence.
Query parameters accepted are:
- template_type: filter by specific template types
- status: filter by specific statuses
- older_than: return a chronological list of notifications older
than this one. The notification with the id that is passed in
is _not_ returned.
Note that both `template_type` and `status` can accept multiple
parameters. Thus it is possible to call
`/v2/notifications?status=created&status=sending&status=delivered`
This commit is contained in:
@@ -262,7 +262,8 @@ def get_notifications_for_service(
|
|||||||
key_type=None,
|
key_type=None,
|
||||||
personalisation=False,
|
personalisation=False,
|
||||||
include_jobs=False,
|
include_jobs=False,
|
||||||
include_from_test_key=False
|
include_from_test_key=False,
|
||||||
|
older_than=None
|
||||||
):
|
):
|
||||||
if page_size is None:
|
if page_size is None:
|
||||||
page_size = current_app.config['PAGE_SIZE']
|
page_size = current_app.config['PAGE_SIZE']
|
||||||
@@ -273,6 +274,11 @@ def get_notifications_for_service(
|
|||||||
days_ago = date.today() - timedelta(days=limit_days)
|
days_ago = date.today() - timedelta(days=limit_days)
|
||||||
filters.append(func.date(Notification.created_at) >= days_ago)
|
filters.append(func.date(Notification.created_at) >= days_ago)
|
||||||
|
|
||||||
|
if older_than is not None:
|
||||||
|
older_than_created_at = db.session.query(
|
||||||
|
Notification.created_at).filter(Notification.id == older_than).as_scalar()
|
||||||
|
filters.append(Notification.created_at < older_than_created_at)
|
||||||
|
|
||||||
if not include_jobs or (key_type and key_type != KEY_TYPE_NORMAL):
|
if not include_jobs or (key_type and key_type != KEY_TYPE_NORMAL):
|
||||||
filters.append(Notification.job_id.is_(None))
|
filters.append(Notification.job_id.is_(None))
|
||||||
|
|
||||||
|
|||||||
@@ -468,6 +468,7 @@ class NotificationsFilterSchema(ma.Schema):
|
|||||||
limit_days = fields.Int(required=False)
|
limit_days = fields.Int(required=False)
|
||||||
include_jobs = fields.Boolean(required=False)
|
include_jobs = fields.Boolean(required=False)
|
||||||
include_from_test_key = fields.Boolean(required=False)
|
include_from_test_key = fields.Boolean(required=False)
|
||||||
|
older_than = fields.UUID(required=False)
|
||||||
|
|
||||||
@pre_load
|
@pre_load
|
||||||
def handle_multidict(self, in_data):
|
def handle_multidict(self, in_data):
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
from flask import jsonify
|
from flask import jsonify, request, url_for
|
||||||
|
|
||||||
from app import api_user
|
from app import api_user
|
||||||
from app.dao import notifications_dao
|
from app.dao import notifications_dao
|
||||||
|
from app.schemas import notifications_filter_schema
|
||||||
from app.v2.notifications import notification_blueprint
|
from app.v2.notifications import notification_blueprint
|
||||||
|
|
||||||
|
|
||||||
@@ -14,9 +15,30 @@ def get_notification_by_id(id):
|
|||||||
return jsonify(notification.serialize()), 200
|
return jsonify(notification.serialize()), 200
|
||||||
|
|
||||||
|
|
||||||
@notification_blueprint.route("/", methods=['GET'])
|
@notification_blueprint.route("", methods=['GET'])
|
||||||
def get_notifications():
|
def get_notifications():
|
||||||
# validate notifications request arguments
|
data = notifications_filter_schema.load(request.args).data
|
||||||
# fetch all notifications
|
|
||||||
# return notifications_response schema
|
paginated_notifications = notifications_dao.get_notifications_for_service(
|
||||||
pass
|
str(api_user.service_id),
|
||||||
|
filter_dict=data,
|
||||||
|
key_type=api_user.key_type,
|
||||||
|
personalisation=True,
|
||||||
|
older_than=data.get('older_than')
|
||||||
|
)
|
||||||
|
|
||||||
|
def _build_links(notifications):
|
||||||
|
_links = {
|
||||||
|
'current': url_for(".get_notifications", **request.args.to_dict(flat=False)),
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(notifications):
|
||||||
|
next_query_params = dict(request.args.to_dict(flat=False), older_than=notifications[-1].id)
|
||||||
|
_links['next'] = url_for(".get_notifications", **next_query_params)
|
||||||
|
|
||||||
|
return _links
|
||||||
|
|
||||||
|
return jsonify(
|
||||||
|
notifications=[notification.serialize() for notification in paginated_notifications.items],
|
||||||
|
links=_build_links(paginated_notifications.items)
|
||||||
|
), 200
|
||||||
|
|||||||
Reference in New Issue
Block a user