Merge branch 'master' into schedule-api-notification

Conflicts:
	app/celery/scheduled_tasks.py
	app/v2/notifications/post_notifications.py
	tests/app/celery/test_scheduled_tasks.py
This commit is contained in:
Rebecca Law
2017-05-22 14:05:57 +01:00
48 changed files with 1917 additions and 291 deletions

View File

@@ -1,3 +1,5 @@
import json
from functools import wraps
from flask import (
@@ -35,7 +37,7 @@ def validate_schema(schema):
def decorator(f):
@wraps(f)
def wrapper(*args, **kw):
validate(request.json, schema)
validate(request.get_json(force=True), schema)
return f(*args, **kw)
return wrapper
return decorator
@@ -44,10 +46,12 @@ def validate_schema(schema):
@letter_callback_blueprint.route('/notifications/letter/dvla', methods=['POST'])
@validate_schema(dvla_sns_callback_schema)
def process_letter_response():
req_json = request.json
req_json = request.get_json(force=True)
current_app.logger.info('Received SNS callback: {}'.format(req_json))
if not autoconfirm_subscription(req_json):
# The callback should have one record for an S3 Put Event.
filename = req_json['Message']['Records'][0]['s3']['object']['key']
message = json.loads(req_json['Message'])
filename = message['Records'][0]['s3']['object']['key']
current_app.logger.info('Received file from DVLA: {}'.format(filename))
current_app.logger.info('DVLA callback: Calling task to update letter notifications')
update_letter_notifications_statuses.apply_async([filename], queue='notify')

View File

@@ -13,6 +13,7 @@ from app.clients.email.aws_ses import get_aws_responses
from app.dao import (
notifications_dao
)
from app.celery.statistics_tasks import create_outcome_notification_statistic_tasks
from app.notifications.process_client_response import validate_callback_data
from app.notifications.utils import autoconfirm_subscription
@@ -92,6 +93,9 @@ def process_ses_response():
datetime.utcnow(),
notification.sent_at
)
create_outcome_notification_statistic_tasks(notification)
return jsonify(
result="success", message="SES callback succeeded"
), 200

View File

@@ -7,6 +7,8 @@ from app import statsd_client
from app.dao import notifications_dao
from app.clients.sms.firetext import get_firetext_responses
from app.clients.sms.mmg import get_mmg_responses
from app.celery.statistics_tasks import create_outcome_notification_statistic_tasks
sms_response_mapper = {
'MMG': get_mmg_responses,
@@ -45,8 +47,9 @@ def process_sms_client_response(status, reference, client_name):
# validate status
try:
response_dict = response_parser(status)
current_app.logger.info('{} callback return status of {} for reference: {}'.format(client_name,
status, reference))
current_app.logger.info('{} callback return status of {} for reference: {}'.format(
client_name, status, reference)
)
except KeyError:
msg = "{} callback failed: status {} not found.".format(client_name, status)
return success, msg
@@ -77,5 +80,8 @@ def process_sms_client_response(status, reference, client_name):
datetime.utcnow(),
notification.sent_at
)
create_outcome_notification_statistic_tasks(notification)
success = "{} callback succeeded. reference {} updated".format(client_name, reference)
return success, errors

View File

