Merge pull request #2269 from alphagov/choose_postage_on_template

Choose postage on template
This commit is contained in:
Pea (Malgorzata Tyczynska)
2019-01-02 14:16:38 +00:00
committed by GitHub
10 changed files with 171 additions and 24 deletions

View File

@@ -23,7 +23,8 @@ from app.models import (
LETTER_TYPE,
NOTIFICATION_CREATED,
Notification,
ScheduledNotification
ScheduledNotification,
CHOOSE_POSTAGE
)
from app.dao.notifications_dao import (
dao_create_notification,
@@ -31,6 +32,8 @@ from app.dao.notifications_dao import (
dao_created_scheduled_notification
)
from app.dao.templates_dao import dao_get_template_by_id
from app.v2.errors import BadRequestError
from app.utils import (
cache_key_for_service_template_counter,
@@ -109,7 +112,11 @@ def persist_notification(
elif notification_type == EMAIL_TYPE:
notification.normalised_to = format_email_address(notification.to)
elif notification_type == LETTER_TYPE:
notification.postage = service.postage
template = dao_get_template_by_id(template_id, template_version)
if service.has_permission(CHOOSE_POSTAGE) and template.postage:
notification.postage = template.postage
else:
notification.postage = service.postage
# if simulated create a Notification model to return but do not persist the Notification to the dB
if not simulated:

View File

@@ -31,7 +31,7 @@ from app.errors import (
InvalidRequest
)
from app.letters.utils import get_letter_pdf
from app.models import SMS_TYPE, Template
from app.models import SMS_TYPE, Template, CHOOSE_POSTAGE
from app.notifications.validators import service_has_permission, check_reply_to
from app.schema_validation import validate
from app.schemas import (template_schema, template_history_schema)
@@ -78,6 +78,12 @@ def create_template(service_id):
errors = {'template_type': [message]}
raise InvalidRequest(errors, 403)
if new_template.postage:
if not service_has_permission(CHOOSE_POSTAGE, fetched_service.permissions):
message = "Setting postage on templates is not enabled for this service."
errors = {'template_postage': [message]}
raise InvalidRequest(errors, 403)
new_template.service = fetched_service
over_limit = _content_count_greater_than_limit(new_template.content, new_template.template_type)
@@ -110,6 +116,12 @@ def update_template(service_id, template_id):
if data.get('redact_personalisation') is True:
return redact_template(fetched_template, data)
if data.get('postage'):
if not service_has_permission(CHOOSE_POSTAGE, fetched_template.service.permissions):
message = "Setting postage on templates is not enabled for this service."
errors = {'template_postage': [message]}
raise InvalidRequest(errors, 403)
if "reply_to" in data:
check_reply_to(service_id, data.get("reply_to"), fetched_template.template_type)
updated = dao_update_template_reply_to(template_id=template_id, reply_to=data.get("reply_to"))
@@ -191,7 +203,7 @@ def get_template_versions(service_id, template_id):
def _template_has_not_changed(current_data, updated_template):
return all(
current_data[key] == updated_template[key]
for key in ('name', 'content', 'subject', 'archived', 'process_type')
for key in ('name', 'content', 'subject', 'archived', 'process_type', 'postage')
)

View File

@@ -17,7 +17,8 @@ post_create_template_schema = {
"content": {"type": "string"},
"subject": {"type": "string"},
"created_by": uuid,
"parent_folder_id": uuid
"parent_folder_id": uuid,
"postage": {"type": "string"},
},
"if": {
"properties": {

View File

@@ -37,6 +37,7 @@ get_template_by_id_response = {
"body": {"type": "string"},
"subject": {"type": ["string", "null"]},
"name": {"type": "string"},
"postage": {"type": "string"}
},
"required": ["id", "type", "created_at", "updated_at", "version", "created_by", "body", "name"],
}
@@ -63,7 +64,8 @@ post_template_preview_response = {
"type": {"enum": TEMPLATE_TYPES},
"version": {"type": "integer"},
"body": {"type": "string"},
"subject": {"type": ["string", "null"]}
"subject": {"type": ["string", "null"]},
"postage": {"type": "string"}
},
"required": ["id", "type", "version", "body"]
}
@@ -77,5 +79,6 @@ def create_post_template_preview_response(template, template_object):
"type": template.template_type,
"version": template.version,
"body": str(template_object),
"subject": subject
"subject": subject,
"postage": template.postage
}