2019-09-12 17:06:22 +01:00
|
|
|
from io import BytesIO
|
2019-09-03 16:49:03 +01:00
|
|
|
|
2019-09-12 17:06:22 +01:00
|
|
|
from flask import jsonify, request, url_for, current_app, send_file
|
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
|
2019-09-12 17:06:22 +01:00
|
|
|
from app.v2.errors import BadRequestError
|
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-12 17:06:22 +01:00
|
|
|
from app.models import (
|
|
|
|
|
NOTIFICATION_PENDING_VIRUS_CHECK,
|
|
|
|
|
NOTIFICATION_VIRUS_SCAN_FAILED,
|
|
|
|
|
NOTIFICATION_TECHNICAL_FAILURE,
|
|
|
|
|
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-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:
|
|
|
|
|
raise BadRequestError(message="Notification is not a letter", status_code=400)
|
|
|
|
|
|
|
|
|
|
if notification.status in {
|
|
|
|
|
NOTIFICATION_PENDING_VIRUS_CHECK,
|
|
|
|
|
NOTIFICATION_VIRUS_SCAN_FAILED,
|
|
|
|
|
NOTIFICATION_TECHNICAL_FAILURE,
|
|
|
|
|
}:
|
|
|
|
|
raise BadRequestError(
|
|
|
|
|
message='PDF not available for letters in status {}'.format(notification.status),
|
|
|
|
|
status_code=400
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
try:
|
|
|
|
|
pdf_data = get_letter_pdf(notification)
|
|
|
|
|
except Exception:
|
|
|
|
|
# this probably means it's a templated letter that hasn't been created yet
|
|
|
|
|
current_app.logger.info('PDF not found for notification id {} status {}'.format(
|
|
|
|
|
notification.id,
|
|
|
|
|
notification.status
|
|
|
|
|
))
|
|
|
|
|
raise BadRequestError(
|
|
|
|
|
message='PDF not available for letter, try again later',
|
|
|
|
|
status_code=400)
|
2019-09-03 16:49:03 +01:00
|
|
|
|
2019-09-12 17:06:22 +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
|