from datetime import datetime, timedelta
from string import ascii_uppercase

from dateutil.parser import parse
from flask import abort, flash, jsonify, redirect, render_template, request, url_for
from flask_babel import _
from flask_babel import lazy_gettext as _l
from flask_login import current_user
from markupsafe import Markup
from notifications_python_client.errors import HTTPError
from notifications_utils.formatters import nl2br
from notifications_utils.recipients import first_column_headings

from app import (
    current_service,
    service_api_client,
    template_api_prefill_client,
    template_folder_api_client,
    template_statistics_client,
)
from app.main import main
from app.main.forms import (
    CreateTemplateForm,
    EmailTemplateForm,
    LetterTemplateForm,
    LetterTemplatePostageForm,
    SearchByNameForm,
    SetTemplateSenderForm,
    SMSTemplateForm,
    TemplateAndFoldersSelectionForm,
    TemplateFolderForm,
)
from app.main.views.send import get_example_csv_rows, get_sender_details
from app.models.service import Service
from app.models.template_list import TemplateList, TemplateLists
from app.template_previews import TemplatePreview, get_page_count_for_letter
from app.utils import (
    email_or_sms_not_enabled,
    get_template,
    should_skip_template_page,
    user_has_permissions,
    user_is_platform_admin,
)

form_objects = {
    "email": EmailTemplateForm,
    "sms": SMSTemplateForm,
    "letter": LetterTemplateForm,
}


def get_email_preview_template(template, template_id, service_id):
    email_preview_template = get_template(
        template,
        current_service,
        letter_preview_url=url_for(
            ".view_letter_template_preview",
            service_id=service_id,
            template_id=template_id,
            filetype="png",
        ),
        show_recipient=True,
        page_count=get_page_count_for_letter(template),
    )

    return email_preview_template


@main.route("/services/<service_id>/templates/<uuid:template_id>")
@user_has_permissions()
def view_template(service_id, template_id):
    template = current_service.get_template(template_id)
    template_folder = current_service.get_template_folder(template["folder"])

    user_has_template_permission = current_user.has_template_folder_permission(template_folder)

    if should_skip_template_page(template["template_type"]):
        return redirect(url_for(".send_one_off", service_id=service_id, template_id=template_id))

    return render_template(
        "views/templates/template.html",
        template=get_email_preview_template(template, template_id, service_id),
        template_postage=template["postage"],
        user_has_template_permission=user_has_template_permission,
    )


@main.route("/services/<service_id>/start-tour/<uuid:template_id>")
@user_has_permissions("view_activity")
def start_tour(service_id, template_id):

    template = current_service.get_template(template_id)

    if template["template_type"] != "email":
        abort(404)

    return render_template(
        "views/templates/start-tour.html",
        template=get_template(
            template,
            current_service,
            show_recipient=True,
        ),
        help="1",
    )


@main.route("/services/<service_id>/templates", methods=["GET", "POST"])
@main.route(
    "/services/<service_id>/templates/folders/<template_folder_id>",
    methods=["GET", "POST"],
)
@main.route("/services/<service_id>/templates/<template_type>", methods=["GET", "POST"])
@main.route(
    "/services/<service_id>/templates/<template_type>/folders/<template_folder_id>",
    methods=["GET", "POST"],
)
@user_has_permissions()
def choose_template(service_id, template_type="all", template_folder_id=None):
    template_folder = current_service.get_template_folder(template_folder_id)

    user_has_template_folder_permission = current_user.has_template_folder_permission(template_folder)

    template_list = TemplateList(current_service, template_type, template_folder_id, current_user)

    templates_and_folders_form = TemplateAndFoldersSelectionForm(
        all_template_folders=current_service.get_user_template_folders(current_user),
        template_list=template_list,
        template_type=template_type,
        allow_adding_letter_template=current_service.has_permission("letter"),
        allow_adding_copy_of_template=(current_service.all_templates or len(current_user.service_ids) > 1),
    )
    option_hints = {template_folder_id: "current folder"}

    if request.method == "POST" and templates_and_folders_form.validate_on_submit():
        if not current_user.has_permissions("manage_templates"):
            abort(403)
        try:
            return process_folder_management_form(templates_and_folders_form, template_folder_id)
        except HTTPError as e:
            flash(e.message)

    if "templates_and_folders" in templates_and_folders_form.errors:
        flash(_("Select at least one template or folder"))

    initial_state = request.args.get("initial_state")
    if request.method == "GET" and initial_state:
        templates_and_folders_form.op = initial_state

    sending_view = request.args.get("view") == "sending"

    return render_template(
        "views/templates/choose.html",
        current_template_folder_id=template_folder_id,
        template_folder_path=current_service.get_template_folder_path(template_folder_id),
        template_list=template_list,
        show_search_box=current_service.count_of_templates_and_folders > 7,
        show_template_nav=(current_service.has_multiple_template_types and (len(current_service.all_templates) > 2)),
        sending_view=sending_view,
        template_nav_items=get_template_nav_items(template_folder_id, sending_view),
        template_type=template_type,
        search_form=SearchByNameForm(),
        templates_and_folders_form=templates_and_folders_form,
        move_to_children=templates_and_folders_form.move_to.children(),
        user_has_template_folder_permission=user_has_template_folder_permission,
        option_hints=option_hints,
    )


def process_folder_management_form(form, current_folder_id):
    current_service.get_template_folder_with_user_permission_or_403(current_folder_id, current_user)
    new_folder_id = None

    if form.is_add_folder_op:
        new_folder_id = template_folder_api_client.create_template_folder(
            current_service.id, name=form.get_folder_name(), parent_id=current_folder_id
        )

    if form.is_move_op:
        # if we've just made a folder, we also want to move there
        move_to_id = new_folder_id or form.move_to.data

        current_service.move_to_folder(ids_to_move=form.templates_and_folders.data, move_to=move_to_id)

    return redirect(request.url)


def get_template_nav_label(value):
    return {
        "all": _l("All"),
        "sms": _l("Text message"),
        "email": _l("Email"),
        "letter": _l("Letter"),
    }[value]


def get_template_nav_items(template_folder_id, sending_view):
    return [
        (
            get_template_nav_label(key),
            key,
            url_for(
                ".choose_template",
                service_id=current_service.id,
                template_type=key,
                template_folder_id=template_folder_id,
                view="sending" if sending_view else None,
            ),
            "",
        )
        for key in ["all"] + current_service.available_template_types
    ]


@main.route("/services/<service_id>/templates/<template_id>.<filetype>")
@user_has_permissions()
def view_letter_template_preview(service_id, template_id, filetype):
    if filetype not in ("pdf", "png"):
        abort(404)

    db_template = current_service.get_template(template_id)

    return TemplatePreview.from_database_object(db_template, filetype, page=request.args.get("page"))


@main.route("/templates/letter-preview-image/<filename>")
@user_is_platform_admin
def letter_branding_preview_image(filename):
    template = {
        "subject": "An example letter",
        "content": (
            "Lorem Ipsum is simply dummy text of the printing and typesetting "
            "industry.\n\nLorem Ipsum has been the industry’s standard dummy "
            "text ever since the 1500s, when an unknown printer took a galley "
            "of type and scrambled it to make a type specimen book.\n\n"
            "# History\n\nIt has survived not only\n\n"
            "* five centuries\n"
            "* but also the leap into electronic typesetting\n\n"
            "It was popularised in the 1960s with the release of Letraset "
            "sheets containing Lorem Ipsum passages, and more recently with "
            "desktop publishing software like Aldus PageMaker including "
            "versions of Lorem Ipsum.\n\n"
            "The point of using Lorem Ipsum is that it has a more-or-less "
            "normal distribution of letters, as opposed to using ‘Content "
            "here, content here’, making it look like readable English."
        ),
    }
    filename = None if filename == "no-branding" else filename

    return TemplatePreview.from_example_template(template, filename)


def _view_template_version(service_id, template_id, version, letters_as_pdf=False):
    return dict(
        template=get_template(
            current_service.get_template(template_id, version=version),
            current_service,
            letter_preview_url=url_for(
                ".view_template_version_preview",
                service_id=service_id,
                template_id=template_id,
                version=version,
                filetype="png",
            )
            if not letters_as_pdf
            else None,
        )
    )


@main.route("/services/<service_id>/templates/<template_id>/version/<int:version>")
@user_has_permissions()
def view_template_version(service_id, template_id, version):
    return render_template(
        "views/templates/template_history.html",
        **_view_template_version(service_id=service_id, template_id=template_id, version=version),
    )


@main.route("/services/<service_id>/templates/<template_id>/version/<int:version>.<filetype>")
@user_has_permissions()
def view_template_version_preview(service_id, template_id, version, filetype):
    db_template = current_service.get_template(template_id, version=version)
    return TemplatePreview.from_database_object(db_template, filetype)