@@ -48,6 +48,9 @@ def persist_notification(
notification_id=None,
simulated=False
):
notification_created_at = created_at or datetime.utcnow()
notification = Notification(
id=notification_id,
template_id=template_id,
@@ -59,7 +62,7 @@ def persist_notification(
notification_type=notification_type,
api_key_id=api_key_id,
key_type=key_type,
created_at=created_at or datetime.utcnow(),
created_at=notification_created_at,
job_id=job_id,
job_row_number=job_row_number,
client_reference=client_reference,
@@ -82,7 +85,7 @@ def persist_notification(
if redis_store.get_all_from_hash(cache_key_for_service_template_counter(service.id)):
redis_store.increment_hash_value(cache_key_for_service_template_counter(service.id), template_id)
current_app.logger.info(
"{} {} created at {}".format(notification.notification_type, notification.id, notification.created_at)
"{} {} created at {}".format(notification_type, notification_id, notification_created_at)
)
return notification

View File

@@ -5,20 +5,23 @@ from flask import (
current_app
)
from app import api_user
from app import api_user, authenticated_service
from app.dao import (
templates_dao,
services_dao,
notifications_dao
)
from app.models import KEY_TYPE_TEAM, PRIORITY
from app.models import SMS_TYPE
from app.notifications.process_notifications import (persist_notification,
send_notification_to_queue,
simulated_recipient)
from app.notifications.validators import (check_service_over_daily_message_limit,
check_template_is_for_notification_type,
check_template_is_active, check_rate_limiting)
from app.notifications.process_notifications import (
persist_notification,
send_notification_to_queue,
simulated_recipient
)
from app.notifications.validators import (
check_template_is_for_notification_type,
check_template_is_active,
check_rate_limiting
)
from app.schemas import (
email_notification_schema,
sms_template_notification_schema,
@@ -45,9 +48,10 @@ register_errors(notifications)
@notifications.route('/notifications/<uuid:notification_id>', methods=['GET'])
def get_notification_by_id(notification_id):
notification = notifications_dao.get_notification_with_personalisation(str(api_user.service_id),
notification_id,
key_type=None)
notification = notifications_dao.get_notification_with_personalisation(
str(authenticated_service.id),
notification_id,
key_type=None)
return jsonify(data={"notification": notification_with_personalisation_schema.dump(notification).data}), 200
@@ -60,7 +64,7 @@ def get_all_notifications():
limit_days = data.get('limit_days')
pagination = notifications_dao.get_notifications_for_service(
str(api_user.service_id),
str(authenticated_service.id),
personalisation=True,
filter_dict=data,
page=page,
@@ -96,8 +100,6 @@ def send_notification(notification_type):
if notification_type not in ['sms', 'email']:
assert False
service = services_dao.dao_fetch_service_by_id(api_user.service_id)
notification_form, errors = (
sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema
).load(request.get_json())
@@ -105,27 +107,27 @@ def send_notification(notification_type):
if errors:
raise InvalidRequest(errors, status_code=400)
check_rate_limiting(service, api_user)
check_rate_limiting(authenticated_service, api_user)
template = templates_dao.dao_get_template_by_id_and_service_id(
template_id=notification_form['template'],
service_id=service.id)
service_id=authenticated_service.id)
check_template_is_for_notification_type(notification_type, template.template_type)
check_template_is_active(template)
template_object = create_template_object_for_notification(template, notification_form.get('personalisation', {}))
_service_allowed_to_send_to(notification_form, service)
_service_allowed_to_send_to(notification_form, authenticated_service)
if notification_type == SMS_TYPE:
_service_can_send_internationally(service, notification_form['to'])
_service_can_send_internationally(authenticated_service, notification_form['to'])
# Do not persist or send notification to the queue if it is a simulated recipient
simulated = simulated_recipient(notification_form['to'], notification_type)
notification_model = persist_notification(template_id=template.id,
template_version=template.version,
recipient=request.get_json()['to'],
service=service,
service=authenticated_service,
personalisation=notification_form.get('personalisation', None),
notification_type=notification_type,
api_key_id=api_user.id,
@@ -134,7 +136,7 @@ def send_notification(notification_type):
if not simulated:
queue_name = 'priority' if template.process_type == PRIORITY else None
send_notification_to_queue(notification=notification_model,
research_mode=service.research_mode,
research_mode=authenticated_service.research_mode,
queue=queue_name)
else:
current_app.logger.info("POST simulated notification for id: {}".format(notification_model.id))

View File

@@ -10,12 +10,12 @@ from app.models import KEY_TYPE_TEST, KEY_TYPE_TEAM, SMS_TYPE
from app.service.utils import service_allowed_to_send_to
from app.v2.errors import TooManyRequestsError, BadRequestError, RateLimitError
from app import redis_store
from notifications_utils.clients import redis
from notifications_utils.clients.redis import rate_limit_cache_key, daily_limit_cache_key
def check_service_over_api_rate_limit(service, api_key):
if current_app.config['API_RATE_LIMIT_ENABLED']:
cache_key = redis.rate_limit_cache_key(service.id, api_key.key_type)
cache_key = rate_limit_cache_key(service.id, api_key.key_type)
rate_limit = current_app.config['API_KEY_LIMITS'][api_key.key_type]['limit']
interval = current_app.config['API_KEY_LIMITS'][api_key.key_type]['interval']
if redis_store.exceeded_rate_limit(cache_key, rate_limit, interval):
@@ -25,7 +25,7 @@ def check_service_over_api_rate_limit(service, api_key):
def check_service_over_daily_message_limit(key_type, service):
if key_type != KEY_TYPE_TEST:
cache_key = redis.daily_limit_cache_key(service.id)
cache_key = daily_limit_cache_key(service.id)
service_stats = redis_store.get(cache_key)
if not service_stats:
service_stats = services_dao.fetch_todays_total_message_count(service.id)