Merge branch 'master' into populate-monthly-letter-usages

This commit is contained in:
Rebecca Law
2017-12-19 13:28:00 +00:00
16 changed files with 285 additions and 36 deletions

0
app/__
View File

View File

@@ -203,6 +203,7 @@ def save_sms(self,
key_type=KEY_TYPE_NORMAL):
notification = encryption.decrypt(encrypted_notification)
service = dao_fetch_service_by_id(service_id)
template = dao_get_template_by_id(notification['template'], version=notification['template_version'])
if not service_allowed_to_send_to(notification['to'], service, key_type):
current_app.logger.info(
@@ -224,7 +225,7 @@ def save_sms(self,
job_id=notification.get('job', None),
job_row_number=notification.get('row_number', None),
notification_id=notification_id,
reply_to_text=service.get_default_sms_sender()
reply_to_text=template.get_reply_to_text()
)
provider_tasks.deliver_sms.apply_async(
@@ -252,7 +253,9 @@ def save_email(self,
api_key_id=None,
key_type=KEY_TYPE_NORMAL):
notification = encryption.decrypt(encrypted_notification)
service = dao_fetch_service_by_id(service_id)
template = dao_get_template_by_id(notification['template'], version=notification['template_version'])
if not service_allowed_to_send_to(notification['to'], service, key_type):
current_app.logger.info("Email {} failed as restricted service".format(notification_id))
@@ -272,7 +275,7 @@ def save_email(self,
job_id=notification.get('job', None),
job_row_number=notification.get('row_number', None),
notification_id=notification_id,
reply_to_text=service.get_default_reply_to_email_address()
reply_to_text=template.get_reply_to_text()
)
provider_tasks.deliver_email.apply_async(
@@ -299,6 +302,8 @@ def save_letter(
recipient = notification['personalisation']['addressline1']
service = dao_fetch_service_by_id(service_id)
template = dao_get_template_by_id(notification['template'], version=notification['template_version'])
try:
saved_notification = persist_notification(
template_id=notification['template'],
@@ -314,7 +319,7 @@ def save_letter(
job_row_number=notification['row_number'],
notification_id=notification_id,
reference=create_random_identifier(),
reply_to_text=service.get_default_letter_contact()
reply_to_text=template.get_reply_to_text()
)
if service.has_permission('letters_as_pdf'):

View File

@@ -42,7 +42,7 @@ def create_invited_user(service_id):
notification_type=EMAIL_TYPE,
api_key_id=None,
key_type=KEY_TYPE_NORMAL,
reply_to_text=service.get_default_reply_to_email_address()
reply_to_text=invited_user.from_user.email_address
)
send_notification_to_queue(saved_notification, False, queue=QueueNames.NOTIFY)

View File

@@ -607,6 +607,10 @@ class TemplateBase(db.Model):
def service_letter_contact_id(cls):
return db.Column(UUID(as_uuid=True), db.ForeignKey('service_letter_contacts.id'), nullable=True)
@declared_attr
def service_letter_contact(cls):
return db.relationship('ServiceLetterContact', viewonly=True)
@property
def reply_to(self):
if self.template_type == LETTER_TYPE:
@@ -623,6 +627,19 @@ class TemplateBase(db.Model):
else:
raise ValueError('Unable to set sender for {} template'.format(self.template_type))
def get_reply_to_text(self):
if self.template_type == LETTER_TYPE:
if self.service_letter_contact_id is not None:
return self.service_letter_contact.contact_block
else:
return self.service.get_default_letter_contact()
elif self.template_type == EMAIL_TYPE:
return self.service.get_default_reply_to_email_address()
elif self.template_type == SMS_TYPE:
return self.service.get_default_sms_sender()
else:
return None
def _as_utils_template(self):
if self.template_type == EMAIL_TYPE:
return PlainTextEmailTemplate(

View File

@@ -10,7 +10,7 @@ from notifications_utils.clients.redis import rate_limit_cache_key, daily_limit_
from app.dao import services_dao, templates_dao
from app.dao.service_sms_sender_dao import dao_get_service_sms_senders_by_id
from app.models import (
INTERNATIONAL_SMS_TYPE, SMS_TYPE, EMAIL_TYPE,
INTERNATIONAL_SMS_TYPE, SMS_TYPE, EMAIL_TYPE, LETTER_TYPE,
KEY_TYPE_TEST, KEY_TYPE_TEAM, SCHEDULE_NOTIFICATIONS
)
from app.service.utils import service_allowed_to_send_to
@@ -20,6 +20,7 @@ from app import redis_store
from app.notifications.process_notifications import create_content_for_notification
from app.utils import get_public_notify_type_text
from app.dao.service_email_reply_to_dao import dao_get_reply_to_by_id
from app.dao.service_letter_contact_dao import dao_get_letter_contact_by_id
def check_service_over_api_rate_limit(service, api_key):
@@ -141,6 +142,15 @@ def validate_template(template_id, personalisation, service, notification_type):
return template, template_with_content
def check_reply_to(service_id, reply_to_id, type_):
if type_ == EMAIL_TYPE:
return check_service_email_reply_to_id(service_id, reply_to_id, type_)
elif type_ == SMS_TYPE:
return check_service_sms_sender_id(service_id, reply_to_id, type_)
elif type_ == LETTER_TYPE:
return check_service_letter_contact_id(service_id, reply_to_id, type_)
def check_service_email_reply_to_id(service_id, reply_to_id, notification_type):
if reply_to_id:
try:
@@ -159,3 +169,13 @@ def check_service_sms_sender_id(service_id, sms_sender_id, notification_type):
message = 'sms_sender_id {} does not exist in database for service id {}'\
.format(sms_sender_id, service_id)
raise BadRequestError(message=message)
def check_service_letter_contact_id(service_id, letter_contact_id, notification_type):
if letter_contact_id:
try:
return dao_get_letter_contact_by_id(service_id, letter_contact_id).contact_block
except NoResultFound:
message = 'letter_contact_id {} does not exist in database for service id {}'\
.format(letter_contact_id, service_id)
raise BadRequestError(message=message)

View File

@@ -15,7 +15,6 @@ from app.models import (
PRIORITY,
SMS_TYPE,
EMAIL_TYPE,
LETTER_TYPE
)
from app.dao.services_dao import dao_fetch_service_by_id
from app.dao.templates_dao import dao_get_template_by_id_and_service_id
@@ -56,7 +55,12 @@ def send_one_off_notification(service_id, post_data):
validate_created_by(service, post_data['created_by'])
sender_id = post_data.get('sender_id', None)
reply_to = get_reply_to_text(notification_type=template.template_type, sender_id=sender_id, service=service)
reply_to = get_reply_to_text(
notification_type=template.template_type,
sender_id=sender_id,
service=service,
template=template
)
notification = persist_notification(
template_id=template.id,
template_version=template.version,
@@ -80,21 +84,14 @@ def send_one_off_notification(service_id, post_data):
return {'id': str(notification.id)}
def get_reply_to_text(notification_type, sender_id, service):
def get_reply_to_text(notification_type, sender_id, service, template):
reply_to = None
if notification_type == EMAIL_TYPE:
if sender_id:
if sender_id:
if notification_type == EMAIL_TYPE:
reply_to = dao_get_reply_to_by_id(service.id, sender_id).email_address
else:
service.get_default_reply_to_email_address()
elif notification_type == SMS_TYPE:
if sender_id:
elif notification_type == SMS_TYPE:
reply_to = dao_get_service_sms_senders_by_id(service.id, sender_id).sms_sender
else:
reply_to = service.get_default_sms_sender()
elif notification_type == LETTER_TYPE:
reply_to = service.get_default_letter_contact()
else:
reply_to = template.get_reply_to_text()
return reply_to

View File

@@ -16,7 +16,7 @@ from app.dao.templates_dao import (
from notifications_utils.template import SMSMessageTemplate
from app.dao.services_dao import dao_fetch_service_by_id
from app.models import SMS_TYPE
from app.notifications.validators import service_has_permission
from app.notifications.validators import service_has_permission, check_reply_to
from app.schemas import (template_schema, template_history_schema)
from app.errors import (
register_errors,
@@ -58,6 +58,8 @@ def create_template(service_id):
errors = {'content': [message]}
raise InvalidRequest(errors, status_code=400)
check_reply_to(service_id, new_template.reply_to, new_template.template_type)
dao_create_template(new_template)
return jsonify(data=template_schema.dump(new_template).data), 201
@@ -93,6 +95,9 @@ def update_template(service_id, template_id):
message = 'Content has a character count greater than the limit of {}'.format(char_count_limit)
errors = {'content': [message]}
raise InvalidRequest(errors, status_code=400)
check_reply_to(service_id, update_dict.reply_to, fetched_template.template_type)
dao_update_template(update_dict)
return jsonify(data=template_schema.dump(update_dict).data), 200

View File

@@ -70,8 +70,6 @@ def post_notification(notification_type):
check_rate_limiting(authenticated_service, api_user)
reply_to = get_reply_to_text(notification_type, form)
template, template_with_content = validate_template(
form['template_id'],
form.get('personalisation', {}),
@@ -79,11 +77,14 @@ def post_notification(notification_type):
notification_type,
)
reply_to = get_reply_to_text(notification_type, form, template)
if notification_type == LETTER_TYPE:
notification = process_letter_notification(
letter_data=form,
api_key=api_user,
template=template,
reply_to_text=reply_to
)
else:
notification = process_sms_or_email_notification(
@@ -164,7 +165,7 @@ def process_sms_or_email_notification(*, form, notification_type, api_key, templ
return notification
def process_letter_notification(*, letter_data, api_key, template):
def process_letter_notification(*, letter_data, api_key, template, reply_to_text):
if api_key.key_type == KEY_TYPE_TEAM:
raise BadRequestError(message='Cannot send letters with a team api key', status_code=403)
@@ -175,12 +176,11 @@ def process_letter_notification(*, letter_data, api_key, template):
# if we don't want to actually send the letter, then start it off in SENDING so we don't pick it up
status = NOTIFICATION_CREATED if should_send else NOTIFICATION_SENDING
letter_contact_block = api_key.service.get_default_letter_contact()
notification = create_letter_notification(letter_data=letter_data,
template=template,
api_key=api_key,
status=status,
reply_to_text=letter_contact_block)
reply_to_text=reply_to_text)
if not should_send:
update_letter_notifications_to_sent_to_dvla.apply_async(
@@ -198,21 +198,21 @@ def process_letter_notification(*, letter_data, api_key, template):
@statsd(namespace="performance-testing")
def get_reply_to_text(notification_type, form):
def get_reply_to_text(notification_type, form, template):
reply_to = None
if notification_type == EMAIL_TYPE:
service_email_reply_to_id = form.get("email_reply_to_id", None)
reply_to = check_service_email_reply_to_id(
str(authenticated_service.id), service_email_reply_to_id, notification_type
) or authenticated_service.get_default_reply_to_email_address()
) or template.get_reply_to_text()
elif notification_type == SMS_TYPE:
service_sms_sender_id = form.get("sms_sender_id", None)
reply_to = check_service_sms_sender_id(
str(authenticated_service.id), service_sms_sender_id, notification_type
) or authenticated_service.get_default_sms_sender()
) or template.get_reply_to_text()
elif notification_type == LETTER_TYPE:
reply_to = authenticated_service.get_default_letter_contact()
reply_to = template.get_reply_to_text()
return reply_to