Skip to content

Commit

Permalink
Merge branch 'master' into fix-billable-units-query
Browse files Browse the repository at this point in the history
Conflicts:
	tests/app/dao/test_notification_dao.py
  • Loading branch information
Rebecca Law committed Jan 10, 2017
2 parents ffa5482 + 46d1e3b commit cefa00b
Show file tree
Hide file tree
Showing 9 changed files with 162 additions and 54 deletions.
4 changes: 2 additions & 2 deletions app/celery/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,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 @@ -195,7 +195,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 @@ -129,9 +129,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 @@ -188,9 +196,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
5 changes: 3 additions & 2 deletions app/invite/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
get_invited_users_for_service
)
from app.dao.templates_dao import dao_get_template_by_id
from app.models import EMAIL_TYPE, KEY_TYPE_NORMAL
from app.models import EMAIL_TYPE, KEY_TYPE_NORMAL, Service
from app.notifications.process_notifications import persist_notification, send_notification_to_queue
from app.schemas import invited_user_schema

Expand All @@ -27,12 +27,13 @@ def create_invited_user(service_id):
save_invited_user(invited_user)

template = dao_get_template_by_id(current_app.config['INVITATION_EMAIL_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=invited_user.email_address,
service_id=current_app.config['NOTIFY_SERVICE_ID'],
service=service,
personalisation={
'user_name': invited_user.from_user.name,
'service_name': invited_user.service.name,
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
20 changes: 11 additions & 9 deletions app/user/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,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 @@ -139,13 +139,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 @@ -167,13 +167,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 @@ -195,12 +195,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 All @@ -219,12 +220,13 @@ def send_user_email_verification(user_id):
def send_already_registered_email(user_id):
to, errors = email_data_request_schema.load(request.get_json())
template = dao_get_template_by_id(current_app.config['ALREADY_REGISTERED_EMAIL_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=to['email'],
service_id=current_app.config['NOTIFY_SERVICE_ID'],
service=service,
personalisation={
'signin_url': current_app.config['ADMIN_BASE_URL'] + '/sign-in',
'forgot_password_url': current_app.config['ADMIN_BASE_URL'] + '/forgot-password',
Expand Down Expand Up @@ -282,12 +284,12 @@ def send_user_reset_password():
user_to_send_to = get_user_by_email(email['email'])

template = dao_get_template_by_id(current_app.config['PASSWORD_RESET_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=email['email'],
service_id=current_app.config['NOTIFY_SERVICE_ID'],
service=service,
personalisation={
'user_name': user_to_send_to.name,
'url': _create_reset_password_url(user_to_send_to.email_address)
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 Down Expand Up @@ -61,7 +61,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 cefa00b

Please sign in to comment.