def _add_template_by_type(template_type, template_folder_id):

    if template_type == "copy-existing":
        return redirect(
            url_for(
                ".choose_template_to_copy",
                service_id=current_service.id,
            )
        )

    if template_type == "letter":
        blank_letter = service_api_client.create_service_template(
            "New letter template",
            "letter",
            "Body",
            current_service.id,
            "Main heading",
            "normal",
            template_folder_id,
        )
        return redirect(
            url_for(
                ".view_template",
                service_id=current_service.id,
                template_id=blank_letter["data"]["id"],
            )
        )

    if email_or_sms_not_enabled(template_type, current_service.permissions):
        return redirect(
            url_for(
                ".action_blocked",
                service_id=current_service.id,
                notification_type=template_type,
                return_to="add_new_template",
                template_id="0",
            )
        )
    else:
        return redirect(
            url_for(
                ".add_service_template",
                service_id=current_service.id,
                template_type=template_type,
                template_folder_id=template_folder_id,
            )
        )


@main.route("/services/<service_id>/templates/create", methods=["GET", "POST"])
@main.route("/services/<service_id>/templates/folders/<template_folder_id>/create", methods=["GET", "POST"])
@main.route("/services/<service_id>/templates/<template_type>/create", methods=["GET", "POST"])
@main.route("/services/<service_id>/templates/<template_type>/folders/<template_folder_id>/create", methods=["GET", "POST"])
@user_has_permissions("manage_templates")
def create_template(service_id, template_type="all", template_folder_id=None):
    form = CreateTemplateForm()

    if request.method == "POST" and form.validate_on_submit():
        try:
            return _add_template_by_type(
                form.what_type.data,
                template_folder_id,
            )
        except HTTPError as e:
            flash(e.message)
    return render_template(
        "views/templates/create.html",
        service_id=service_id,
        template_folder_id=template_folder_id,
        template_type=template_type,
        form=form,
        disabled_options={},
        option_hints={},
    )


@main.route("/services/<service_id>/templates/copy")
@main.route("/services/<service_id>/templates/all/copy")
@main.route("/services/<service_id>/templates/email/copy")
@main.route("/services/<service_id>/templates/sms/copy")
@main.route("/services/<service_id>/templates/copy/from-folder/<uuid:from_folder>")
@main.route("/services/<service_id>/templates/copy/from-service/<uuid:from_service>")
@main.route("/services/<service_id>/templates/copy/from-service/<uuid:from_service>/from-folder/<uuid:from_folder>")
@main.route("/services/<service_id>/templates/all/folders/<uuid:from_folder>/copy")
@user_has_permissions("manage_templates")
def choose_template_to_copy(
    service_id,
    from_service=None,
    from_folder=None,
):

    if from_folder and from_service is None:
        from_service = service_id

    if from_service:

        current_user.belongs_to_service_or_403(from_service)
        service = Service(service_api_client.get_service(from_service)["data"])

        return render_template(
            "views/templates/copy.html",
            services_templates_and_folders=TemplateList(service, template_folder_id=from_folder, user=current_user),
            template_folder_path=service.get_template_folder_path(from_folder),
            from_service=service,
            search_form=SearchByNameForm(),
        )

    else:
        return render_template(
            "views/templates/copy.html",
            services_templates_and_folders=TemplateLists(current_user),
            search_form=SearchByNameForm(),
        )


@main.route("/services/<service_id>/templates/copy/<uuid:template_id>", methods=["GET", "POST"])
@user_has_permissions("manage_templates")
def copy_template(service_id, template_id):
    from_service = request.args.get("from_service")

    current_user.belongs_to_service_or_403(from_service)

    template = service_api_client.get_service_template(from_service, str(template_id))["data"]

    template_folder = template_folder_api_client.get_template_folder(from_service, template["folder"])
    if not current_user.has_template_folder_permission(template_folder):
        abort(403)

    if request.method == "POST":
        return add_service_template(
            service_id,
            template["template_type"],
            template_folder_id=template_folder.get("id"),
        )

    template["template_content"] = template["content"]
    template["name"] = _get_template_copy_name(template, current_service.all_templates)
    form = form_objects[template["template_type"]](**template)

    return render_template(
        f"views/edit-{template['template_type']}-template.html",
        form=form,
        template=template,
        heading=_l("Copy email template") if template["template_type"] == "email" else _l("Copy text message template"),
        service_id=service_id,
        services=current_user.service_ids,
    )


def _get_template_copy_name(template, existing_templates):

    template_names = [existing["name"] for existing in existing_templates]

    for index in reversed(range(1, 10)):
        if "{} (copy {})".format(template["name"], index) in template_names:
            return "{} (copy {})".format(template["name"], index + 1)

    if "{} (copy)".format(template["name"]) in template_names:
        return "{} (copy 2)".format(template["name"])

    return "{} (copy)".format(template["name"])


@main.route("/services/<service_id>/templates/action-blocked/<notification_type>/<return_to>/<template_id>")
@user_has_permissions("manage_templates")
def action_blocked(service_id, notification_type, return_to, template_id):
    if notification_type == "sms":
        notification_type = "text messages"
    elif notification_type == "email":
        notification_type = "emails"

    return render_template(
        "views/templates/action_blocked.html",
        service_id=service_id,
        notification_type=notification_type,
        return_to=return_to,
        template_id=template_id,
    )


@main.route(
    "/services/<service_id>/templates/folders/<template_folder_id>/manage",
    methods=["GET", "POST"],
)
@user_has_permissions("manage_templates")
def manage_template_folder(service_id, template_folder_id):
    template_folder = current_service.get_template_folder_with_user_permission_or_403(template_folder_id, current_user)
    form = TemplateFolderForm(
        name=template_folder["name"],
        users_with_permission=template_folder.get("users_with_permission", None),
        all_service_users=[user for user in current_service.active_users if user.id != current_user.id],
    )
    if form.validate_on_submit():
        if current_user.has_permissions("manage_service") and form.users_with_permission.all_service_users:
            users_with_permission = form.users_with_permission.data + [current_user.id]
        else:
            users_with_permission = None
        template_folder_api_client.update_template_folder(
            current_service.id,
            template_folder_id,
            name=form.name.data,
            users_with_permission=users_with_permission,
        )
        return redirect(
            url_for(
                ".choose_template",
                service_id=service_id,
                template_folder_id=template_folder_id,
            )
        )

    return render_template(
        "views/templates/manage-template-folder.html",
        form=form,
        template_folder_path=current_service.get_template_folder_path(template_folder_id),
        current_service_id=current_service.id,
        template_folder_id=template_folder_id,
        template_type="all",
    )


@main.route(
    "/services/<service_id>/templates/folders/<template_folder_id>/delete",
    methods=["GET", "POST"],
)
@user_has_permissions("manage_templates")
def delete_template_folder(service_id, template_folder_id):
    template_folder = current_service.get_template_folder_with_user_permission_or_403(template_folder_id, current_user)

    if len(current_service.get_template_folders_and_templates(template_type="all", template_folder_id=template_folder_id)) > 0:
        flash(_l("You must empty this folder before you can delete it"), "info")
        return redirect(
            url_for(
                ".choose_template",
                service_id=service_id,
                template_type="all",
                template_folder_id=template_folder_id,
            )
        )

    if request.method == "POST":

        try:
            template_folder_api_client.delete_template_folder(current_service.id, template_folder_id)

            return redirect(
                url_for(
                    ".choose_template",
                    service_id=service_id,
                    template_folder_id=template_folder["parent_id"],
                )
            )
        except HTTPError as e:
            msg = _l("Folder is not empty")
            if e.status_code == 400 and msg in e.message:
                flash(_("You must empty this folder before you can delete it"), "info")
                return redirect(
                    url_for(
                        ".choose_template",
                        service_id=service_id,
                        template_type="all",
                        template_folder_id=template_folder_id,
                    )
                )
            else:
                abort(500, e)
    else:
        flash(
            "{} ‘{}’ {}".format(
                _l("Are you sure you want to delete the"),
                template_folder["name"],
                _l("folder?"),
            ),
            "delete",
        )
        return manage_template_folder(service_id, template_folder_id)


@main.route("/services/templates/<template_id>/get-data", methods=["POST"])
def get_template_data(template_id):
    data = template_api_prefill_client.get_template(template_id)
    return jsonify({"result": data})


