2019-09-03 16:49:03 +01:00
|
|
|
import base64
|
|
|
|
|
|
2017-01-20 12:26:55 +00:00
|
|
|
from flask import jsonify, request, url_for, current_app
|
2019-09-03 16:49:03 +01:00
|
|
|
|
2017-05-05 15:23:06 +01:00
|
|
|
from app import api_user, authenticated_service
|
2016-11-18 17:36:11 +00:00
|
|
|
from app.dao import notifications_dao
|
2019-09-03 16:49:03 +01:00
|
|
|
from app.letters.utils import get_letter_pdf
|
2016-11-28 14:22:51 +00:00
|
|
|
from app.schema_validation import validate
|
2017-03-16 18:15:49 +00:00
|
|
|
from app.v2.notifications import v2_notification_blueprint
|
2018-04-03 16:37:41 +01:00
|
|
|
from app.v2.notifications.notification_schemas import get_notifications_request, notification_by_id
|
2019-09-03 16:49:03 +01:00
|
|
|
from app.models import NOTIFICATION_CREATED, NOTIFICATION_STATUS_TYPES_BILLABLE_FOR_LETTERS, LETTER_TYPE
|
2016-10-25 18:04:03 +01:00
|
|
|
|
|
|
|
|
|
2018-04-03 16:37:41 +01:00
|
|
|
@v2_notification_blueprint.route("/<notification_id>", methods=['GET'])
|
|
|
|
|
def get_notification_by_id(notification_id):
|
|
|
|
|
_data = {"notification_id": notification_id}
|
|
|
|
|
validate(_data, notification_by_id)
|
2016-11-18 17:36:11 +00:00
|
|
|
notification = notifications_dao.get_notification_with_personalisation(
|
2018-04-03 16:37:41 +01:00
|
|
|
authenticated_service.id, notification_id, key_type=None
|
2016-11-18 17:36:11 +00:00
|
|
|
)
|
|
|
|
|
|
2019-09-03 16:49:03 +01:00
|
|
|
response = notification.serialize()
|
|
|
|
|
|
|
|
|
|
if request.args.get('return_pdf_content') and notification.notification_type == LETTER_TYPE:
|
|
|
|
|
if notification.status in (NOTIFICATION_CREATED, *NOTIFICATION_STATUS_TYPES_BILLABLE_FOR_LETTERS):
|
|
|
|
|
pdf_data = get_letter_pdf(notification)
|
|
|
|
|
else:
|
|
|
|
|
# precompiled letters that are still being virus scanned, or that failed validation/virus scan
|
|
|
|
|
pdf_data = b''
|
|
|
|
|
response['body'] = base64.b64encode(pdf_data).decode('utf-8')
|
|
|
|
|
|
|
|
|
|
return jsonify(response), 200
|
2016-10-25 18:04:03 +01:00
|
|
|
|
|
|
|
|
|
2017-03-16 18:15:49 +00:00
|
|
|
@v2_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)
|
2016-12-07 14:09:56 +00:00
|
|
|
|
|
|
|
|
# flat=False makes everything a list, but we only ever allow one value for "older_than"
|
2016-11-28 14:22:51 +00:00
|
|
|
if 'older_than' in _data:
|
|
|
|
|
_data['older_than'] = _data['older_than'][0]
|
|
|
|
|
|
2016-12-07 14:09:56 +00:00
|
|
|
# and client reference
|
2016-12-12 18:04:20 +00:00
|
|
|
if 'reference' in _data:
|
|
|
|
|
_data['reference'] = _data['reference'][0]
|
2016-12-07 14:09:56 +00:00
|
|
|
|
2017-11-16 14:45:02 +00:00
|
|
|
if 'include_jobs' in _data:
|
|
|
|
|
_data['include_jobs'] = _data['include_jobs'][0]
|
|
|
|
|
|
2016-11-28 14:22:51 +00:00
|
|
|
data = validate(_data, get_notifications_request)
|
2016-11-23 11:44:38 +00:00
|
|
|
|
|
|
|
|
paginated_notifications = notifications_dao.get_notifications_for_service(
|
2017-05-05 15:23:06 +01:00
|
|
|
str(authenticated_service.id),
|
2016-11-23 11:44:38 +00:00
|
|
|
filter_dict=data,
|
|
|
|
|
key_type=api_user.key_type,
|
|
|
|
|
personalisation=True,
|
2016-12-12 18:04:20 +00:00
|
|
|
older_than=data.get('older_than'),
|
2017-01-20 12:26:55 +00:00
|
|
|
client_reference=data.get('reference'),
|
2017-11-16 14:45:02 +00:00
|
|
|
page_size=current_app.config.get('API_PAGE_SIZE'),
|
|
|
|
|
include_jobs=data.get('include_jobs')
|
2016-11-23 11:44:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
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
|