mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-01 15:46:07 -05:00
Merge pull request #811 from alphagov/check-service-is-active
Check service is active
This commit is contained in:
@@ -25,8 +25,8 @@ from app.models import (
|
||||
EMAIL_TYPE,
|
||||
SMS_TYPE,
|
||||
LETTER_TYPE,
|
||||
KEY_TYPE_NORMAL
|
||||
)
|
||||
KEY_TYPE_NORMAL,
|
||||
JOB_STATUS_CANCELLED, JOB_STATUS_PENDING, JOB_STATUS_IN_PROGRESS, JOB_STATUS_FINISHED)
|
||||
from app.notifications.process_notifications import persist_notification
|
||||
from app.service.utils import service_allowed_to_send_to
|
||||
from app.statsd_decorators import statsd
|
||||
@@ -38,15 +38,22 @@ def process_job(job_id):
|
||||
start = datetime.utcnow()
|
||||
job = dao_get_job_by_id(job_id)
|
||||
|
||||
if job.job_status != 'pending':
|
||||
if job.job_status != JOB_STATUS_PENDING:
|
||||
return
|
||||
|
||||
service = job.service
|
||||
|
||||
if not service.active:
|
||||
job.job_status = JOB_STATUS_CANCELLED
|
||||
dao_update_job(job)
|
||||
current_app.logger.warn(
|
||||
"Job {} has been cancelled, service {} is inactive".format(job_id, service.id))
|
||||
return
|
||||
|
||||
if __sending_limits_for_job_exceeded(service, job, job_id):
|
||||
return
|
||||
|
||||
job.job_status = 'in progress'
|
||||
job.job_status = JOB_STATUS_IN_PROGRESS
|
||||
dao_update_job(job)
|
||||
|
||||
db_template = dao_get_template_by_id(job.template_id, job.template_version)
|
||||
@@ -62,7 +69,7 @@ def process_job(job_id):
|
||||
process_row(row_number, recipient, personalisation, template, job, service)
|
||||
|
||||
finished = datetime.utcnow()
|
||||
job.job_status = 'finished'
|
||||
job.job_status = JOB_STATUS_FINISHED
|
||||
job.processing_started = start
|
||||
job.processing_finished = finished
|
||||
dao_update_job(job)
|
||||
|
||||
@@ -59,7 +59,8 @@ class PermissionDAO(DAOClass):
|
||||
db.session.commit()
|
||||
|
||||
def get_permissions_by_user_id(self, user_id):
|
||||
return self.Meta.model.query.filter_by(user_id=user_id).all()
|
||||
return self.Meta.model.query.filter_by(user_id=user_id)\
|
||||
.join(Permission.service).filter_by(active=True).all()
|
||||
|
||||
|
||||
permission_dao = PermissionDAO()
|
||||
|
||||
@@ -13,19 +13,26 @@ from app.dao.provider_details_dao import (
|
||||
get_provider_details_by_notification_type,
|
||||
dao_toggle_sms_provider
|
||||
)
|
||||
from app.dao.services_dao import dao_fetch_service_by_id
|
||||
from app.celery.research_mode_tasks import send_sms_response, send_email_response
|
||||
from app.dao.templates_dao import dao_get_template_by_id
|
||||
from app.models import SMS_TYPE, KEY_TYPE_TEST, BRANDING_ORG, EMAIL_TYPE
|
||||
from app.models import SMS_TYPE, KEY_TYPE_TEST, BRANDING_ORG, EMAIL_TYPE, NOTIFICATION_TECHNICAL_FAILURE
|
||||
|
||||
|
||||
def send_sms_to_provider(notification):
|
||||
service = dao_fetch_service_by_id(notification.service_id)
|
||||
provider = provider_to_use(SMS_TYPE, notification.id)
|
||||
current_app.logger.info(
|
||||
"Starting sending SMS {} to provider at {}".format(notification.id, datetime.utcnow())
|
||||
)
|
||||
service = notification.service
|
||||
if not service.active:
|
||||
notification.status = NOTIFICATION_TECHNICAL_FAILURE
|
||||
dao_update_notification(notification)
|
||||
current_app.logger.warn(
|
||||
"Send sms for notification id {} to provider is not allowed: service {} is inactive".format(notification.id,
|
||||
service.id))
|
||||
return
|
||||
|
||||
if notification.status == 'created':
|
||||
provider = provider_to_use(SMS_TYPE, notification.id)
|
||||
current_app.logger.info(
|
||||
"Starting sending SMS {} to provider at {}".format(notification.id, datetime.utcnow())
|
||||
)
|
||||
template_model = dao_get_template_by_id(notification.template_id, notification.template_version)
|
||||
template = SMSMessageTemplate(
|
||||
template_model.__dict__,
|
||||
@@ -61,12 +68,20 @@ def send_sms_to_provider(notification):
|
||||
|
||||
|
||||
def send_email_to_provider(notification):
|
||||
service = dao_fetch_service_by_id(notification.service_id)
|
||||
provider = provider_to_use(EMAIL_TYPE, notification.id)
|
||||
current_app.logger.info(
|
||||
"Starting sending EMAIL {} to provider at {}".format(notification.id, datetime.utcnow())
|
||||
)
|
||||
service = notification.service
|
||||
if not service.active:
|
||||
notification.status = NOTIFICATION_TECHNICAL_FAILURE
|
||||
dao_update_notification(notification)
|
||||
current_app.logger.warn(
|
||||
"Send email for notification id {} to provider is not allowed: service {} is inactive".format(
|
||||
notification.id,
|
||||
service.id))
|
||||
return
|
||||
if notification.status == 'created':
|
||||
provider = provider_to_use(EMAIL_TYPE, notification.id)
|
||||
current_app.logger.info(
|
||||
"Starting sending EMAIL {} to provider at {}".format(notification.id, datetime.utcnow())
|
||||
)
|
||||
template_dict = dao_get_template_by_id(notification.template_id, notification.template_version).__dict__
|
||||
|
||||
html_email = HTMLEmailTemplate(
|
||||
|
||||
@@ -110,7 +110,9 @@ def get_jobs_by_service(service_id):
|
||||
|
||||
@job.route('', methods=['POST'])
|
||||
def create_job(service_id):
|
||||
dao_fetch_service_by_id(service_id)
|
||||
service = dao_fetch_service_by_id(service_id)
|
||||
if not service.active:
|
||||
raise InvalidRequest("Create job is not allowed: service is inactive ", 403)
|
||||
|
||||
data = request.get_json()
|
||||
|
||||
|
||||
@@ -353,20 +353,6 @@ def update_whitelist(service_id):
|
||||
return '', 204
|
||||
|
||||
|
||||
# Renaming this endpoint to archive
|
||||
@service_blueprint.route('/<uuid:service_id>/deactivate', methods=['POST'])
|
||||
def deactivate_service(service_id):
|
||||
service = dao_fetch_service_by_id(service_id)
|
||||
|
||||
if not service.active:
|
||||
# assume already inactive, don't change service name
|
||||
return '', 204
|
||||
|
||||
dao_archive_service(service.id)
|
||||
|
||||
return '', 204
|
||||
|
||||
|
||||
@service_blueprint.route('/<uuid:service_id>/archive', methods=['POST'])
|
||||
def archive_service(service_id):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user