2019-09-12 17:06:22 +01:00
|
|
|
from io import BytesIO
|
2019-09-03 16:49:03 +01:00
|
|
|
|
2021-03-10 13:55:06 +00:00
|
|
|
from flask import current_app, jsonify, request, send_file, url_for
|
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-10-29 16:19:50 +00:00
|
|
|
from app.letters.utils import get_letter_pdf_and_metadata
|
2019-09-12 17:06:22 +01:00
|
|
|
from app.models import (
|
2021-03-10 13:55:06 +00:00
|
|
|
LETTER_TYPE,
|
2019-09-12 17:06:22 +01:00
|
|
|
NOTIFICATION_PENDING_VIRUS_CHECK,
|
|
|
|
|
NOTIFICATION_TECHNICAL_FAILURE,
|
2021-03-10 13:55:06 +00:00
|
|
|
NOTIFICATION_VIRUS_SCAN_FAILED,
|
|
|
|
|
)
|
|
|
|
|
from app.schema_validation import validate
|
|
|
|
|
from app.v2.errors import BadRequestError, PDFNotReadyError
|
|
|
|
|
from app.v2.notifications import v2_notification_blueprint
|
|
|
|
|
from app.v2.notifications.notification_schemas import (
|
|
|
|
|
get_notifications_request,
|
|
|
|
|
notification_by_id,
|
2019-09-12 17:06:22 +01:00
|
|
|
)
|
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-12 17:06:22 +01:00
|
|
|
return jsonify(notification.serialize()), 200
|
2016-11-18 17:36:11 +00:00
|
|
|
|
2019-09-03 16:49:03 +01:00
|
|
|
|
2019-09-12 17:06:22 +01:00
|
|
|
@v2_notification_blueprint.route('/<notification_id>/pdf', methods=['GET'])
|
|
|
|
|
def get_pdf_for_notification(notification_id):
|
|
|
|
|
_data = {"notification_id": notification_id}
|
|
|
|
|
validate(_data, notification_by_id)
|
|
|
|
|
notification = notifications_dao.get_notification_by_id(
|
|
|
|
|
notification_id, authenticated_service.id, _raise=True
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
if notification.notification_type != LETTER_TYPE:
|
2019-09-17 12:16:24 +01:00
|
|
|
raise BadRequestError(message="Notification is not a letter")
|
|
|
|
|
|
|
|
|
|
if notification.status == NOTIFICATION_VIRUS_SCAN_FAILED:
|
2020-01-16 12:11:56 +00:00
|
|
|
raise BadRequestError(message='File did not pass the virus scan')
|
2019-09-17 12:16:24 +01:00
|
|
|
|
|
|
|
|
if notification.status == NOTIFICATION_TECHNICAL_FAILURE:
|
|
|
|
|
raise BadRequestError(message='PDF not available for letters in status {}'.format(notification.status))
|
|
|
|
|
|
|
|
|
|
if notification.status == NOTIFICATION_PENDING_VIRUS_CHECK:
|
|
|
|
|
raise PDFNotReadyError()
|
2019-09-12 17:06:22 +01:00
|
|
|
|
|
|
|
|
try:
|
2019-10-29 16:19:50 +00:00
|
|
|
pdf_data, metadata = get_letter_pdf_and_metadata(notification)
|
2019-09-12 17:06:22 +01:00
|
|
|
except Exception:
|
2019-09-17 12:16:24 +01:00
|
|
|
raise PDFNotReadyError()
|
2019-09-03 16:49:03 +01:00
|
|
|
|
2021-06-01 10:53:28 +01:00
|
|
|
return send_file(filename_or_fp=BytesIO(pdf_data), mimetype='application/pdf')
|
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
|