2016-11-23 11:44:38 +00:00
|
|
|
from flask import jsonify, request, url_for
|
2016-11-18 17:36:11 +00:00
|
|
|
from app import api_user
|
|
|
|
|
from app.dao import notifications_dao
|
2016-11-28 14:22:51 +00:00
|
|
|
from app.schema_validation import validate
|
2016-10-25 18:04:03 +01:00
|
|
|
from app.v2.notifications import notification_blueprint
|
2016-11-28 14:22:51 +00:00
|
|
|
from app.v2.notifications.notification_schemas import get_notifications_request
|
2016-10-25 18:04:03 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notification_blueprint.route("/<uuid:id>", methods=['GET'])
|
|
|
|
|
def get_notification_by_id(id):
|
2016-11-18 17:36:11 +00:00
|
|
|
notification = notifications_dao.get_notification_with_personalisation(
|
|
|
|
|
str(api_user.service_id), id, key_type=None
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
return jsonify(notification.serialize()), 200
|
2016-10-25 18:04:03 +01:00
|
|
|
|
|
|
|
|
|
2016-11-23 11:44:38 +00:00
|
|
|
@notification_blueprint.route("", methods=['GET'])
|
2016-10-25 18:04:03 +01:00
|
|
|
def get_notifications():
|
2016-11-28 14:22:51 +00:00
|
|
|
_data = request.args.to_dict(flat=False)
|
|
|
|
|
if 'older_than' in _data:
|
|
|
|
|
# flat=False makes everything a list, but we only ever allow one value for "older_than"
|
|
|
|
|
_data['older_than'] = _data['older_than'][0]
|
|
|
|
|
|
|
|
|
|
data = validate(_data, get_notifications_request)
|
2016-11-23 11:44:38 +00:00
|
|
|
|
|
|
|
|
paginated_notifications = notifications_dao.get_notifications_for_service(
|
|
|
|
|
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 = {
|
2016-11-28 14:22:51 +00:00
|
|
|
'current': url_for(".get_notifications", _external=True, **data),
|
2016-11-23 11:44:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if len(notifications):
|
2016-11-28 14:22:51 +00:00
|
|
|
next_query_params = dict(data, older_than=notifications[-1].id)
|
2016-11-28 11:13:11 +00:00
|
|
|
_links['next'] = url_for(".get_notifications", _external=True, **next_query_params)
|
2016-11-23 11:44:38 +00:00
|
|
|
|
|
|
|
|
return _links
|
|
|
|
|
|
|
|
|
|
return jsonify(
|
|
|
|
|
notifications=[notification.serialize() for notification in paginated_notifications.items],
|
|
|
|
|
links=_build_links(paginated_notifications.items)
|
|
|
|
|
), 200
|