Message limit added and all tests passing.

This commit is contained in:
Nicholas Staples
2016-04-29 10:36:59 +01:00
parent 87a02cba14
commit f71dbe9c0f
9 changed files with 135 additions and 36 deletions

View File

@@ -38,8 +38,7 @@ 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,
get_character_count_of_content
update_notification_reference_by_id
)
from app.dao.jobs_dao import (
@@ -233,7 +232,7 @@ def send_sms(service_id, notification_id, encrypted_notification, created_at):
created_at=datetime.strptime(created_at, DATETIME_FORMAT),
sent_at=sent_at,
sent_by=client.get_name(),
content_char_count=get_character_count_of_content(template.replaced)
content_char_count=template.replaced_content_count
)
dao_create_notification(notification_db_object, TEMPLATE_TYPE_SMS, client.get_name())

View File

@@ -22,6 +22,8 @@ from app.models import (
ProviderStatistics
)
from notifications_utils.template import get_sms_fragment_count
from app.clients import (
STATISTICS_FAILURE,
STATISTICS_DELIVERED,
@@ -31,14 +33,6 @@ from app.clients import (
from app.dao.dao_utils import transactional
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, limit_days=None):
filter = [NotificationStatistics.service_id == service_id]
if limit_days is not None:
@@ -105,14 +99,14 @@ def dao_create_notification(notification, notification_type, provider):
service_id=notification.service_id,
provider=provider
).update({'unit_count': ProviderStatistics.unit_count + (
1 if notification_type == TEMPLATE_TYPE_EMAIL else get_sms_message_count(notification.content_char_count))})
1 if notification_type == TEMPLATE_TYPE_EMAIL else get_sms_fragment_count(notification.content_char_count))})
if update_count == 0:
provider_stats = ProviderStatistics(
day=notification.created_at.date(),
service_id=notification.service_id,
provider=provider,
unit_count=1 if notification_type == TEMPLATE_TYPE_EMAIL else get_sms_message_count(
unit_count=1 if notification_type == TEMPLATE_TYPE_EMAIL else get_sms_fragment_count(
notification.content_char_count))
db.session.add(provider_stats)

View File

@@ -338,6 +338,12 @@ def send_notification(notification_type):
}
), 400
if template_object.replaced_content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT'):
return jsonify(
result="error",
message={'content': ['Content has a character count greater than the limit of {}'.format(
current_app.config.get('SMS_CHAR_COUNT_LIMIT'))]}), 400
if service.restricted and not allowed_to_send_to(
notification['to'],
itertools.chain.from_iterable(

View File

@@ -13,9 +13,8 @@ from app.dao.templates_dao import (
dao_get_template_by_id_and_service_id,
dao_get_all_templates_for_service
)
from app.dao.services_dao import (
dao_fetch_service_by_id
)
from notifications_utils.template import Template
from app.dao.services_dao import dao_fetch_service_by_id
from app.schemas import template_schema
template = Blueprint('template', __name__, url_prefix='/service/<uuid:service_id>/template')
@@ -25,6 +24,18 @@ from app.errors import register_errors
register_errors(template)
def _content_count_greater_than_limit(content, template_type):
template = Template({'content': content, 'template_type': template_type})
if template_type == 'sms' and \
template.content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT'):
return True, jsonify(
result="error",
message={'content': ['Content has a character count greater than the limit of {}'.format(
current_app.config.get('SMS_CHAR_COUNT_LIMIT'))]}
)
return False, ''
@template.route('', methods=['POST'])
def create_template(service_id):
fetched_service = dao_fetch_service_by_id(service_id=service_id)
@@ -33,6 +44,11 @@ def create_template(service_id):
return jsonify(result="error", message=errors), 400
new_template.service = fetched_service
new_template.content = _strip_html(new_template.content)
over_limit, json_resp = _content_count_greater_than_limit(
new_template.content,
new_template.template_type)
if over_limit:
return json_resp, 400
try:
dao_create_template(new_template)
except IntegrityError as ex:
@@ -40,7 +56,7 @@ def create_template(service_id):
message = "Failed to create template"
if "templates_subject_key" in str(ex):
message = 'Duplicate template subject'
return jsonify(result="error", message=[{'subject': message}]), 400
return jsonify(result="error", message={'subject': [message]}), 400
return jsonify(result="error", message=message), 500
return jsonify(data=template_schema.dump(new_template).data), 201
@@ -57,7 +73,11 @@ def update_template(service_id, template_id):
update_dict, errors = template_schema.load(current_data)
if errors:
return jsonify(result="error", message=errors), 400
over_limit, json_resp = _content_count_greater_than_limit(
current_data['content'],
fetched_template.template_type)
if over_limit:
return json_resp, 400
dao_update_template(update_dict)
return jsonify(data=template_schema.dump(update_dict).data), 200