@main.route("/services/<service_id>/templates/add-<template_type>", methods=["GET", "POST"])
@main.route(
    "/services/<service_id>/templates/folders/<template_folder_id>/add-<template_type>",
    methods=["GET", "POST"],
)
@user_has_permissions("manage_templates")
def add_service_template(service_id, template_type, template_folder_id=None):

    if template_type not in ["sms", "email", "letter"]:
        abort(404)
    if not current_service.has_permission("letter") and template_type == "letter":
        abort(403)

    form = form_objects[template_type]()
    if form.validate_on_submit():
        if form.process_type.data != "normal":
            abort_403_if_not_admin_user()
        try:
            new_template = service_api_client.create_service_template(
                form.name.data,
                template_type,
                form.template_content.data,
                service_id,
                form.subject.data if hasattr(form, "subject") else None,
                form.process_type.data,
                template_folder_id,
            )
        except HTTPError as e:
            if (
                e.status_code == 400
                and "content" in e.message
                and any(["character count greater than" in x for x in e.message["content"]])
            ):
                form.template_content.errors.extend(e.message["content"])
            else:
                raise e
        else:
            flash(_("'{}' template saved").format(form.name.data), "default_with_tick")

            return redirect(
                url_for(
                    ".view_template",
                    service_id=service_id,
                    template_id=new_template["data"]["id"],
                )
            )

    if email_or_sms_not_enabled(template_type, current_service.permissions):
        return redirect(
            url_for(
                ".action_blocked",
                service_id=service_id,
                notification_type=template_type,
                template_folder_id=template_folder_id,
                return_to="templates",
                template_id="0",
            )
        )
    else:
        return render_template(
            f"views/edit-{template_type}-template.html",
            form=form,
            template_type=template_type,
            template_folder_id=template_folder_id,
            service_id=service_id,
            heading=_l("New email template") if template_type == "email" else _l("New text message template"),
        )


def abort_403_if_not_admin_user():
    if not current_user.platform_admin:
        abort(403)


@main.route("/services/<service_id>/templates/<template_id>/edit", methods=["GET", "POST"])
@user_has_permissions("manage_templates")
def edit_service_template(service_id, template_id):
    template = current_service.get_template_with_user_permission_or_403(template_id, current_user)
    template["template_content"] = template["content"]
    form = form_objects[template["template_type"]](**template)
    if form.validate_on_submit():
        if form.process_type.data != template["process_type"]:
            abort_403_if_not_admin_user()

        subject = form.subject.data if hasattr(form, "subject") else None

        new_template_data = {
            "name": form.name.data,
            "content": form.template_content.data,
            "subject": subject,
            "template_type": template["template_type"],
            "id": template["id"],
            "process_type": form.process_type.data,
            "reply_to_text": template["reply_to_text"],
        }

        new_template = get_template(new_template_data, current_service)
        template_change = get_template(template, current_service).compare_to(new_template)
        if template_change.placeholders_added and not request.form.get("confirm"):
            example_column_headings = first_column_headings[new_template.template_type] + list(new_template.placeholders)
            return render_template(
                "views/templates/breaking-change.html",
                template_change=template_change,
                new_template=new_template,
                column_headings=list(ascii_uppercase[: len(example_column_headings)]),
                example_rows=[
                    example_column_headings,
                    get_example_csv_rows(new_template),
                    get_example_csv_rows(new_template),
                ],
                form=form,
            )
        try:
            service_api_client.update_service_template(
                template_id,
                form.name.data,
                template["template_type"],
                form.template_content.data,
                service_id,
                subject,
                form.process_type.data,
            )
        except HTTPError as e:
            if e.status_code == 400:
                if "content" in e.message and any(["character count greater than" in x for x in e.message["content"]]):
                    form.template_content.errors.extend(e.message["content"])
                else:
                    raise e
            else:
                raise e
        else:
            flash(_("'{}' template saved").format(form.name.data), "default_with_tick")

            return redirect(url_for(".view_template", service_id=service_id, template_id=template_id))

    if email_or_sms_not_enabled(template["template_type"], current_service.permissions):
        return redirect(
            url_for(
                ".action_blocked",
                service_id=service_id,
                notification_type=template["template_type"],
                return_to="view_template",
                template_id=template_id,
            )
        )
    else:
        return render_template(
            f"views/edit-{template['template_type']}-template.html",
            form=form,
            template=template,
            heading=_l("Edit email template") if template["template_type"] == "email" else _l("Edit text message template"),
        )


@main.route("/services/<service_id>/templates/<template_id>/delete", methods=["GET", "POST"])
@user_has_permissions("manage_templates")
def delete_service_template(service_id, template_id):
    template = current_service.get_template_with_user_permission_or_403(template_id, current_user)

    if request.method == "POST":
        service_api_client.delete_service_template(service_id, template_id)
        return redirect(
            url_for(
                ".choose_template",
                service_id=service_id,
                template_folder_id=template["folder"],
            )
        )

    try:
        last_used_notification = template_statistics_client.get_template_statistics_for_template(service_id, template["id"])

        last_used_text = ""
        if not last_used_notification:
            last_used_text = _l("more than seven days")
        else:
            last_used_date = parse(last_used_notification["created_at"]).replace(tzinfo=None)
            last_used_text = get_human_readable_delta(last_used_date, datetime.utcnow())

        message = "{} {} {}".format(_l("This template was last used"), last_used_text, _l("ago."))

    except HTTPError as e:
        if e.status_code == 404:
            message = None
        else:
            raise e

    flash(
        [
            "{} ‘{}’?".format(_l("Are you sure you want to delete"), template["name"]),
            message,
        ],
        "delete",
    )
    return render_template(
        "views/templates/template.html",
        template=get_email_preview_template(template, template["id"], service_id),
        user_has_template_permission=True,
    )


@main.route("/services/<service_id>/templates/<template_id>/redact", methods=["GET"])
@user_has_permissions("manage_templates")
def confirm_redact_template(service_id, template_id):
    template = current_service.get_template_with_user_permission_or_403(template_id, current_user)

    return render_template(
        "views/templates/template.html",
        template=get_email_preview_template(template, template["id"], service_id),
        user_has_template_permission=True,
        show_redaction_message=True,
    )


@main.route("/services/<service_id>/templates/<template_id>/redact", methods=["POST"])
@user_has_permissions("manage_templates")
def redact_template(service_id, template_id):

    service_api_client.redact_service_template(service_id, template_id)

    flash(
        _("Personalised content will be hidden for messages sent with this template"),
        "default_with_tick",
    )

    return redirect(
        url_for(
            ".view_template",
            service_id=service_id,
            template_id=template_id,
        )
    )


@main.route("/services/<service_id>/templates/<template_id>/versions")
@user_has_permissions("view_activity")
def view_template_versions(service_id, template_id):
    return render_template(
        "views/templates/choose_history.html",
        versions=[
            get_template(
                template,
                current_service,
                letter_preview_url=url_for(
                    ".view_template_version_preview",
                    service_id=service_id,
                    template_id=template_id,
                    version=template["version"],
                    filetype="png",
                ),
            )
            for template in service_api_client.get_service_template_versions(service_id, template_id)["data"]
        ],
    )


@main.route(
    "/services/<service_id>/templates/<template_id>/set-template-sender",
    methods=["GET", "POST"],
)
@user_has_permissions("manage_templates")
def set_template_sender(service_id, template_id):
    template = current_service.get_template_with_user_permission_or_403(template_id, current_user)
    sender_details = get_template_sender_form_dict(service_id, template)
    no_senders = sender_details.get("no_senders", False)

    form = SetTemplateSenderForm(
        sender=sender_details["current_choice"],
        sender_choices=sender_details["value_and_label"],
    )
    option_hints = {sender_details["default_sender"]: "(Default)"}

    if form.validate_on_submit():
        service_api_client.update_service_template_sender(
            service_id,
            template_id,
            form.sender.data if form.sender.data else None,
        )
        return redirect(url_for(".view_template", service_id=service_id, template_id=template_id))

    return render_template(
        "views/templates/set-template-sender.html",
        form=form,
        template_id=template_id,
        no_senders=no_senders,
        option_hints=option_hints,
    )


@main.route(
    "/services/<service_id>/templates/<template_id>/edit-postage",
    methods=["GET", "POST"],
)
@user_has_permissions("manage_templates")
def edit_template_postage(service_id, template_id):
    template = current_service.get_template_with_user_permission_or_403(template_id, current_user)
    if template["template_type"] != "letter":
        abort(404)
    form = LetterTemplatePostageForm(**template)
    if form.validate_on_submit():
        postage = form.postage.data
        service_api_client.update_service_template_postage(service_id, template_id, postage)

        return redirect(url_for(".view_template", service_id=service_id, template_id=template_id))

    return render_template(
        "views/templates/edit-template-postage.html",
        form=form,
        service_id=service_id,
        template_id=template_id,
        template_postage=template["postage"],
    )


def get_template_sender_form_dict(service_id, template):
    context = {
        "email": {"field_name": "email_address"},
        "letter": {"field_name": "contact_block"},
        "sms": {"field_name": "sms_sender"},
    }[template["template_type"]]

    sender_format = context["field_name"]
    service_senders = get_sender_details(service_id, template["template_type"])
    context["default_sender"] = next((x["id"] for x in service_senders if x["is_default"]), "Not set")
    if not service_senders:
        context["no_senders"] = True

    context["value_and_label"] = [(sender["id"], Markup(nl2br(sender[sender_format]))) for sender in service_senders]
    context["value_and_label"].insert(0, ("", "Blank"))  # Add blank option to start of list

    context["current_choice"] = template["service_letter_contact"] if template["service_letter_contact"] else ""
    return context


