mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 07:35:34 -05:00
Merge branch 'master' into capture-delivery-outcomes
Conflicts: tests/app/notifications/test_rest.py
This commit is contained in:
@@ -267,7 +267,8 @@ def send_email(service_id, notification_id, subject, from_address, encrypted_not
|
||||
from_address,
|
||||
notification['to'],
|
||||
subject,
|
||||
template.replaced
|
||||
body=template.replaced,
|
||||
html_body=template.as_HTML_email,
|
||||
)
|
||||
update_notification_reference_by_id(notification_id, reference)
|
||||
except AwsSesClientException as e:
|
||||
|
||||
@@ -52,6 +52,7 @@ class AwsSesClient(EmailClient):
|
||||
to_addresses,
|
||||
subject,
|
||||
body,
|
||||
html_body='',
|
||||
reply_to_addresses=None):
|
||||
try:
|
||||
if isinstance(to_addresses, str):
|
||||
@@ -61,6 +62,15 @@ class AwsSesClient(EmailClient):
|
||||
elif reply_to_addresses is None:
|
||||
reply_to_addresses = []
|
||||
|
||||
body = {
|
||||
'Text': {'Data': body}
|
||||
}
|
||||
|
||||
if html_body:
|
||||
body.update({
|
||||
'Html': {'Data': html_body}
|
||||
})
|
||||
|
||||
start_time = monotonic()
|
||||
response = self._client.send_email(
|
||||
Source=source,
|
||||
@@ -73,9 +83,7 @@ class AwsSesClient(EmailClient):
|
||||
'Subject': {
|
||||
'Data': subject,
|
||||
},
|
||||
'Body': {
|
||||
'Text': {
|
||||
'Data': body}}
|
||||
'Body': body
|
||||
},
|
||||
ReplyToAddresses=reply_to_addresses)
|
||||
elapsed_time = monotonic() - start_time
|
||||
|
||||
@@ -1,6 +1,12 @@
|
||||
from flask import current_app
|
||||
from app import db
|
||||
from app.models import Notification, Job, NotificationStatistics, TEMPLATE_TYPE_SMS, TEMPLATE_TYPE_EMAIL
|
||||
from app.models import (
|
||||
Notification,
|
||||
Job,
|
||||
NotificationStatistics,
|
||||
TEMPLATE_TYPE_SMS,
|
||||
TEMPLATE_TYPE_EMAIL,
|
||||
Template)
|
||||
from sqlalchemy import desc
|
||||
from datetime import datetime, timedelta
|
||||
from app.clients import (STATISTICS_FAILURE, STATISTICS_DELIVERED, STATISTICS_REQUESTED)
|
||||
@@ -131,14 +137,14 @@ def get_notification_for_job(service_id, job_id, notification_id):
|
||||
return Notification.query.filter_by(service_id=service_id, job_id=job_id, id=notification_id).one()
|
||||
|
||||
|
||||
def get_notifications_for_job(service_id, job_id, page=1):
|
||||
query = Notification.query.filter_by(service_id=service_id, job_id=job_id) \
|
||||
.order_by(desc(Notification.created_at)) \
|
||||
.paginate(
|
||||
def get_notifications_for_job(service_id, job_id, filter_dict=None, page=1):
|
||||
query = Notification.query.filter_by(service_id=service_id, job_id=job_id)
|
||||
query = filter_query(query, filter_dict)
|
||||
pagination = query.order_by(desc(Notification.created_at)).paginate(
|
||||
page=page,
|
||||
per_page=current_app.config['PAGE_SIZE']
|
||||
)
|
||||
return query
|
||||
return pagination
|
||||
|
||||
|
||||
def get_notification(service_id, notification_id):
|
||||
@@ -149,11 +155,21 @@ def get_notification_by_id(notification_id):
|
||||
return Notification.query.filter_by(id=notification_id).first()
|
||||
|
||||
|
||||
def get_notifications_for_service(service_id, page=1):
|
||||
query = Notification.query.filter_by(service_id=service_id).order_by(desc(Notification.created_at)).paginate(
|
||||
def get_notifications_for_service(service_id, filter_dict=None, page=1):
|
||||
query = Notification.query.filter_by(service_id=service_id)
|
||||
query = filter_query(query, filter_dict)
|
||||
pagination = query.order_by(desc(Notification.created_at)).paginate(
|
||||
page=page,
|
||||
per_page=current_app.config['PAGE_SIZE']
|
||||
)
|
||||
return pagination
|
||||
|
||||
|
||||
def filter_query(query, filter_dict=None):
|
||||
if filter_dict and 'status' in filter_dict:
|
||||
query = query.filter_by(status=filter_dict['status'])
|
||||
if filter_dict and 'template_type' in filter_dict:
|
||||
query = query.join(Template).filter(Template.template_type == filter_dict['template_type'])
|
||||
return query
|
||||
|
||||
|
||||
|
||||
@@ -23,7 +23,9 @@ from app.dao import (
|
||||
from app.schemas import (
|
||||
email_notification_schema,
|
||||
sms_template_notification_schema,
|
||||
notification_status_schema
|
||||
notification_status_schema,
|
||||
template_schema,
|
||||
notifications_filter_schema
|
||||
)
|
||||
from app.celery.tasks import send_sms, send_email
|
||||
from app.validation import allowed_send_to_number, allowed_send_to_email
|
||||
@@ -218,16 +220,20 @@ def get_notifications(notification_id):
|
||||
|
||||
@notifications.route('/notifications', methods=['GET'])
|
||||
def get_all_notifications():
|
||||
page = get_page_from_request()
|
||||
data, errors = notifications_filter_schema.load(request.args)
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
|
||||
if not page:
|
||||
return jsonify(result="error", message="Invalid page"), 400
|
||||
page = data['page'] if 'page' in data else 1
|
||||
|
||||
all_notifications = notifications_dao.get_notifications_for_service(api_user['client'], page)
|
||||
pagination = notifications_dao.get_notifications_for_service(
|
||||
api_user['client'],
|
||||
filter_dict=data,
|
||||
page=page)
|
||||
return jsonify(
|
||||
notifications=notification_status_schema.dump(all_notifications.items, many=True).data,
|
||||
notifications=notification_status_schema.dump(pagination.items, many=True).data,
|
||||
links=pagination_links(
|
||||
all_notifications,
|
||||
pagination,
|
||||
'.get_all_notifications',
|
||||
**request.args.to_dict()
|
||||
)
|
||||
@@ -237,18 +243,22 @@ def get_all_notifications():
|
||||
@notifications.route('/service/<service_id>/notifications', methods=['GET'])
|
||||
@require_admin()
|
||||
def get_all_notifications_for_service(service_id):
|
||||
page = get_page_from_request()
|
||||
data, errors = notifications_filter_schema.load(request.args)
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
|
||||
if not page:
|
||||
return jsonify(result="error", message="Invalid page"), 400
|
||||
page = data['page'] if 'page' in data else 1
|
||||
|
||||
all_notifications = notifications_dao.get_notifications_for_service(service_id, page)
|
||||
pagination = notifications_dao.get_notifications_for_service(
|
||||
service_id,
|
||||
filter_dict=data,
|
||||
page=page)
|
||||
kwargs = request.args.to_dict()
|
||||
kwargs['service_id'] = service_id
|
||||
return jsonify(
|
||||
notifications=notification_status_schema.dump(all_notifications.items, many=True).data,
|
||||
notifications=notification_status_schema.dump(pagination.items, many=True).data,
|
||||
links=pagination_links(
|
||||
all_notifications,
|
||||
pagination,
|
||||
'.get_all_notifications_for_service',
|
||||
**kwargs
|
||||
)
|
||||
@@ -258,36 +268,30 @@ def get_all_notifications_for_service(service_id):
|
||||
@notifications.route('/service/<service_id>/job/<job_id>/notifications', methods=['GET'])
|
||||
@require_admin()
|
||||
def get_all_notifications_for_service_job(service_id, job_id):
|
||||
page = get_page_from_request()
|
||||
data, errors = notifications_filter_schema.load(request.args)
|
||||
if errors:
|
||||
return jsonify(result="error", message=errors), 400
|
||||
|
||||
if not page:
|
||||
return jsonify(result="error", message="Invalid page"), 400
|
||||
page = data['page'] if 'page' in data else 1
|
||||
|
||||
all_notifications = notifications_dao.get_notifications_for_job(service_id, job_id, page)
|
||||
pagination = notifications_dao.get_notifications_for_job(
|
||||
service_id,
|
||||
job_id,
|
||||
filter_dict=data,
|
||||
page=page)
|
||||
kwargs = request.args.to_dict()
|
||||
kwargs['service_id'] = service_id
|
||||
kwargs['job_id'] = job_id
|
||||
return jsonify(
|
||||
notifications=notification_status_schema.dump(all_notifications.items, many=True).data,
|
||||
notifications=notification_status_schema.dump(pagination.items, many=True).data,
|
||||
links=pagination_links(
|
||||
all_notifications,
|
||||
pagination,
|
||||
'.get_all_notifications_for_service_job',
|
||||
**kwargs
|
||||
)
|
||||
), 200
|
||||
|
||||
|
||||
def get_page_from_request():
|
||||
if 'page' in request.args:
|
||||
try:
|
||||
return int(request.args['page'])
|
||||
|
||||
except ValueError:
|
||||
return None
|
||||
else:
|
||||
return 1
|
||||
|
||||
|
||||
def pagination_links(pagination, endpoint, **kwargs):
|
||||
if 'page' in kwargs:
|
||||
kwargs.pop('page', None)
|
||||
|
||||
@@ -218,6 +218,21 @@ class EmailDataSchema(ma.Schema):
|
||||
except InvalidEmailError as e:
|
||||
raise ValidationError(e.message)
|
||||
|
||||
|
||||
class NotificationsFilterSchema(ma.Schema):
|
||||
template_type = field_for(models.Template, 'template_type', load_only=True, required=False)
|
||||
status = field_for(models.Notification, 'status', load_only=True, required=False)
|
||||
page = fields.Int(required=False)
|
||||
|
||||
@validates('page')
|
||||
def validate_page(self, value):
|
||||
try:
|
||||
page_int = int(value)
|
||||
if page_int < 1:
|
||||
raise ValidationError("Not a positive integer")
|
||||
except:
|
||||
raise ValidationError("Not a positive integer")
|
||||
|
||||
user_schema = UserSchema()
|
||||
user_schema_load_json = UserSchema(load_json=True)
|
||||
service_schema = ServiceSchema()
|
||||
@@ -240,3 +255,4 @@ invited_user_schema = InvitedUserSchema()
|
||||
permission_schema = PermissionSchema()
|
||||
email_data_request_schema = EmailDataSchema()
|
||||
notifications_statistics_schema = NotificationsStatisticsSchema()
|
||||
notifications_filter_schema = NotificationsFilterSchema()
|
||||
|
||||
Reference in New Issue
Block a user