mirror of
https://github.com/GSA/notifications-api.git
synced 2026-04-30 06:09:58 -04:00
Merge pull request #498 from alphagov/set-sms-sender-api
Set sms sender on service
This commit is contained in:
@@ -4,10 +4,14 @@ from flask import current_app
|
||||
from app import notify_celery, statsd_client, clients, create_uuid
|
||||
from app.clients.email import EmailClientException
|
||||
from app.clients.sms import SmsClientException
|
||||
|
||||
from app.dao.notifications_dao import (
|
||||
update_provider_stats,
|
||||
get_notification_by_id,
|
||||
dao_update_notification, update_notification_status_by_id)
|
||||
dao_update_notification,
|
||||
update_notification_status_by_id
|
||||
)
|
||||
|
||||
from app.dao.provider_details_dao import get_provider_details_by_notification_type
|
||||
from app.dao.services_dao import dao_fetch_service_by_id
|
||||
from app.celery.research_mode_tasks import send_sms_response, send_email_response
|
||||
@@ -54,12 +58,12 @@ def send_sms_to_provider(self, service_id, notification_id):
|
||||
provider = provider_to_use(SMS_TYPE, notification_id)
|
||||
notification = get_notification_by_id(notification_id)
|
||||
if notification.status == 'created':
|
||||
template_model = dao_get_template_by_id(notification.template_id, notification.template_version)
|
||||
template = Template(
|
||||
dao_get_template_by_id(notification.template_id, notification.template_version).__dict__,
|
||||
template_model.__dict__,
|
||||
values={} if not notification.personalisation else notification.personalisation,
|
||||
prefix=service.name
|
||||
)
|
||||
|
||||
try:
|
||||
if service.research_mode:
|
||||
send_sms_response.apply_async(
|
||||
@@ -69,7 +73,8 @@ def send_sms_to_provider(self, service_id, notification_id):
|
||||
provider.send_sms(
|
||||
to=validate_and_format_phone_number(notification.to),
|
||||
content=template.replaced,
|
||||
reference=str(notification_id)
|
||||
reference=str(notification_id),
|
||||
sender=service.sms_sender
|
||||
)
|
||||
|
||||
update_provider_stats(
|
||||
|
||||
@@ -67,11 +67,11 @@ class FiretextClient(SmsClient):
|
||||
def get_name(self):
|
||||
return self.name
|
||||
|
||||
def send_sms(self, to, content, reference):
|
||||
def send_sms(self, to, content, reference, sender=None):
|
||||
|
||||
data = {
|
||||
"apiKey": self.api_key,
|
||||
"from": self.from_number,
|
||||
"from": self.from_number if sender is None else sender,
|
||||
"to": to.replace('+', ''),
|
||||
"message": content,
|
||||
"reference": reference
|
||||
|
||||
@@ -67,12 +67,12 @@ class MMGClient(SmsClient):
|
||||
def get_name(self):
|
||||
return self.name
|
||||
|
||||
def send_sms(self, to, content, reference, multi=True):
|
||||
def send_sms(self, to, content, reference, multi=True, sender=None):
|
||||
data = {
|
||||
"reqType": "BULK",
|
||||
"MSISDN": to,
|
||||
"msg": content,
|
||||
"sender": self.from_number,
|
||||
"sender": self.from_number if sender is None else sender,
|
||||
"cid": reference,
|
||||
"multi": multi
|
||||
}
|
||||
|
||||
@@ -103,6 +103,7 @@ class Service(db.Model, Versioned):
|
||||
created_by = db.relationship('User')
|
||||
created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), index=True, nullable=False)
|
||||
reply_to_email_address = db.Column(db.Text, index=False, unique=False, nullable=True)
|
||||
sms_sender = db.Column(db.String(11), nullable=True)
|
||||
|
||||
|
||||
class ApiKey(db.Model, Versioned):
|
||||
@@ -203,7 +204,6 @@ class Template(db.Model, Versioned):
|
||||
created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), index=True, nullable=False)
|
||||
created_by = db.relationship('User')
|
||||
|
||||
|
||||
MMG_PROVIDER = "mmg"
|
||||
FIRETEXT_PROVIDER = "firetext"
|
||||
SES_PROVIDER = 'ses'
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import re
|
||||
from datetime import (
|
||||
datetime,
|
||||
date
|
||||
@@ -106,6 +107,11 @@ class ServiceSchema(BaseSchema):
|
||||
exclude = ("updated_at", "created_at", "api_keys", "templates", "jobs", 'old_id')
|
||||
strict = True
|
||||
|
||||
@validates('sms_sender')
|
||||
def validate_sms_sender(self, value):
|
||||
if value and not re.match('^[a-zA-Z0-9\s]+$', value):
|
||||
raise ValidationError('Only alphanumeric characters allowed')
|
||||
|
||||
|
||||
class NotificationModelSchema(BaseSchema):
|
||||
class Meta:
|
||||
|
||||
Reference in New Issue
Block a user