def get_human_readable_delta(from_time, until_time):
    delta = until_time - from_time
    if delta < timedelta(seconds=60):
        return "under a minute"
    elif delta < timedelta(hours=1):
        minutes = int(delta.seconds / 60)
        return "{} minute{}".format(minutes, "" if minutes == 1 else "s")
    elif delta < timedelta(days=1):
        hours = int(delta.seconds / 3600)
        return "{} hour{}".format(hours, "" if hours == 1 else "s")
    else:
        days = delta.days
        return "{} day{}".format(days, "" if days == 1 else "s")

[((1959, 2024), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<uuid:template_id>"""'], {}), "('/services/<service_id>/templates/<uuid:template_id>')\n", (1969, 2024), False, 'from app.main import main\n'), ((2026, 2048), 'app.utils.user_has_permissions', 'user_has_permissions', ([], {}), '()\n', (2046, 2048), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((2757, 2823), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/start-tour/<uuid:template_id>"""'], {}), "('/services/<service_id>/start-tour/<uuid:template_id>')\n", (2767, 2823), False, 'from app.main import main\n'), ((2825, 2862), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""view_activity"""'], {}), "('view_activity')\n", (2845, 2862), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((3252, 3323), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates"""'], {'methods': "['GET', 'POST']"}), "('/services/<service_id>/templates', methods=['GET', 'POST'])\n", (3262, 3323), False, 'from app.main import main\n'), ((3325, 3429), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/folders/<template_folder_id>"""'], {'methods': "['GET', 'POST']"}), "('/services/<service_id>/templates/folders/<template_folder_id>',\n    methods=['GET', 'POST'])\n", (3335, 3429), False, 'from app.main import main\n'), ((3438, 3530), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<template_type>"""'], {'methods': "['GET', 'POST']"}), "('/services/<service_id>/templates/<template_type>', methods=[\n    'GET', 'POST'])\n", (3448, 3530), False, 'from app.main import main\n'), ((3527, 3653), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<template_type>/folders/<template_folder_id>"""'], {'methods': "['GET', 'POST']"}), "(\n    '/services/<service_id>/templates/<template_type>/folders/<template_folder_id>'\n    , methods=['GET', 'POST'])\n", (3537, 3653), False, 'from app.main import main\n'), ((3656, 3678), 'app.utils.user_has_permissions', 'user_has_permissions', ([], {}), '()\n', (3676, 3678), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((7491, 7562), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<template_id>.<filetype>"""'], {}), "('/services/<service_id>/templates/<template_id>.<filetype>')\n", (7501, 7562), False, 'from app.main import main\n'), ((7564, 7586), 'app.utils.user_has_permissions', 'user_has_permissions', ([], {}), '()\n', (7584, 7586), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((7881, 7937), 'app.main.main.route', 'main.route', (['"""/templates/letter-preview-image/<filename>"""'], {}), "('/templates/letter-preview-image/<filename>')\n", (7891, 7937), False, 'from app.main import main\n'), ((9758, 9845), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<template_id>/version/<int:version>"""'], {}), "(\n    '/services/<service_id>/templates/<template_id>/version/<int:version>')\n", (9768, 9845), False, 'from app.main import main\n'), ((9842, 9864), 'app.utils.user_has_permissions', 'user_has_permissions', ([], {}), '()\n', (9862, 9864), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((10111, 10214), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<template_id>/version/<int:version>.<filetype>"""'], {}), "(\n    '/services/<service_id>/templates/<template_id>/version/<int:version>.<filetype>'\n    )\n", (10121, 10214), False, 'from app.main import main\n'), ((10206, 10228), 'app.utils.user_has_permissions', 'user_has_permissions', ([], {}), '()\n', (10226, 10228), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((11862, 11940), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/create"""'], {'methods': "['GET', 'POST']"}), "('/services/<service_id>/templates/create', methods=['GET', 'POST'])\n", (11872, 11940), False, 'from app.main import main\n'), ((11942, 12058), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/folders/<template_folder_id>/create"""'], {'methods': "['GET', 'POST']"}), "(\n    '/services/<service_id>/templates/folders/<template_folder_id>/create',\n    methods=['GET', 'POST'])\n", (11952, 12058), False, 'from app.main import main\n'), ((12051, 12149), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<template_type>/create"""'], {'methods': "['GET', 'POST']"}), "('/services/<service_id>/templates/<template_type>/create',\n    methods=['GET', 'POST'])\n", (12061, 12149), False, 'from app.main import main\n'), ((12147, 12280), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<template_type>/folders/<template_folder_id>/create"""'], {'methods': "['GET', 'POST']"}), "(\n    '/services/<service_id>/templates/<template_type>/folders/<template_folder_id>/create'\n    , methods=['GET', 'POST'])\n", (12157, 12280), False, 'from app.main import main\n'), ((12272, 12312), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""manage_templates"""'], {}), "('manage_templates')\n", (12292, 12312), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((12954, 13005), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/copy"""'], {}), "('/services/<service_id>/templates/copy')\n", (12964, 13005), False, 'from app.main import main\n'), ((13007, 13062), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/all/copy"""'], {}), "('/services/<service_id>/templates/all/copy')\n", (13017, 13062), False, 'from app.main import main\n'), ((13064, 13121), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/email/copy"""'], {}), "('/services/<service_id>/templates/email/copy')\n", (13074, 13121), False, 'from app.main import main\n'), ((13123, 13178), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/sms/copy"""'], {}), "('/services/<service_id>/templates/sms/copy')\n", (13133, 13178), False, 'from app.main import main\n'), ((13180, 13267), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/copy/from-folder/<uuid:from_folder>"""'], {}), "(\n    '/services/<service_id>/templates/copy/from-folder/<uuid:from_folder>')\n", (13190, 13267), False, 'from app.main import main\n'), ((13264, 13353), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/copy/from-service/<uuid:from_service>"""'], {}), "(\n    '/services/<service_id>/templates/copy/from-service/<uuid:from_service>')\n", (13274, 13353), False, 'from app.main import main\n'), ((13350, 13475), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/copy/from-service/<uuid:from_service>/from-folder/<uuid:from_folder>"""'], {}), "(\n    '/services/<service_id>/templates/copy/from-service/<uuid:from_service>/from-folder/<uuid:from_folder>'\n    )\n", (13360, 13475), False, 'from app.main import main\n'), ((13467, 13554), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/all/folders/<uuid:from_folder>/copy"""'], {}), "(\n    '/services/<service_id>/templates/all/folders/<uuid:from_folder>/copy')\n", (13477, 13554), False, 'from app.main import main\n'), ((13551, 13591), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""manage_templates"""'], {}), "('manage_templates')\n", (13571, 13591), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((14501, 14600), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/copy/<uuid:template_id>"""'], {'methods': "['GET', 'POST']"}), "('/services/<service_id>/templates/copy/<uuid:template_id>',\n    methods=['GET', 'POST'])\n", (14511, 14600), False, 'from app.main import main\n'), ((14598, 14638), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""manage_templates"""'], {}), "('manage_templates')\n", (14618, 14638), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((16331, 16448), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/action-blocked/<notification_type>/<return_to>/<template_id>"""'], {}), "(\n    '/services/<service_id>/templates/action-blocked/<notification_type>/<return_to>/<template_id>'\n    )\n", (16341, 16448), False, 'from app.main import main\n'), ((16440, 16480), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""manage_templates"""'], {}), "('manage_templates')\n", (16460, 16480), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((16934, 17050), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/folders/<template_folder_id>/manage"""'], {'methods': "['GET', 'POST']"}), "(\n    '/services/<service_id>/templates/folders/<template_folder_id>/manage',\n    methods=['GET', 'POST'])\n", (16944, 17050), False, 'from app.main import main\n'), ((17054, 17094), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""manage_templates"""'], {}), "('manage_templates')\n", (17074, 17094), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((18571, 18687), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/folders/<template_folder_id>/delete"""'], {'methods': "['GET', 'POST']"}), "(\n    '/services/<service_id>/templates/folders/<template_folder_id>/delete',\n    methods=['GET', 'POST'])\n", (18581, 18687), False, 'from app.main import main\n'), ((18691, 18731), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""manage_templates"""'], {}), "('manage_templates')\n", (18711, 18731), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((20627, 20701), 'app.main.main.route', 'main.route', (['"""/services/templates/<template_id>/get-data"""'], {'methods': "['POST']"}), "('/services/templates/<template_id>/get-data', methods=['POST'])\n", (20637, 20701), False, 'from app.main import main\n'), ((20843, 20939), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/add-<template_type>"""'], {'methods': "['GET', 'POST']"}), "('/services/<service_id>/templates/add-<template_type>', methods=\n    ['GET', 'POST'])\n", (20853, 20939), False, 'from app.main import main\n'), ((20936, 21066), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/folders/<template_folder_id>/add-<template_type>"""'], {'methods': "['GET', 'POST']"}), "(\n    '/services/<service_id>/templates/folders/<template_folder_id>/add-<template_type>'\n    , methods=['GET', 'POST'])\n", (20946, 21066), False, 'from app.main import main\n'), ((21069, 21109), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""manage_templates"""'], {}), "('manage_templates')\n", (21089, 21109), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((23462, 23557), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<template_id>/edit"""'], {'methods': "['GET', 'POST']"}), "('/services/<service_id>/templates/<template_id>/edit', methods=[\n    'GET', 'POST'])\n", (23472, 23557), False, 'from app.main import main\n'), ((23554, 23594), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""manage_templates"""'], {}), "('manage_templates')\n", (23574, 23594), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((26894, 26991), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<template_id>/delete"""'], {'methods': "['GET', 'POST']"}), "('/services/<service_id>/templates/<template_id>/delete', methods\n    =['GET', 'POST'])\n", (26904, 26991), False, 'from app.main import main\n'), ((26988, 27028), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""manage_templates"""'], {}), "('manage_templates')\n", (27008, 27028), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((28540, 28629), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<template_id>/redact"""'], {'methods': "['GET']"}), "('/services/<service_id>/templates/<template_id>/redact', methods\n    =['GET'])\n", (28550, 28629), False, 'from app.main import main\n'), ((28626, 28666), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""manage_templates"""'], {}), "('manage_templates')\n", (28646, 28666), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((29062, 29152), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<template_id>/redact"""'], {'methods': "['POST']"}), "('/services/<service_id>/templates/<template_id>/redact', methods\n    =['POST'])\n", (29072, 29152), False, 'from app.main import main\n'), ((29149, 29189), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""manage_templates"""'], {}), "('manage_templates')\n", (29169, 29189), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((29603, 29672), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<template_id>/versions"""'], {}), "('/services/<service_id>/templates/<template_id>/versions')\n", (29613, 29672), False, 'from app.main import main\n'), ((29674, 29711), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""view_activity"""'], {}), "('view_activity')\n", (29694, 29711), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((30378, 30488), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<template_id>/set-template-sender"""'], {'methods': "['GET', 'POST']"}), "('/services/<service_id>/templates/<template_id>/set-template-sender'\n    , methods=['GET', 'POST'])\n", (30388, 30488), False, 'from app.main import main\n'), ((30496, 30536), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""manage_templates"""'], {}), "('manage_templates')\n", (30516, 30536), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((31551, 31653), 'app.main.main.route', 'main.route', (['"""/services/<service_id>/templates/<template_id>/edit-postage"""'], {'methods': "['GET', 'POST']"}), "('/services/<service_id>/templates/<template_id>/edit-postage',\n    methods=['GET', 'POST'])\n", (31561, 31653), False, 'from app.main import main\n'), ((31662, 31702), 'app.utils.user_has_permissions', 'user_has_permissions', (['"""manage_templates"""'], {}), "('manage_templates')\n", (31682, 31702), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((2108, 2149), 'app.current_service.get_template', 'current_service.get_template', (['template_id'], {}), '(template_id)\n', (2136, 2149), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((2172, 2227), 'app.current_service.get_template_folder', 'current_service.get_template_folder', (["template['folder']"], {}), "(template['folder'])\n", (2207, 2227), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((2264, 2324), 'flask_login.current_user.has_template_folder_permission', 'current_user.has_template_folder_permission', (['template_folder'], {}), '(template_folder)\n', (2307, 2324), False, 'from flask_login import current_user\n'), ((2333, 2385), 'app.utils.should_skip_template_page', 'should_skip_template_page', (["template['template_type']"], {}), "(template['template_type'])\n", (2358, 2385), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((2920, 2961), 'app.current_service.get_template', 'current_service.get_template', (['template_id'], {}), '(template_id)\n', (2948, 2961), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((3780, 3835), 'app.current_service.get_template_folder', 'current_service.get_template_folder', (['template_folder_id'], {}), '(template_folder_id)\n', (3815, 3835), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((3879, 3939), 'flask_login.current_user.has_template_folder_permission', 'current_user.has_template_folder_permission', (['template_folder'], {}), '(template_folder)\n', (3922, 3939), False, 'from flask_login import current_user\n'), ((3961, 4039), 'app.models.template_list.TemplateList', 'TemplateList', (['current_service', 'template_type', 'template_folder_id', 'current_user'], {}), '(current_service, template_type, template_folder_id, current_user)\n', (3973, 4039), False, 'from app.models.template_list import TemplateList, TemplateLists\n'), ((5013, 5046), 'flask.request.args.get', 'request.args.get', (['"""initial_state"""'], {}), "('initial_state')\n", (5029, 5046), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((6175, 6276), 'app.current_service.get_template_folder_with_user_permission_or_403', 'current_service.get_template_folder_with_user_permission_or_403', (['current_folder_id', 'current_user'], {}), '(\n    current_folder_id, current_user)\n', (6238, 6276), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((6766, 6787), 'flask.redirect', 'redirect', (['request.url'], {}), '(request.url)\n', (6774, 6787), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((7733, 7774), 'app.current_service.get_template', 'current_service.get_template', (['template_id'], {}), '(template_id)\n', (7761, 7774), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((9140, 9197), 'app.template_previews.TemplatePreview.from_example_template', 'TemplatePreview.from_example_template', (['template', 'filename'], {}), '(template, filename)\n', (9177, 9197), False, 'from app.template_previews import TemplatePreview, get_page_count_for_letter\n'), ((10326, 10384), 'app.current_service.get_template', 'current_service.get_template', (['template_id'], {'version': 'version'}), '(template_id, version=version)\n', (10354, 10384), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((10396, 10455), 'app.template_previews.TemplatePreview.from_database_object', 'TemplatePreview.from_database_object', (['db_template', 'filetype'], {}), '(db_template, filetype)\n', (10432, 10455), False, 'from app.template_previews import TemplatePreview, get_page_count_for_letter\n'), ((11241, 11309), 'app.utils.email_or_sms_not_enabled', 'email_or_sms_not_enabled', (['template_type', 'current_service.permissions'], {}), '(template_type, current_service.permissions)\n', (11265, 11309), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((12403, 12423), 'app.main.forms.CreateTemplateForm', 'CreateTemplateForm', ([], {}), '()\n', (12421, 12423), False, 'from app.main.forms import CreateTemplateForm, EmailTemplateForm, LetterTemplateForm, LetterTemplatePostageForm, SearchByNameForm, SetTemplateSenderForm, SMSTemplateForm, TemplateAndFoldersSelectionForm, TemplateFolderForm\n'), ((12701, 12895), 'flask.render_template', 'render_template', (['"""views/templates/create.html"""'], {'service_id': 'service_id', 'template_folder_id': 'template_folder_id', 'template_type': 'template_type', 'form': 'form', 'disabled_options': '{}', 'option_hints': '{}'}), "('views/templates/create.html', service_id=service_id,\n    template_folder_id=template_folder_id, template_type=template_type,\n    form=form, disabled_options={}, option_hints={})\n", (12716, 12895), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((14702, 14734), 'flask.request.args.get', 'request.args.get', (['"""from_service"""'], {}), "('from_service')\n", (14718, 14734), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((14740, 14792), 'flask_login.current_user.belongs_to_service_or_403', 'current_user.belongs_to_service_or_403', (['from_service'], {}), '(from_service)\n', (14778, 14792), False, 'from flask_login import current_user\n'), ((14912, 14997), 'app.template_folder_api_client.get_template_folder', 'template_folder_api_client.get_template_folder', (['from_service', "template['folder']"], {}), "(from_service, template['folder']\n    )\n", (14958, 14997), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((16723, 16892), 'flask.render_template', 'render_template', (['"""views/templates/action_blocked.html"""'], {'service_id': 'service_id', 'notification_type': 'notification_type', 'return_to': 'return_to', 'template_id': 'template_id'}), "('views/templates/action_blocked.html', service_id=\n    service_id, notification_type=notification_type, return_to=return_to,\n    template_id=template_id)\n", (16738, 16892), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((17177, 17279), 'app.current_service.get_template_folder_with_user_permission_or_403', 'current_service.get_template_folder_with_user_permission_or_403', (['template_folder_id', 'current_user'], {}), '(\n    template_folder_id, current_user)\n', (17240, 17279), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((18814, 18916), 'app.current_service.get_template_folder_with_user_permission_or_403', 'current_service.get_template_folder_with_user_permission_or_403', (['template_folder_id', 'current_user'], {}), '(\n    template_folder_id, current_user)\n', (18877, 18916), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((20749, 20802), 'app.template_api_prefill_client.get_template', 'template_api_prefill_client.get_template', (['template_id'], {}), '(template_id)\n', (20789, 20802), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((20814, 20839), 'flask.jsonify', 'jsonify', (["{'result': data}"], {}), "({'result': data})\n", (20821, 20839), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((22603, 22671), 'app.utils.email_or_sms_not_enabled', 'email_or_sms_not_enabled', (['template_type', 'current_service.permissions'], {}), '(template_type, current_service.permissions)\n', (22627, 22671), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((23662, 23749), 'app.current_service.get_template_with_user_permission_or_403', 'current_service.get_template_with_user_permission_or_403', (['template_id', 'current_user'], {}), '(template_id,\n    current_user)\n', (23718, 23749), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((26220, 26305), 'app.utils.email_or_sms_not_enabled', 'email_or_sms_not_enabled', (["template['template_type']", 'current_service.permissions'], {}), "(template['template_type'], current_service.permissions\n    )\n", (26244, 26305), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((27098, 27185), 'app.current_service.get_template_with_user_permission_or_403', 'current_service.get_template_with_user_permission_or_403', (['template_id', 'current_user'], {}), '(template_id,\n    current_user)\n', (27154, 27185), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((28736, 28823), 'app.current_service.get_template_with_user_permission_or_403', 'current_service.get_template_with_user_permission_or_403', (['template_id', 'current_user'], {}), '(template_id,\n    current_user)\n', (28792, 28823), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((29241, 29308), 'app.service_api_client.redact_service_template', 'service_api_client.redact_service_template', (['service_id', 'template_id'], {}), '(service_id, template_id)\n', (29283, 29308), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((30602, 30689), 'app.current_service.get_template_with_user_permission_or_403', 'current_service.get_template_with_user_permission_or_403', (['template_id', 'current_user'], {}), '(template_id,\n    current_user)\n', (30658, 30689), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((30828, 30944), 'app.main.forms.SetTemplateSenderForm', 'SetTemplateSenderForm', ([], {'sender': "sender_details['current_choice']", 'sender_choices': "sender_details['value_and_label']"}), "(sender=sender_details['current_choice'],\n    sender_choices=sender_details['value_and_label'])\n", (30849, 30944), False, 'from app.main.forms import CreateTemplateForm, EmailTemplateForm, LetterTemplateForm, LetterTemplatePostageForm, SearchByNameForm, SetTemplateSenderForm, SMSTemplateForm, TemplateAndFoldersSelectionForm, TemplateFolderForm\n'), ((31355, 31504), 'flask.render_template', 'render_template', (['"""views/templates/set-template-sender.html"""'], {'form': 'form', 'template_id': 'template_id', 'no_senders': 'no_senders', 'option_hints': 'option_hints'}), "('views/templates/set-template-sender.html', form=form,\n    template_id=template_id, no_senders=no_senders, option_hints=option_hints)\n", (31370, 31504), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((31770, 31857), 'app.current_service.get_template_with_user_permission_or_403', 'current_service.get_template_with_user_permission_or_403', (['template_id', 'current_user'], {}), '(template_id,\n    current_user)\n', (31826, 31857), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((31930, 31967), 'app.main.forms.LetterTemplatePostageForm', 'LetterTemplatePostageForm', ([], {}), '(**template)\n', (31955, 31967), False, 'from app.main.forms import CreateTemplateForm, EmailTemplateForm, LetterTemplateForm, LetterTemplatePostageForm, SearchByNameForm, SetTemplateSenderForm, SMSTemplateForm, TemplateAndFoldersSelectionForm, TemplateFolderForm\n'), ((32243, 32410), 'flask.render_template', 'render_template', (['"""views/templates/edit-template-postage.html"""'], {'form': 'form', 'service_id': 'service_id', 'template_id': 'template_id', 'template_postage': "template['postage']"}), "('views/templates/edit-template-postage.html', form=form,\n    service_id=service_id, template_id=template_id, template_postage=\n    template['postage'])\n", (32258, 32410), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((32768, 32825), 'app.main.views.send.get_sender_details', 'get_sender_details', (['service_id', "template['template_type']"], {}), "(service_id, template['template_type'])\n", (32786, 32825), False, 'from app.main.views.send import get_example_csv_rows, get_sender_details\n'), ((3016, 3026), 'flask.abort', 'abort', (['(404)'], {}), '(404)\n', (3021, 3026), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((5171, 5195), 'flask.request.args.get', 'request.args.get', (['"""view"""'], {}), "('view')\n", (5187, 5195), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((6658, 6757), 'app.current_service.move_to_folder', 'current_service.move_to_folder', ([], {'ids_to_move': 'form.templates_and_folders.data', 'move_to': 'move_to_id'}), '(ids_to_move=form.templates_and_folders.data,\n    move_to=move_to_id)\n', (6688, 6757), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((7703, 7713), 'flask.abort', 'abort', (['(404)'], {}), '(404)\n', (7708, 7713), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((10781, 10934), 'app.service_api_client.create_service_template', 'service_api_client.create_service_template', (['"""New letter template"""', '"""letter"""', '"""Body"""', 'current_service.id', '"""Main heading"""', '"""normal"""', 'template_folder_id'], {}), "('New letter template', 'letter',\n    'Body', current_service.id, 'Main heading', 'normal', template_folder_id)\n", (10823, 10934), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((13796, 13848), 'flask_login.current_user.belongs_to_service_or_403', 'current_user.belongs_to_service_or_403', (['from_service'], {}), '(from_service)\n', (13834, 13848), False, 'from flask_login import current_user\n'), ((15004, 15064), 'flask_login.current_user.has_template_folder_permission', 'current_user.has_template_folder_permission', (['template_folder'], {}), '(template_folder)\n', (15047, 15064), False, 'from flask_login import current_user\n'), ((15074, 15084), 'flask.abort', 'abort', (['(403)'], {}), '(403)\n', (15079, 15084), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((17830, 17994), 'app.template_folder_api_client.update_template_folder', 'template_folder_api_client.update_template_folder', (['current_service.id', 'template_folder_id'], {'name': 'form.name.data', 'users_with_permission': 'users_with_permission'}), '(current_service.id,\n    template_folder_id, name=form.name.data, users_with_permission=\n    users_with_permission)\n', (17879, 17994), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((21253, 21263), 'flask.abort', 'abort', (['(404)'], {}), '(404)\n', (21258, 21263), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((21355, 21365), 'flask.abort', 'abort', (['(403)'], {}), '(403)\n', (21360, 21365), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((23448, 23458), 'flask.abort', 'abort', (['(403)'], {}), '(403)\n', (23453, 23458), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((24460, 24508), 'app.utils.get_template', 'get_template', (['new_template_data', 'current_service'], {}), '(new_template_data, current_service)\n', (24472, 24508), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((27224, 27291), 'app.service_api_client.delete_service_template', 'service_api_client.delete_service_template', (['service_id', 'template_id'], {}), '(service_id, template_id)\n', (27266, 27291), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((27535, 27630), 'app.template_statistics_client.get_template_statistics_for_template', 'template_statistics_client.get_template_statistics_for_template', (['service_id', "template['id']"], {}), "(service_id,\n    template['id'])\n", (27598, 27630), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((29329, 29406), 'flask_babel._', '_', (['"""Personalised content will be hidden for messages sent with this template"""'], {}), "('Personalised content will be hidden for messages sent with this template')\n", (29330, 29406), False, 'from flask_babel import _\n'), ((29473, 29546), 'flask.url_for', 'url_for', (['""".view_template"""'], {'service_id': 'service_id', 'template_id': 'template_id'}), "('.view_template', service_id=service_id, template_id=template_id)\n", (29480, 29546), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((31074, 31201), 'app.service_api_client.update_service_template_sender', 'service_api_client.update_service_template_sender', (['service_id', 'template_id', '(form.sender.data if form.sender.data else None)'], {}), '(service_id, template_id, \n    form.sender.data if form.sender.data else None)\n', (31123, 31201), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((31908, 31918), 'flask.abort', 'abort', (['(404)'], {}), '(404)\n', (31913, 31918), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((32046, 32134), 'app.service_api_client.update_service_template_postage', 'service_api_client.update_service_template_postage', (['service_id', 'template_id', 'postage'], {}), '(service_id, template_id,\n    postage)\n', (32096, 32134), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((33444, 33465), 'datetime.timedelta', 'timedelta', ([], {'seconds': '(60)'}), '(seconds=60)\n', (33453, 33465), False, 'from datetime import datetime, timedelta\n'), ((1665, 1774), 'flask.url_for', 'url_for', (['""".view_letter_template_preview"""'], {'service_id': 'service_id', 'template_id': 'template_id', 'filetype': '"""png"""'}), "('.view_letter_template_preview', service_id=service_id, template_id\n    =template_id, filetype='png')\n", (1672, 1774), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((1878, 1913), 'app.template_previews.get_page_count_for_letter', 'get_page_count_for_letter', (['template'], {}), '(template)\n', (1903, 1913), False, 'from app.template_previews import TemplatePreview, get_page_count_for_letter\n'), ((2411, 2483), 'flask.url_for', 'url_for', (['""".send_one_off"""'], {'service_id': 'service_id', 'template_id': 'template_id'}), "('.send_one_off', service_id=service_id, template_id=template_id)\n", (2418, 2483), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((3116, 3176), 'app.utils.get_template', 'get_template', (['template', 'current_service'], {'show_recipient': '(True)'}), '(template, current_service, show_recipient=True)\n', (3128, 3176), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((4136, 4191), 'app.current_service.get_user_template_folders', 'current_service.get_user_template_folders', (['current_user'], {}), '(current_user)\n', (4177, 4191), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((4304, 4344), 'app.current_service.has_permission', 'current_service.has_permission', (['"""letter"""'], {}), "('letter')\n", (4334, 4344), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((4619, 4667), 'flask_login.current_user.has_permissions', 'current_user.has_permissions', (['"""manage_templates"""'], {}), "('manage_templates')\n", (4647, 4667), False, 'from flask_login import current_user\n'), ((4681, 4691), 'flask.abort', 'abort', (['(403)'], {}), '(403)\n', (4686, 4691), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((4947, 4990), 'flask_babel._', '_', (['"""Select at least one template or folder"""'], {}), "('Select at least one template or folder')\n", (4948, 4990), False, 'from flask_babel import _\n'), ((5361, 5421), 'app.current_service.get_template_folder_path', 'current_service.get_template_folder_path', (['template_folder_id'], {}), '(template_folder_id)\n', (5401, 5421), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((5831, 5849), 'app.main.forms.SearchByNameForm', 'SearchByNameForm', ([], {}), '()\n', (5847, 5849), False, 'from app.main.forms import CreateTemplateForm, EmailTemplateForm, LetterTemplateForm, LetterTemplatePostageForm, SearchByNameForm, SetTemplateSenderForm, SMSTemplateForm, TemplateAndFoldersSelectionForm, TemplateFolderForm\n'), ((6853, 6862), 'flask_babel.lazy_gettext', '_l', (['"""All"""'], {}), "('All')\n", (6855, 6862), True, 'from flask_babel import lazy_gettext as _l\n'), ((6879, 6897), 'flask_babel.lazy_gettext', '_l', (['"""Text message"""'], {}), "('Text message')\n", (6881, 6897), True, 'from flask_babel import lazy_gettext as _l\n'), ((6916, 6927), 'flask_babel.lazy_gettext', '_l', (['"""Email"""'], {}), "('Email')\n", (6918, 6927), True, 'from flask_babel import lazy_gettext as _l\n'), ((6947, 6959), 'flask_babel.lazy_gettext', '_l', (['"""Letter"""'], {}), "('Letter')\n", (6949, 6959), True, 'from flask_babel import lazy_gettext as _l\n'), ((7131, 7298), 'flask.url_for', 'url_for', (['""".choose_template"""'], {'service_id': 'current_service.id', 'template_type': 'key', 'template_folder_id': 'template_folder_id', 'view': "('sending' if sending_view else None)"}), "('.choose_template', service_id=current_service.id, template_type=\n    key, template_folder_id=template_folder_id, view='sending' if\n    sending_view else None)\n", (7138, 7298), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((7852, 7876), 'flask.request.args.get', 'request.args.get', (['"""page"""'], {}), "('page')\n", (7868, 7876), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((10599, 10665), 'flask.url_for', 'url_for', (['""".choose_template_to_copy"""'], {'service_id': 'current_service.id'}), "('.choose_template_to_copy', service_id=current_service.id)\n", (10606, 10665), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((11063, 11164), 'flask.url_for', 'url_for', (['""".view_template"""'], {'service_id': 'current_service.id', 'template_id': "blank_letter['data']['id']"}), "('.view_template', service_id=current_service.id, template_id=\n    blank_letter['data']['id'])\n", (11070, 11164), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((11348, 11490), 'flask.url_for', 'url_for', (['""".action_blocked"""'], {'service_id': 'current_service.id', 'notification_type': 'template_type', 'return_to': '"""add_new_template"""', 'template_id': '"""0"""'}), "('.action_blocked', service_id=current_service.id, notification_type\n    =template_type, return_to='add_new_template', template_id='0')\n", (11355, 11490), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((11638, 11773), 'flask.url_for', 'url_for', (['""".add_service_template"""'], {'service_id': 'current_service.id', 'template_type': 'template_type', 'template_folder_id': 'template_folder_id'}), "('.add_service_template', service_id=current_service.id,\n    template_type=template_type, template_folder_id=template_folder_id)\n", (11645, 11773), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((17582, 17628), 'flask_login.current_user.has_permissions', 'current_user.has_permissions', (['"""manage_service"""'], {}), "('manage_service')\n", (17610, 17628), False, 'from flask_login import current_user\n'), ((18082, 18176), 'flask.url_for', 'url_for', (['""".choose_template"""'], {'service_id': 'service_id', 'template_folder_id': 'template_folder_id'}), "('.choose_template', service_id=service_id, template_folder_id=\n    template_folder_id)\n", (18089, 18176), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((18377, 18437), 'app.current_service.get_template_folder_path', 'current_service.get_template_folder_path', (['template_folder_id'], {}), '(template_folder_id)\n', (18417, 18437), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((18924, 19038), 'app.current_service.get_template_folders_and_templates', 'current_service.get_template_folders_and_templates', ([], {'template_type': '"""all"""', 'template_folder_id': 'template_folder_id'}), "(template_type='all',\n    template_folder_id=template_folder_id)\n", (18974, 19038), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((19055, 19112), 'flask_babel.lazy_gettext', '_l', (['"""You must empty this folder before you can delete it"""'], {}), "('You must empty this folder before you can delete it')\n", (19057, 19112), True, 'from flask_babel import lazy_gettext as _l\n'), ((19159, 19273), 'flask.url_for', 'url_for', (['""".choose_template"""'], {'service_id': 'service_id', 'template_type': '"""all"""', 'template_folder_id': 'template_folder_id'}), "('.choose_template', service_id=service_id, template_type='all',\n    template_folder_id=template_folder_id)\n", (19166, 19273), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((19419, 19512), 'app.template_folder_api_client.delete_template_folder', 'template_folder_api_client.delete_template_folder', (['current_service.id', 'template_folder_id'], {}), '(current_service.id,\n    template_folder_id)\n', (19468, 19512), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((21275, 21315), 'app.current_service.has_permission', 'current_service.has_permission', (['"""letter"""'], {}), "('letter')\n", (21305, 21315), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((22710, 22881), 'flask.url_for', 'url_for', (['""".action_blocked"""'], {'service_id': 'service_id', 'notification_type': 'template_type', 'template_folder_id': 'template_folder_id', 'return_to': '"""templates"""', 'template_id': '"""0"""'}), "('.action_blocked', service_id=service_id, notification_type=\n    template_type, template_folder_id=template_folder_id, return_to=\n    'templates', template_id='0')\n", (22717, 22881), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((25349, 25528), 'app.service_api_client.update_service_template', 'service_api_client.update_service_template', (['template_id', 'form.name.data', "template['template_type']", 'form.template_content.data', 'service_id', 'subject', 'form.process_type.data'], {}), "(template_id, form.name.data,\n    template['template_type'], form.template_content.data, service_id,\n    subject, form.process_type.data)\n", (25391, 25528), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((26339, 26495), 'flask.url_for', 'url_for', (['""".action_blocked"""'], {'service_id': 'service_id', 'notification_type': "template['template_type']", 'return_to': '"""view_template"""', 'template_id': 'template_id'}), "('.action_blocked', service_id=service_id, notification_type=\n    template['template_type'], return_to='view_template', template_id=\n    template_id)\n", (26346, 26495), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((27329, 27423), 'flask.url_for', 'url_for', (['""".choose_template"""'], {'service_id': 'service_id', 'template_folder_id': "template['folder']"}), "('.choose_template', service_id=service_id, template_folder_id=\n    template['folder'])\n", (27336, 27423), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((27724, 27750), 'flask_babel.lazy_gettext', '_l', (['"""more than seven days"""'], {}), "('more than seven days')\n", (27726, 27750), True, 'from flask_babel import lazy_gettext as _l\n'), ((27985, 28018), 'flask_babel.lazy_gettext', '_l', (['"""This template was last used"""'], {}), "('This template was last used')\n", (27987, 28018), True, 'from flask_babel import lazy_gettext as _l\n'), ((28036, 28046), 'flask_babel.lazy_gettext', '_l', (['"""ago."""'], {}), "('ago.')\n", (28038, 28046), True, 'from flask_babel import lazy_gettext as _l\n'), ((31268, 31341), 'flask.url_for', 'url_for', (['""".view_template"""'], {'service_id': 'service_id', 'template_id': 'template_id'}), "('.view_template', service_id=service_id, template_id=template_id)\n", (31275, 31341), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((32156, 32229), 'flask.url_for', 'url_for', (['""".view_template"""'], {'service_id': 'service_id', 'template_id': 'template_id'}), "('.view_template', service_id=service_id, template_id=template_id)\n", (32163, 32229), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((33516, 33534), 'datetime.timedelta', 'timedelta', ([], {'hours': '(1)'}), '(hours=1)\n', (33525, 33534), False, 'from datetime import datetime, timedelta\n'), ((4846, 4862), 'flask.flash', 'flash', (['e.message'], {}), '(e.message)\n', (4851, 4862), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((9344, 9402), 'app.current_service.get_template', 'current_service.get_template', (['template_id'], {'version': 'version'}), '(template_id, version=version)\n', (9372, 9402), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((12673, 12689), 'flask.flash', 'flash', (['e.message'], {}), '(e.message)\n', (12678, 12689), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((13875, 13919), 'app.service_api_client.get_service', 'service_api_client.get_service', (['from_service'], {}), '(from_service)\n', (13905, 13919), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n'), ((14046, 14118), 'app.models.template_list.TemplateList', 'TemplateList', (['service'], {'template_folder_id': 'from_folder', 'user': 'current_user'}), '(service, template_folder_id=from_folder, user=current_user)\n', (14058, 14118), False, 'from app.models.template_list import TemplateList, TemplateLists\n'), ((14258, 14276), 'app.main.forms.SearchByNameForm', 'SearchByNameForm', ([], {}), '()\n', (14274, 14276), False, 'from app.main.forms import CreateTemplateForm, EmailTemplateForm, LetterTemplateForm, LetterTemplatePostageForm, SearchByNameForm, SetTemplateSenderForm, SMSTemplateForm, TemplateAndFoldersSelectionForm, TemplateFolderForm\n'), ((14415, 14442), 'app.models.template_list.TemplateLists', 'TemplateLists', (['current_user'], {}), '(current_user)\n', (14428, 14442), False, 'from app.models.template_list import TemplateList, TemplateLists\n'), ((14468, 14486), 'app.main.forms.SearchByNameForm', 'SearchByNameForm', ([], {}), '()\n', (14484, 14486), False, 'from app.main.forms import CreateTemplateForm, EmailTemplateForm, LetterTemplateForm, LetterTemplatePostageForm, SearchByNameForm, SetTemplateSenderForm, SMSTemplateForm, TemplateAndFoldersSelectionForm, TemplateFolderForm\n'), ((15650, 15675), 'flask_babel.lazy_gettext', '_l', (['"""Copy email template"""'], {}), "('Copy email template')\n", (15652, 15675), True, 'from flask_babel import lazy_gettext as _l\n'), ((15721, 15753), 'flask_babel.lazy_gettext', '_l', (['"""Copy text message template"""'], {}), "('Copy text message template')\n", (15723, 15753), True, 'from flask_babel import lazy_gettext as _l\n'), ((19555, 19659), 'flask.url_for', 'url_for', (['""".choose_template"""'], {'service_id': 'service_id', 'template_folder_id': "template_folder['parent_id']"}), "('.choose_template', service_id=service_id, template_folder_id=\n    template_folder['parent_id'])\n", (19562, 19659), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((19797, 19822), 'flask_babel.lazy_gettext', '_l', (['"""Folder is not empty"""'], {}), "('Folder is not empty')\n", (19799, 19822), True, 'from flask_babel import lazy_gettext as _l\n'), ((20392, 20433), 'flask_babel.lazy_gettext', '_l', (['"""Are you sure you want to delete the"""'], {}), "('Are you sure you want to delete the')\n", (20394, 20433), True, 'from flask_babel import lazy_gettext as _l\n'), ((20492, 20505), 'flask_babel.lazy_gettext', '_l', (['"""folder?"""'], {}), "('folder?')\n", (20494, 20505), True, 'from flask_babel import lazy_gettext as _l\n'), ((22413, 22506), 'flask.url_for', 'url_for', (['""".view_template"""'], {'service_id': 'service_id', 'template_id': "new_template['data']['id']"}), "('.view_template', service_id=service_id, template_id=new_template[\n    'data']['id'])\n", (22420, 22506), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((24535, 24574), 'app.utils.get_template', 'get_template', (['template', 'current_service'], {}), '(template, current_service)\n', (24547, 24574), False, 'from app.utils import email_or_sms_not_enabled, get_template, should_skip_template_page, user_has_permissions, user_is_platform_admin\n'), ((24654, 24681), 'flask.request.form.get', 'request.form.get', (['"""confirm"""'], {}), "('confirm')\n", (24670, 24681), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((26137, 26210), 'flask.url_for', 'url_for', (['""".view_template"""'], {'service_id': 'service_id', 'template_id': 'template_id'}), "('.view_template', service_id=service_id, template_id=template_id)\n", (26144, 26210), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((27929, 27946), 'datetime.datetime.utcnow', 'datetime.utcnow', ([], {}), '()\n', (27944, 27946), False, 'from datetime import datetime, timedelta\n'), ((28226, 28263), 'flask_babel.lazy_gettext', '_l', (['"""Are you sure you want to delete"""'], {}), "('Are you sure you want to delete')\n", (28228, 28263), True, 'from flask_babel import lazy_gettext as _l\n'), ((33051, 33079), 'notifications_utils.formatters.nl2br', 'nl2br', (['sender[sender_format]'], {}), '(sender[sender_format])\n', (33056, 33079), False, 'from notifications_utils.formatters import nl2br\n'), ((33669, 33686), 'datetime.timedelta', 'timedelta', ([], {'days': '(1)'}), '(days=1)\n', (33678, 33686), False, 'from datetime import datetime, timedelta\n'), ((20304, 20317), 'flask.abort', 'abort', (['(500)', 'e'], {}), '(500, e)\n', (20309, 20317), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((23262, 23286), 'flask_babel.lazy_gettext', '_l', (['"""New email template"""'], {}), "('New email template')\n", (23264, 23286), True, 'from flask_babel import lazy_gettext as _l\n'), ((23320, 23351), 'flask_babel.lazy_gettext', '_l', (['"""New text message template"""'], {}), "('New text message template')\n", (23322, 23351), True, 'from flask_babel import lazy_gettext as _l\n'), ((26776, 26801), 'flask_babel.lazy_gettext', '_l', (['"""Edit email template"""'], {}), "('Edit email template')\n", (26778, 26801), True, 'from flask_babel import lazy_gettext as _l\n'), ((26847, 26879), 'flask_babel.lazy_gettext', '_l', (['"""Edit text message template"""'], {}), "('Edit text message template')\n", (26849, 26879), True, 'from flask_babel import lazy_gettext as _l\n'), ((27794, 27837), 'dateutil.parser.parse', 'parse', (["last_used_notification['created_at']"], {}), "(last_used_notification['created_at'])\n", (27799, 27837), False, 'from dateutil.parser import parse\n'), ((9464, 9590), 'flask.url_for', 'url_for', (['""".view_template_version_preview"""'], {'service_id': 'service_id', 'template_id': 'template_id', 'version': 'version', 'filetype': '"""png"""'}), "('.view_template_version_preview', service_id=service_id,\n    template_id=template_id, version=version, filetype='png')\n", (9471, 9590), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((19903, 19959), 'flask_babel._', '_', (['"""You must empty this folder before you can delete it"""'], {}), "('You must empty this folder before you can delete it')\n", (19904, 19959), False, 'from flask_babel import _\n'), ((20022, 20136), 'flask.url_for', 'url_for', (['""".choose_template"""'], {'service_id': 'service_id', 'template_type': '"""all"""', 'template_folder_id': 'template_folder_id'}), "('.choose_template', service_id=service_id, template_type='all',\n    template_folder_id=template_folder_id)\n", (20029, 20136), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((22297, 22321), 'flask_babel._', '_', (['"""\'{}\' template saved"""'], {}), '("\'{}\' template saved")\n', (22298, 22321), False, 'from flask_babel import _\n'), ((25172, 25206), 'app.main.views.send.get_example_csv_rows', 'get_example_csv_rows', (['new_template'], {}), '(new_template)\n', (25192, 25206), False, 'from app.main.views.send import get_example_csv_rows, get_sender_details\n'), ((25228, 25262), 'app.main.views.send.get_example_csv_rows', 'get_example_csv_rows', (['new_template'], {}), '(new_template)\n', (25248, 25262), False, 'from app.main.views.send import get_example_csv_rows, get_sender_details\n'), ((26038, 26062), 'flask_babel._', '_', (['"""\'{}\' template saved"""'], {}), '("\'{}\' template saved")\n', (26039, 26062), False, 'from flask_babel import _\n'), ((29979, 30117), 'flask.url_for', 'url_for', (['""".view_template_version_preview"""'], {'service_id': 'service_id', 'template_id': 'template_id', 'version': "template['version']", 'filetype': '"""png"""'}), "('.view_template_version_preview', service_id=service_id,\n    template_id=template_id, version=template['version'], filetype='png')\n", (29986, 30117), False, 'from flask import abort, flash, jsonify, redirect, render_template, request, url_for\n'), ((30276, 30349), 'app.service_api_client.get_service_template_versions', 'service_api_client.get_service_template_versions', (['service_id', 'template_id'], {}), '(service_id, template_id)\n', (30324, 30349), False, 'from app import current_service, service_api_client, template_api_prefill_client, template_folder_api_client, template_statistics_client\n')]