Refactored to make code clearer

This commit is contained in:
Ken Tsang
2017-06-29 18:02:21 +01:00
committed by venusbb
parent e0fbcb0dc6
commit 0b3277b8a4
6 changed files with 47 additions and 46 deletions

View File

@@ -17,6 +17,7 @@ from notifications_utils.template import SMSMessageTemplate
from app.dao.services_dao import dao_fetch_service_by_id
from app.dao.service_permissions_dao import dao_fetch_service_permissions
from app.models import SMS_TYPE, EMAIL_TYPE
from app.notifications.validators import service_has_permission
from app.schemas import (template_schema, template_history_schema)
template_blueprint = Blueprint('template', __name__, url_prefix='/service/<uuid:service_id>/template')
@@ -25,7 +26,7 @@ from app.errors import (
register_errors,
InvalidRequest
)
from app.utils import get_template_instance
from app.utils import get_template_instance, get_public_notify_type_text
register_errors(template_blueprint)
@@ -37,15 +38,6 @@ def _content_count_greater_than_limit(content, template_type):
return template.content_count > current_app.config.get('SMS_CHAR_COUNT_LIMIT')
def _service_has_permission(template_type, action, permissions):
if template_type not in [p.permission for p in permissions]:
template_type_text = template_type
if template_type == SMS_TYPE:
template_type_text = 'text message'
raise InvalidRequest("{action} {type} template is not allowed".format(
action=action, type=template_type_text), 403)
@template_blueprint.route('', methods=['POST'])
def create_template(service_id):
fetched_service = dao_fetch_service_by_id(service_id=service_id)
@@ -53,7 +45,10 @@ def create_template(service_id):
permissions = fetched_service.permissions
new_template = template_schema.load(request.get_json()).data
_service_has_permission(new_template.template_type, 'Create', permissions)
if service_has_permission(new_template.template_type, permissions) is False:
raise InvalidRequest(
"Creating {} templates is not allowed".format(
get_public_notify_type_text(new_template.template_type)), 403)
new_template.service = fetched_service
over_limit = _content_count_greater_than_limit(new_template.content, new_template.template_type)
@@ -71,7 +66,10 @@ def create_template(service_id):
def update_template(service_id, template_id):
fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id)
_service_has_permission(fetched_template.template_type, 'Update', fetched_template.service.permissions)
if service_has_permission(fetched_template.template_type, fetched_template.service.permissions) is False:
raise InvalidRequest(
"Updating {} templates is not allowed".format(
get_public_notify_type_text(fetched_template.template_type)), 403)
data = request.get_json()