Character count added and tests passing.

Remove sql restriction for count to not be null.
This commit is contained in:
Nicholas Staples
2016-04-15 15:19:58 +01:00
parent 966a662513
commit f681d40366
5 changed files with 74 additions and 5 deletions

View File

@@ -38,7 +38,8 @@ from app.dao.notifications_dao import (
dao_update_notification,
delete_notifications_created_more_than_a_week_ago,
dao_get_notification_statistics_for_service_and_day,
update_notification_reference_by_id
update_notification_reference_by_id,
get_character_count_of_content
)
from app.dao.jobs_dao import (
@@ -240,6 +241,9 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at):
content=template.replaced,
reference=str(notification_id)
)
# Record the character count of the content after placeholders included
notification_db_object.content_char_count = get_character_count_of_content(template.replaced)
dao_update_notification(notification_db_object)
except MMGClientException as e:
current_app.logger.error(
"SMS notification {} failed".format(notification_id)

View File

@@ -1,3 +1,4 @@
import math
from sqlalchemy import (
desc,
func
@@ -47,6 +48,14 @@ def transactional(func):
return commit_or_rollback
def get_character_count_of_content(content, encoding='utf-8'):
return len(content.encode(encoding))
def get_sms_message_count(char_count):
return 1 if char_count <= 160 else math.ceil(float(char_count) / 153)
def dao_get_notification_statistics_for_service(service_id):
return NotificationStatistics.query.filter_by(
service_id=service_id

View File

@@ -64,9 +64,7 @@ user_to_service = db.Table(
db.Model.metadata,
db.Column('user_id', UUID(as_uuid=True), db.ForeignKey('users.id')),
db.Column('service_id', UUID(as_uuid=True), db.ForeignKey('services.id')),
UniqueConstraint('user_id', 'service_id', name='uix_user_to_service')
)
@@ -249,6 +247,7 @@ class Notification(db.Model):
service = db.relationship('Service')
template_id = db.Column(UUID(as_uuid=True), db.ForeignKey('templates.id'), index=True, unique=False)
template = db.relationship('Template')
content_char_count = db.Column(db.Integer, nullable=True)
created_at = db.Column(
db.DateTime,
index=False,