mirror of
https://github.com/GSA/notifications-api.git
synced 2026-08-11 09:27:56 -04:00
Merge branch 'master' into delete-csv-file-after-7-days
Conflicts: app/notifications/rest.py
This commit is contained in:
@@ -41,7 +41,6 @@ def create_app(app_name=None):
|
||||
|
||||
from config import configs
|
||||
application.config.from_object(configs[os.environ['NOTIFY_ENVIRONMENT']])
|
||||
|
||||
if app_name:
|
||||
application.config['NOTIFY_APP_NAME'] = app_name
|
||||
|
||||
|
||||
@@ -125,8 +125,11 @@ def send_sms(self,
|
||||
return
|
||||
|
||||
try:
|
||||
_save_notification(created_at, notification, notification_id, service_id, SMS_TYPE, api_key_id, key_type)
|
||||
|
||||
dao_create_notification(
|
||||
Notification.from_api_request(
|
||||
created_at, notification, notification_id, service.id, SMS_TYPE, api_key_id, key_type
|
||||
)
|
||||
)
|
||||
send_sms_to_provider.apply_async((service_id, notification_id), queue='send-sms')
|
||||
|
||||
current_app.logger.info(
|
||||
@@ -160,7 +163,11 @@ def send_email(self, service_id,
|
||||
return
|
||||
|
||||
try:
|
||||
_save_notification(created_at, notification, notification_id, service_id, EMAIL_TYPE, api_key_id, key_type)
|
||||
dao_create_notification(
|
||||
Notification.from_api_request(
|
||||
created_at, notification, notification_id, service.id, EMAIL_TYPE, api_key_id, key_type
|
||||
)
|
||||
)
|
||||
|
||||
send_email_to_provider.apply_async((service_id, notification_id), queue='send-email')
|
||||
|
||||
@@ -176,25 +183,6 @@ def send_email(self, service_id,
|
||||
)
|
||||
|
||||
|
||||
def _save_notification(created_at, notification, notification_id, service_id, notification_type, api_key_id, key_type):
|
||||
notification_db_object = Notification(
|
||||
id=notification_id,
|
||||
template_id=notification['template'],
|
||||
template_version=notification['template_version'],
|
||||
to=notification['to'],
|
||||
service_id=service_id,
|
||||
job_id=notification.get('job', None),
|
||||
job_row_number=notification.get('row_number', None),
|
||||
status='created',
|
||||
created_at=datetime.strptime(created_at, DATETIME_FORMAT),
|
||||
personalisation=notification.get('personalisation'),
|
||||
notification_type=notification_type,
|
||||
api_key_id=api_key_id,
|
||||
key_type=key_type
|
||||
)
|
||||
dao_create_notification(notification_db_object)
|
||||
|
||||
|
||||
def service_allowed_to_send_to(recipient, service, key_type):
|
||||
if not service.restricted or key_type == KEY_TYPE_TEST:
|
||||
return True
|
||||
|
||||
@@ -106,7 +106,7 @@ def dao_get_template_usage(service_id, limit_days=None):
|
||||
|
||||
@statsd(namespace="dao")
|
||||
def dao_get_last_template_usage(template_id):
|
||||
return NotificationHistory.query.filter(NotificationHistory.template_id == template_id)\
|
||||
return NotificationHistory.query.filter(NotificationHistory.template_id == template_id) \
|
||||
.join(Template) \
|
||||
.order_by(desc(NotificationHistory.created_at)) \
|
||||
.first()
|
||||
@@ -275,3 +275,14 @@ def delete_notifications_created_more_than_a_week_ago(status):
|
||||
).delete(synchronize_session='fetch')
|
||||
db.session.commit()
|
||||
return deleted
|
||||
|
||||
|
||||
@statsd(namespace="dao")
|
||||
@transactional
|
||||
def dao_delete_notifications_and_history_by_id(notification_id):
|
||||
db.session.query(Notification).filter(
|
||||
Notification.id == notification_id
|
||||
).delete(synchronize_session='fetch')
|
||||
db.session.query(NotificationHistory).filter(
|
||||
NotificationHistory.id == notification_id
|
||||
).delete(synchronize_session='fetch')
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
import uuid
|
||||
import datetime
|
||||
|
||||
from sqlalchemy.dialects.postgresql import (
|
||||
UUID,
|
||||
JSON
|
||||
@@ -15,8 +14,8 @@ from app.encryption import (
|
||||
from app.authentication.utils import get_secret
|
||||
from app import (
|
||||
db,
|
||||
encryption
|
||||
)
|
||||
encryption,
|
||||
DATETIME_FORMAT)
|
||||
|
||||
from app.history_meta import Versioned
|
||||
|
||||
@@ -74,7 +73,6 @@ user_to_service = db.Table(
|
||||
UniqueConstraint('user_id', 'service_id', name='uix_user_to_service')
|
||||
)
|
||||
|
||||
|
||||
BRANDING_GOVUK = 'govuk'
|
||||
BRANDING_ORG = 'org'
|
||||
BRANDING_BOTH = 'both'
|
||||
@@ -395,6 +393,7 @@ class VerifyCode(db.Model):
|
||||
def check_code(self, cde):
|
||||
return check_hash(cde, self._code)
|
||||
|
||||
|
||||
NOTIFICATION_CREATED = 'created'
|
||||
NOTIFICATION_SENDING = 'sending'
|
||||
NOTIFICATION_DELIVERED = 'delivered'
|
||||
@@ -427,7 +426,6 @@ NOTIFICATION_STATUS_TYPES_ENUM = db.Enum(*NOTIFICATION_STATUS_TYPES, name='notif
|
||||
|
||||
|
||||
class Notification(db.Model):
|
||||
|
||||
__tablename__ = 'notifications'
|
||||
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
@@ -482,6 +480,32 @@ class Notification(db.Model):
|
||||
if personalisation:
|
||||
self._personalisation = encryption.encrypt(personalisation)
|
||||
|
||||
@classmethod
|
||||
def from_api_request(
|
||||
cls,
|
||||
created_at,
|
||||
notification,
|
||||
notification_id,
|
||||
service_id,
|
||||
notification_type,
|
||||
api_key_id,
|
||||
key_type):
|
||||
return cls(
|
||||
id=notification_id,
|
||||
template_id=notification['template'],
|
||||
template_version=notification['template_version'],
|
||||
to=notification['to'],
|
||||
service_id=service_id,
|
||||
job_id=notification.get('job', None),
|
||||
job_row_number=notification.get('row_number', None),
|
||||
status='created',
|
||||
created_at=datetime.datetime.strptime(created_at, DATETIME_FORMAT),
|
||||
personalisation=notification.get('personalisation'),
|
||||
notification_type=notification_type,
|
||||
api_key_id=api_key_id,
|
||||
key_type=key_type
|
||||
)
|
||||
|
||||
|
||||
class NotificationHistory(db.Model):
|
||||
__tablename__ = 'notification_history'
|
||||
@@ -522,7 +546,6 @@ INVITED_USER_STATUS_TYPES = ['pending', 'accepted', 'cancelled']
|
||||
|
||||
|
||||
class InvitedUser(db.Model):
|
||||
|
||||
__tablename__ = 'invited_users'
|
||||
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
@@ -598,7 +621,6 @@ class Permission(db.Model):
|
||||
|
||||
|
||||
class TemplateStatistics(db.Model):
|
||||
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False, nullable=False)
|
||||
service = db.relationship('Service', backref=db.backref('template_statistics', lazy='dynamic'))
|
||||
@@ -615,7 +637,6 @@ class TemplateStatistics(db.Model):
|
||||
|
||||
|
||||
class Event(db.Model):
|
||||
|
||||
__tablename__ = 'events'
|
||||
|
||||
id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4)
|
||||
|
||||
@@ -13,9 +13,10 @@ from notifications_utils.recipients import allowed_to_send_to, first_column_head
|
||||
from notifications_utils.template import Template
|
||||
from notifications_utils.renderers import PassThrough
|
||||
from app.clients.email.aws_ses import get_aws_responses
|
||||
from app import api_user, encryption, create_uuid, DATETIME_FORMAT, statsd_client
|
||||
from app import api_user, create_uuid, DATETIME_FORMAT, statsd_client
|
||||
from app.dao.notifications_dao import dao_create_notification, dao_delete_notifications_and_history_by_id
|
||||
from app.dao.services_dao import dao_fetch_todays_stats_for_service
|
||||
from app.models import KEY_TYPE_TEAM, KEY_TYPE_TEST
|
||||
from app.models import KEY_TYPE_TEAM, KEY_TYPE_TEST, Notification, KEY_TYPE_NORMAL, EMAIL_TYPE
|
||||
from app.dao import (
|
||||
templates_dao,
|
||||
services_dao,
|
||||
@@ -35,7 +36,6 @@ from app.schemas import (
|
||||
day_schema,
|
||||
unarchived_template_schema
|
||||
)
|
||||
from app.celery.tasks import send_sms, send_email
|
||||
from app.utils import pagination_links
|
||||
|
||||
notifications = Blueprint('notifications', __name__)
|
||||
@@ -46,6 +46,7 @@ from app.errors import (
|
||||
)
|
||||
|
||||
register_errors(notifications)
|
||||
from app.celery.provider_tasks import send_sms_to_provider, send_email_to_provider
|
||||
|
||||
|
||||
@notifications.route('/notifications/email/ses', methods=['POST'])
|
||||
@@ -192,19 +193,16 @@ def send_notification(notification_type):
|
||||
service_id = str(api_user.service_id)
|
||||
service = services_dao.dao_fetch_service_by_id(service_id)
|
||||
|
||||
service_stats = sum(row.count for row in dao_fetch_todays_stats_for_service(service.id))
|
||||
|
||||
if all((
|
||||
api_user.key_type != KEY_TYPE_TEST,
|
||||
service_stats >= service.message_limit
|
||||
)):
|
||||
error = 'Exceeded send limits ({}) for today'.format(service.message_limit)
|
||||
raise InvalidRequest(error, status_code=429)
|
||||
|
||||
notification, errors = (
|
||||
sms_template_notification_schema if notification_type == SMS_TYPE else email_notification_schema
|
||||
).load(request.get_json())
|
||||
|
||||
if all((api_user.key_type != KEY_TYPE_TEST, service.restricted)):
|
||||
service_stats = sum(row.count for row in dao_fetch_todays_stats_for_service(service.id))
|
||||
if service_stats >= service.message_limit:
|
||||
error = 'Exceeded send limits ({}) for today'.format(service.message_limit)
|
||||
raise InvalidRequest(error, status_code=429)
|
||||
|
||||
if errors:
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
@@ -233,8 +231,8 @@ def send_notification(notification_type):
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
if (
|
||||
template_object.template_type == SMS_TYPE and
|
||||
template_object.replaced_content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
||||
template_object.template_type == SMS_TYPE and
|
||||
template_object.replaced_content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
||||
):
|
||||
char_count = current_app.config.get('SMS_CHAR_COUNT_LIMIT')
|
||||
message = 'Content has a character count greater than the limit of {}'.format(char_count)
|
||||
@@ -242,14 +240,14 @@ def send_notification(notification_type):
|
||||
raise InvalidRequest(errors, status_code=400)
|
||||
|
||||
if all((
|
||||
api_user.key_type != KEY_TYPE_TEST,
|
||||
service.restricted or api_user.key_type == KEY_TYPE_TEAM,
|
||||
not allowed_to_send_to(
|
||||
notification['to'],
|
||||
itertools.chain.from_iterable(
|
||||
[user.mobile_number, user.email_address] for user in service.users
|
||||
api_user.key_type != KEY_TYPE_TEST,
|
||||
service.restricted or api_user.key_type == KEY_TYPE_TEAM,
|
||||
not allowed_to_send_to(
|
||||
notification['to'],
|
||||
itertools.chain.from_iterable(
|
||||
[user.mobile_number, user.email_address] for user in service.users
|
||||
)
|
||||
)
|
||||
)
|
||||
)):
|
||||
if (api_user.key_type == KEY_TYPE_TEAM):
|
||||
message = 'Can’t send to this recipient using a team-only API key'
|
||||
@@ -265,34 +263,15 @@ def send_notification(notification_type):
|
||||
|
||||
notification_id = create_uuid()
|
||||
notification.update({"template_version": template.version})
|
||||
if notification_type == SMS_TYPE:
|
||||
send_sms.apply_async(
|
||||
(
|
||||
service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification),
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||
),
|
||||
kwargs={
|
||||
'api_key_id': str(api_user.id),
|
||||
'key_type': api_user.key_type
|
||||
},
|
||||
queue='db-sms'
|
||||
)
|
||||
else:
|
||||
send_email.apply_async(
|
||||
(
|
||||
service_id,
|
||||
notification_id,
|
||||
encryption.encrypt(notification),
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT)
|
||||
),
|
||||
kwargs={
|
||||
'api_key_id': str(api_user.id),
|
||||
'key_type': api_user.key_type
|
||||
},
|
||||
queue='db-email'
|
||||
)
|
||||
persist_notification(
|
||||
service,
|
||||
notification_id,
|
||||
notification,
|
||||
datetime.utcnow().strftime(DATETIME_FORMAT),
|
||||
notification_type,
|
||||
str(api_user.id),
|
||||
api_user.key_type
|
||||
)
|
||||
|
||||
return jsonify(
|
||||
data=get_notification_return_data(
|
||||
@@ -323,3 +302,33 @@ def get_notification_statistics_for_day():
|
||||
)
|
||||
data, errors = notifications_statistics_schema.dump(statistics, many=True)
|
||||
return jsonify(data=data), 200
|
||||
|
||||
|
||||
def persist_notification(
|
||||
service,
|
||||
notification_id,
|
||||
notification,
|
||||
created_at,
|
||||
notification_type,
|
||||
api_key_id=None,
|
||||
key_type=KEY_TYPE_NORMAL,
|
||||
):
|
||||
dao_create_notification(
|
||||
Notification.from_api_request(
|
||||
created_at, notification, notification_id, service.id, notification_type, api_key_id, key_type
|
||||
)
|
||||
)
|
||||
|
||||
try:
|
||||
if notification_type == SMS_TYPE:
|
||||
send_sms_to_provider.apply_async((str(service.id), str(notification_id)), queue='send-sms')
|
||||
if notification_type == EMAIL_TYPE:
|
||||
send_email_to_provider.apply_async((str(service.id), str(notification_id)), queue='send-email')
|
||||
except Exception as e:
|
||||
current_app.logger.exception("Failed to send to SQS exception", e)
|
||||
dao_delete_notifications_and_history_by_id(notification_id)
|
||||
raise InvalidRequest(message="Internal server error", status_code=500)
|
||||
|
||||
current_app.logger.info(
|
||||
"{} {} created at {}".format(notification_type, notification_id, created_at)
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user