mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-16 20:48:37 -04:00
Merge branch 'master' into vb-remove-ip-whitelist
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from datetime import datetime, timedelta, time
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from flask import current_app
|
||||
|
||||
@@ -85,7 +85,7 @@ def upload_letters_pdf(reference, crown, filedata):
|
||||
now = datetime.utcnow()
|
||||
|
||||
print_datetime = now
|
||||
if now.time() > time(17, 30):
|
||||
if now.time() > current_app.config.get('LETTER_PROCESSING_DEADLINE'):
|
||||
print_datetime = now + timedelta(days=1)
|
||||
|
||||
upload_file_name = LETTERS_PDF_FILE_LOCATION_STRUCTURE.format(
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
from datetime import (
|
||||
date,
|
||||
datetime,
|
||||
timedelta
|
||||
)
|
||||
@@ -33,6 +34,7 @@ from app.dao.notifications_dao import (
|
||||
dao_timeout_notifications,
|
||||
is_delivery_slow_for_provider,
|
||||
delete_notifications_created_more_than_a_week_ago_by_type,
|
||||
dao_get_count_of_letters_to_process_for_date,
|
||||
dao_get_scheduled_notifications,
|
||||
set_scheduled_notification_to_processed,
|
||||
dao_set_created_live_letter_api_notifications_to_pending,
|
||||
@@ -355,6 +357,20 @@ def run_letter_jobs():
|
||||
current_app.logger.info("Queued {} ready letter job ids onto {}".format(len(job_ids), QueueNames.PROCESS_FTP))
|
||||
|
||||
|
||||
@notify_celery.task(name="trigger-letter-pdfs-for-day")
|
||||
@statsd(namespace="tasks")
|
||||
def trigger_letter_pdfs_for_day():
|
||||
letter_pdfs_count = dao_get_count_of_letters_to_process_for_date()
|
||||
if letter_pdfs_count:
|
||||
notify_celery.send_task(
|
||||
name='collate-letter-pdfs-for-day',
|
||||
args=(date.today().strftime("%Y-%m-%d"),),
|
||||
queue=QueueNames.LETTERS
|
||||
)
|
||||
current_app.logger.info("{} letter pdfs to be process by {} task".format(
|
||||
letter_pdfs_count, 'collate-letter-pdfs-for-day'))
|
||||
|
||||
|
||||
@notify_celery.task(name="run-letter-api-notifications")
|
||||
@statsd(namespace="tasks")
|
||||
def run_letter_api_notifications():
|
||||
|
||||
@@ -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'):
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
from datetime import timedelta
|
||||
from datetime import timedelta, time
|
||||
import os
|
||||
import json
|
||||
|
||||
@@ -32,6 +32,7 @@ class QueueNames(object):
|
||||
PROCESS_FTP = 'process-ftp-tasks'
|
||||
CREATE_LETTERS_PDF = 'create-letters-pdf-tasks'
|
||||
CALLBACKS = 'service-callbacks'
|
||||
LETTERS = 'letter-tasks'
|
||||
|
||||
@staticmethod
|
||||
def all_queues():
|
||||
@@ -48,6 +49,7 @@ class QueueNames(object):
|
||||
QueueNames.NOTIFY,
|
||||
QueueNames.CREATE_LETTERS_PDF,
|
||||
QueueNames.CALLBACKS,
|
||||
QueueNames.LETTERS,
|
||||
]
|
||||
|
||||
|
||||
@@ -238,6 +240,11 @@ class Config(object):
|
||||
'schedule': crontab(hour=17, minute=30),
|
||||
'options': {'queue': QueueNames.PERIODIC}
|
||||
},
|
||||
'trigger-letter-pdfs-for-day': {
|
||||
'task': 'trigger-letter-pdfs-for-day',
|
||||
'schedule': crontab(hour=17, minute=50),
|
||||
'options': {'queue': QueueNames.PERIODIC}
|
||||
},
|
||||
'run-letter-api-notifications': {
|
||||
'task': 'run-letter-api-notifications',
|
||||
'schedule': crontab(hour=17, minute=40),
|
||||
@@ -313,6 +320,8 @@ class Config(object):
|
||||
TEMPLATE_PREVIEW_API_HOST = os.environ.get('TEMPLATE_PREVIEW_API_HOST', 'http://localhost:6013')
|
||||
TEMPLATE_PREVIEW_API_KEY = os.environ.get('TEMPLATE_PREVIEW_API_KEY', 'my-secret-key')
|
||||
|
||||
LETTER_PROCESSING_DEADLINE = time(17, 30)
|
||||
|
||||
|
||||
######################
|
||||
# Config overrides ###
|
||||
|
||||
@@ -584,3 +584,37 @@ def dao_get_last_notification_added_for_job_id(job_id):
|
||||
).first()
|
||||
|
||||
return last_notification_added
|
||||
|
||||
|
||||
def dao_get_count_of_letters_to_process_for_date(date_to_process=None):
|
||||
"""
|
||||
Returns a count of letter notifications for services with letters_as_pdf permission set
|
||||
to be processed today if no argument passed in otherwise will return the count for
|
||||
the date passed in.
|
||||
Records processed today = yesterday 17:30 to today 17:29:59
|
||||
|
||||
Note - services without letters_as_pdf permission will be ignored
|
||||
"""
|
||||
if date_to_process is None:
|
||||
date_to_process = date.today()
|
||||
|
||||
day_before = date_to_process - timedelta(days=1)
|
||||
letter_deadline_time = current_app.config.get('LETTER_PROCESSING_DEADLINE')
|
||||
|
||||
start_datetime = datetime.combine(day_before, letter_deadline_time)
|
||||
end_datetime = start_datetime + timedelta(days=1)
|
||||
|
||||
count_of_letters_to_process_for_date = Notification.query.join(
|
||||
Service
|
||||
).filter(
|
||||
Notification.created_at >= start_datetime,
|
||||
Notification.created_at < end_datetime,
|
||||
Notification.notification_type == LETTER_TYPE,
|
||||
Notification.status == NOTIFICATION_CREATED,
|
||||
Notification.key_type != KEY_TYPE_TEST,
|
||||
Service.permissions.any(
|
||||
ServicePermission.permission == 'letters_as_pdf'
|
||||
)
|
||||
).count()
|
||||
|
||||
return count_of_letters_to_process_for_date
|
||||
|
||||
@@ -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(
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user