2016-02-17 17:48:23 +00:00
|
|
|
from app import notify_celery, encryption, firetext_client, aws_ses_client
|
|
|
|
|
from app.clients.email.aws_ses import AwsSesClientException
|
2016-02-17 13:59:01 +00:00
|
|
|
from app.clients.sms.firetext import FiretextClientException
|
2016-02-15 16:01:14 +00:00
|
|
|
from app.dao.templates_dao import get_model_templates
|
2016-02-16 17:17:02 +00:00
|
|
|
from app.dao.notifications_dao import save_notification
|
2016-02-15 16:01:14 +00:00
|
|
|
from app.models import Notification
|
|
|
|
|
from flask import current_app
|
2016-02-16 17:42:04 +00:00
|
|
|
from sqlalchemy.exc import SQLAlchemyError
|
2016-02-09 13:31:45 +00:00
|
|
|
|
|
|
|
|
|
2016-02-16 17:17:02 +00:00
|
|
|
@notify_celery.task(name="send-sms")
|
2016-02-16 15:28:30 +00:00
|
|
|
def send_sms(service_id, notification_id, encrypted_notification):
|
|
|
|
|
notification = encryption.decrypt(encrypted_notification)
|
2016-02-15 16:01:14 +00:00
|
|
|
template = get_model_templates(notification['template'])
|
|
|
|
|
|
|
|
|
|
try:
|
2016-02-16 17:42:04 +00:00
|
|
|
notification_db_object = Notification(
|
|
|
|
|
id=notification_id,
|
|
|
|
|
template_id=notification['template'],
|
|
|
|
|
to=notification['to'],
|
|
|
|
|
service_id=service_id,
|
|
|
|
|
status='sent'
|
|
|
|
|
)
|
|
|
|
|
save_notification(notification_db_object)
|
2016-02-15 16:01:14 +00:00
|
|
|
|
2016-02-16 17:42:04 +00:00
|
|
|
try:
|
2016-02-17 12:57:51 +00:00
|
|
|
firetext_client.send_sms(notification['to'], template.content)
|
2016-02-17 13:59:01 +00:00
|
|
|
except FiretextClientException as e:
|
2016-02-16 17:42:04 +00:00
|
|
|
current_app.logger.debug(e)
|
|
|
|
|
save_notification(notification_db_object, {"status": "failed"})
|
2016-02-15 16:01:14 +00:00
|
|
|
|
2016-02-16 17:42:04 +00:00
|
|
|
except SQLAlchemyError as e:
|
|
|
|
|
current_app.logger.debug(e)
|
2016-02-17 15:41:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='send-sms-code')
|
2016-02-18 09:59:18 +00:00
|
|
|
def send_sms_code(encrypted_verification):
|
|
|
|
|
verification_message = encryption.decrypt(encrypted_verification)
|
2016-02-17 15:41:33 +00:00
|
|
|
try:
|
2016-02-18 09:59:18 +00:00
|
|
|
firetext_client.send_sms(verification_message['to'], verification_message['secret_code'])
|
2016-02-17 15:52:09 +00:00
|
|
|
except FiretextClientException as e:
|
2016-02-17 17:48:23 +00:00
|
|
|
current_app.logger.error(e)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@notify_celery.task(name='send-email-code')
|
2016-02-18 09:52:27 +00:00
|
|
|
def send_email_code(encrypted_verification_message):
|
|
|
|
|
verification_message = encryption.decrypt(encrypted_verification_message)
|
2016-02-17 17:48:23 +00:00
|
|
|
try:
|
2016-02-18 09:52:27 +00:00
|
|
|
aws_ses_client.send_email(verification_message['from_address'],
|
|
|
|
|
verification_message['to_address'],
|
|
|
|
|
verification_message['subject'],
|
|
|
|
|
verification_message['body'])
|
2016-02-17 17:48:23 +00:00
|
|
|
except AwsSesClientException as e:
|
|
|
|
|
current_app.logger.error(e)
|