Example #1
0
def edit_service_template(service_id, template_id):
    template = service_api_client.get_service_template(service_id, template_id)['data']
    template['template_content'] = template['content']
    form = form_objects[template['template_type']](**template)

    if form.validate_on_submit():
        subject = form.subject.data if hasattr(form, 'subject') else None
        new_template = Template({
            'name': form.name.data,
            'content': form.template_content.data,
            'subject': subject,
            'template_type': template['template_type'],
            'id': template['id']
        })
        template_change = Template(template).compare_to(new_template)
        if template_change.has_different_placeholders and not request.form.get('confirm'):
            return render_template(
                'views/templates/breaking-change.html',
                template_change=template_change,
                new_template=new_template,
                column_headings=list(ascii_uppercase[:len(new_template.placeholders) + 1]),
                example_rows=[
                    [first_column_heading[new_template.template_type]] + list(new_template.placeholders),
                    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
            )
        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:
            return redirect(url_for(
                '.view_template',
                service_id=service_id,
                template_id=template_id
            ))
    return render_template(
        'views/edit-{}-template.html'.format(template['template_type']),
        form=form,
        template_id=template_id,
        template_type=template['template_type'],
        heading_action='Edit'
    )
def edit_service_template(service_id, template_id):
    template = service_api_client.get_service_template(service_id,
                                                       template_id)['data']
    template['template_content'] = template['content']
    form = form_objects[template['template_type']](**template)

    if form.validate_on_submit():
        subject = form.subject.data if hasattr(form, 'subject') else None
        new_template = Template({
            'name': form.name.data,
            'content': form.template_content.data,
            'subject': subject,
            'template_type': template['template_type'],
            'id': template['id']
        })
        template_change = Template(template).compare_to(new_template)
        if template_change.has_different_placeholders and not request.form.get(
                'confirm'):
            return render_template(
                'views/templates/breaking-change.html',
                template_change=template_change,
                new_template=new_template,
                column_headings=list(
                    ascii_uppercase[:len(new_template.placeholders) + 1]),
                example_rows=[
                    [first_column_heading[new_template.template_type]] +
                    list(new_template.placeholders),
                    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)
        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:
            return redirect(
                url_for('.view_template',
                        service_id=service_id,
                        template_id=template_id))
    return render_template('views/edit-{}-template.html'.format(
        template['template_type']),
                           form=form,
                           template_id=template_id,
                           template_type=template['template_type'],
                           heading_action='Edit')
Example #3
0
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')
                and current_service.api_keys):
            return render_template(
                'views/templates/breaking-change.html',
                template_change=template_change,
                new_template=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:
            return redirect(
                url_for('.view_template',
                        service_id=service_id,
                        template_id=template_id))

    if template[
            'template_type'] not in current_service.available_template_types:
        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(
            'views/edit-{}-template.html'.format(template['template_type']),
            form=form,
            template=template,
            heading_action='Edit',
        )
Example #4
0
def edit_service_template(service_id, template_id):
    template = service_api_client.get_service_template(service_id,
                                                       template_id)['data']
    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 = get_template(
            {
                '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']
            }, 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:
            return redirect(
                url_for('.view_template',
                        service_id=service_id,
                        template_id=template_id))

    db_template = service_api_client.get_service_template(
        service_id, template_id)['data']

    if email_or_sms_not_enabled(db_template['template_type'],
                                current_service['permissions']):
        return redirect(
            url_for('.action_blocked',
                    service_id=service_id,
                    notification_type=db_template['template_type'],
                    return_to='view_template',
                    template_id=template_id))
    else:
        return render_template('views/edit-{}-template.html'.format(
            template['template_type']),
                               form=form,
                               template_id=template_id,
                               template_type=template['template_type'],
                               heading_action='Edit')
Example #5
0
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"),
        )