Skip to content

Commit

Permalink
Merge c6df84a into 6bf0245
Browse files Browse the repository at this point in the history
  • Loading branch information
minglis committed Dec 21, 2016
2 parents 6bf0245 + c6df84a commit cdeb79a
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 63 deletions.
4 changes: 2 additions & 2 deletions app/celery/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ def send_sms(self,
saved_notification = persist_notification(template_id=notification['template'],
template_version=notification['template_version'],
recipient=notification['to'],
service_id=service.id,
service=service,
personalisation=notification.get('personalisation'),
notification_type=SMS_TYPE,
api_key_id=api_key_id,
Expand Down Expand Up @@ -194,7 +194,7 @@ def send_email(self, service_id,
template_id=notification['template'],
template_version=notification['template_version'],
recipient=notification['to'],
service_id=service.id,
service=service,
personalisation=notification.get('personalisation'),
notification_type=EMAIL_TYPE,
api_key_id=api_key_id,
Expand Down
18 changes: 14 additions & 4 deletions app/dao/notifications_dao.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,9 +130,17 @@ def dao_create_notification(notification):
if not notification.status:
notification.status = 'created'

notification_history = NotificationHistory.from_original(notification)
db.session.add(notification)
db.session.add(notification_history)
if _should_record_notification_in_history_table(notification):
db.session.add(NotificationHistory.from_original(notification))


def _should_record_notification_in_history_table(notification):
if notification.api_key_id and notification.key_type == KEY_TYPE_TEST:
return False
if notification.service.research_mode:
return False
return True


def _decide_permanent_temporary_failure(current_status, status):
Expand Down Expand Up @@ -189,9 +197,11 @@ def update_notification_status_by_reference(reference, status):
@statsd(namespace="dao")
def dao_update_notification(notification):
notification.updated_at = datetime.utcnow()
notification_history = NotificationHistory.query.get(notification.id)
notification_history.update_from_original(notification)
db.session.add(notification)
if _should_record_notification_in_history_table(notification):
notification_history = NotificationHistory.query.get(notification.id)
notification_history.update_from_original(notification)
db.session.add(notification_history)
db.session.commit()


Expand Down
7 changes: 4 additions & 3 deletions app/notifications/process_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def check_placeholders(template_object):
def persist_notification(template_id,
template_version,
recipient,
service_id,
service,
personalisation,
notification_type,
api_key_id,
Expand All @@ -47,7 +47,8 @@ def persist_notification(template_id,
template_id=template_id,
template_version=template_version,
to=recipient,
service_id=service_id,
service_id=service.id,
service=service,
personalisation=personalisation,
notification_type=notification_type,
api_key_id=api_key_id,
Expand All @@ -58,7 +59,7 @@ def persist_notification(template_id,
client_reference=reference
)
dao_create_notification(notification)
redis_store.incr(redis.daily_limit_cache_key(service_id))
redis_store.incr(redis.daily_limit_cache_key(service.id))
return notification


Expand Down
2 changes: 1 addition & 1 deletion app/notifications/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ def send_notification(notification_type):
saved_notification = persist_notification(template_id=template.id,
template_version=template.version,
recipient=notification['to'],
service_id=service.id,
service=service,
personalisation=notification.get('personalisation', None),
notification_type=notification_type,
api_key_id=api_user.id,
Expand Down
13 changes: 7 additions & 6 deletions app/user/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
from app.dao.permissions_dao import permission_dao
from app.dao.services_dao import dao_fetch_service_by_id
from app.dao.templates_dao import dao_get_template_by_id
from app.models import SMS_TYPE, KEY_TYPE_NORMAL, EMAIL_TYPE
from app.models import SMS_TYPE, KEY_TYPE_NORMAL, EMAIL_TYPE, Service
from app.notifications.process_notifications import (
persist_notification,
send_notification_to_queue
Expand Down Expand Up @@ -146,13 +146,13 @@ def send_user_sms_code(user_id):
mobile = user_to_send_to.mobile_number if verify_code.get('to', None) is None else verify_code.get('to')
sms_code_template_id = current_app.config['SMS_CODE_TEMPLATE_ID']
sms_code_template = dao_get_template_by_id(sms_code_template_id)
notify_service_id = current_app.config['NOTIFY_SERVICE_ID']
service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID'])

saved_notification = persist_notification(
template_id=sms_code_template_id,
template_version=sms_code_template.version,
recipient=mobile,
service_id=notify_service_id,
service=service,
personalisation={'verify_code': secret_code},
notification_type=SMS_TYPE,
api_key_id=None,
Expand All @@ -174,13 +174,13 @@ def send_user_confirm_new_email(user_id):
raise InvalidRequest(message=errors, status_code=400)

template = dao_get_template_by_id(current_app.config['CHANGE_EMAIL_CONFIRMATION_TEMPLATE_ID'])
notify_service_id = current_app.config['NOTIFY_SERVICE_ID']
service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID'])

saved_notification = persist_notification(
template_id=template.id,
template_version=template.version,
recipient=email['email'],
service_id=notify_service_id,
service=service,
personalisation={
'name': user_to_send_to.name,
'url': _create_confirmation_url(user=user_to_send_to, email_address=email['email']),
Expand All @@ -202,12 +202,13 @@ def send_user_email_verification(user_id):
create_user_code(user_to_send_to, secret_code, 'email')

template = dao_get_template_by_id(current_app.config['EMAIL_VERIFY_CODE_TEMPLATE_ID'])
service = Service.query.get(current_app.config['NOTIFY_SERVICE_ID'])

saved_notification = persist_notification(
template_id=template.id,
template_version=template.version,
recipient=user_to_send_to.email_address,
service_id=current_app.config['NOTIFY_SERVICE_ID'],
service=service,
personalisation={
'name': user_to_send_to.name,
'url': _create_verification_url(user_to_send_to, secret_code)
Expand Down
4 changes: 2 additions & 2 deletions app/v2/notifications/post_notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def post_sms_notification():
notification = persist_notification(template_id=template.id,
template_version=template.version,
recipient=form['phone_number'],
service_id=service.id,
service=service,
personalisation=form.get('personalisation', None),
notification_type=SMS_TYPE,
api_key_id=api_user.id,
Expand All @@ -60,7 +60,7 @@ def post_email_notification():
notification = persist_notification(template_id=template.id,
template_version=template.version,
recipient=form['email_address'],
service_id=service.id,
service=service,
personalisation=form.get('personalisation', None),
notification_type=EMAIL_TYPE,
api_key_id=api_user.id,
Expand Down

0 comments on commit cdeb79a

Please sign in to comment.