mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-05 10:53:28 -05:00
Add basic flow for adding email _or_ sms templates
Templates now have: - a type (email or sms) - a subject (if they are email templates) We don’t want two completely separate view files for email and SMS, because they would have an enormous amount of repetition. So this commit adds - different templates for SMS and email templates - different form objects for SMS and email templates …and wires them up.
This commit is contained in:
@@ -4,12 +4,12 @@ from app.utils import BrowsableItem
|
||||
from notifications_python_client.errors import HTTPError
|
||||
|
||||
|
||||
def insert_service_template(name, content, service_id):
|
||||
def insert_service_template(name, content, service_id, subject=None):
|
||||
return notifications_api_client.create_service_template(
|
||||
name, 'sms', content, service_id)
|
||||
name, 'sms' if subject is None else 'email', content, service_id, subject)
|
||||
|
||||
|
||||
def update_service_template(id_, name, content, service_id):
|
||||
def update_service_template(id_, name, content, service_id, subject=None):
|
||||
return notifications_api_client.update_service_template(
|
||||
id_, name, 'sms', content, service_id)
|
||||
|
||||
|
||||
@@ -188,7 +188,7 @@ class ConfirmPasswordForm(Form):
|
||||
raise ValidationError('Invalid password')
|
||||
|
||||
|
||||
class TemplateForm(Form):
|
||||
class SMSTemplateForm(Form):
|
||||
name = StringField(
|
||||
u'Template name',
|
||||
validators=[DataRequired(message="Template name cannot be empty")])
|
||||
@@ -198,6 +198,13 @@ class TemplateForm(Form):
|
||||
validators=[DataRequired(message="Template content cannot be empty")])
|
||||
|
||||
|
||||
class EmailTemplateForm(SMSTemplateForm):
|
||||
|
||||
subject = StringField(
|
||||
u'Subject',
|
||||
validators=[DataRequired(message="Subject cannot be empty")])
|
||||
|
||||
|
||||
class ForgotPasswordForm(Form):
|
||||
email_address = email_address()
|
||||
|
||||
|
||||
0
app/main/views/email.py
Normal file
0
app/main/views/email.py
Normal file
@@ -22,15 +22,3 @@ def register_from_invite():
|
||||
@login_required
|
||||
def verify_mobile():
|
||||
return render_template('views/verify-mobile.html')
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/send-email")
|
||||
@login_required
|
||||
def send_email(service_id):
|
||||
return render_template('views/send-email.html', service_id=service_id)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/check-email")
|
||||
@login_required
|
||||
def check_email(service_id):
|
||||
return render_template('views/check-email.html')
|
||||
|
||||
@@ -35,8 +35,10 @@ from app.utils import (
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/sms/send", methods=['GET'])
|
||||
def choose_sms_template(service_id):
|
||||
@main.route("/services/<service_id>/send/<template_type>", methods=['GET'])
|
||||
def choose_template(service_id, template_type):
|
||||
if template_type not in ['email', 'sms']:
|
||||
abort(404)
|
||||
try:
|
||||
jobs = job_api_client.get_job(service_id)['data']
|
||||
except HTTPError as e:
|
||||
@@ -44,21 +46,18 @@ def choose_sms_template(service_id):
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
print("="*80)
|
||||
print(jobs)
|
||||
print(len(jobs))
|
||||
print(bool(len(jobs)))
|
||||
return render_template(
|
||||
'views/choose-sms-template.html',
|
||||
'views/choose-{}-template.html'.format(template_type),
|
||||
templates=[
|
||||
Template(template) for template in templates_dao.get_service_templates(service_id)['data']
|
||||
if template['template_type'] == template_type
|
||||
],
|
||||
has_jobs=len(jobs),
|
||||
service_id=service_id
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/sms/send/<template_id>", methods=['GET', 'POST'])
|
||||
@main.route("/services/<service_id>/send/<int:template_id>", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def send_sms(service_id, template_id):
|
||||
|
||||
@@ -91,7 +90,7 @@ def send_sms(service_id, template_id):
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/sms/send/<template_id>.csv", methods=['GET'])
|
||||
@main.route("/services/<service_id>/send/<template_id>.csv", methods=['GET'])
|
||||
@login_required
|
||||
def get_example_csv(service_id, template_id):
|
||||
template = templates_dao.get_service_template_or_404(service_id, template_id)['data']
|
||||
@@ -104,7 +103,7 @@ def get_example_csv(service_id, template_id):
|
||||
return(output.getvalue(), 200, {'Content-Type': 'text/csv; charset=utf-8'})
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/sms/send/<template_id>/to-self", methods=['GET'])
|
||||
@main.route("/services/<service_id>/send/<template_id>/to-self", methods=['GET'])
|
||||
@login_required
|
||||
def send_sms_to_self(service_id, template_id):
|
||||
template = templates_dao.get_service_template_or_404(service_id, template_id)['data']
|
||||
@@ -126,7 +125,7 @@ def send_sms_to_self(service_id, template_id):
|
||||
upload_id=upload_id))
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/sms/check/<upload_id>",
|
||||
@main.route("/services/<service_id>/check/<upload_id>",
|
||||
methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def check_sms(service_id, upload_id):
|
||||
|
||||
@@ -5,45 +5,44 @@ from notifications_python_client.errors import HTTPError
|
||||
from utils.template import Template
|
||||
|
||||
from app.main import main
|
||||
from app.main.forms import TemplateForm
|
||||
from app.main.forms import SMSTemplateForm, EmailTemplateForm
|
||||
from app import job_api_client
|
||||
from app.main.dao.services_dao import get_service_by_id
|
||||
from app.main.dao.services_dao import get_service_by_id_or_404
|
||||
from app.main.dao import templates_dao as tdao
|
||||
from app.main.dao import services_dao as sdao
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/templates")
|
||||
form_objects = {
|
||||
'email': EmailTemplateForm,
|
||||
'sms': SMSTemplateForm
|
||||
}
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/templates/add-<template_type>", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def manage_service_templates(service_id):
|
||||
return redirect(url_for(
|
||||
'.choose_sms_template',
|
||||
service_id=service_id
|
||||
))
|
||||
def add_service_template(service_id, template_type):
|
||||
|
||||
service = sdao.get_service_by_id_or_404(service_id)
|
||||
|
||||
@main.route("/services/<service_id>/templates/add", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def add_service_template(service_id):
|
||||
try:
|
||||
service = sdao.get_service_by_id(service_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
if template_type not in ['sms', 'email']:
|
||||
abort(404)
|
||||
|
||||
form = TemplateForm()
|
||||
form = form_objects[template_type]()
|
||||
|
||||
if form.validate_on_submit():
|
||||
tdao.insert_service_template(
|
||||
form.name.data, form.template_content.data, service_id)
|
||||
return redirect(url_for(
|
||||
'.choose_sms_template', service_id=service_id))
|
||||
form.name.data, form.template_content.data, service_id, form.subject.data or None
|
||||
)
|
||||
return redirect(
|
||||
url_for('.choose_template', service_id=service_id, template_type=template_type)
|
||||
)
|
||||
|
||||
return render_template(
|
||||
'views/edit-template.html',
|
||||
h1='Add a text message template',
|
||||
'views/edit-{}-template.html'.format(template_type),
|
||||
form=form,
|
||||
service_id=service_id)
|
||||
template_type=template_type,
|
||||
service_id=service_id
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/templates/<int:template_id>", methods=['GET', 'POST'])
|
||||
@@ -51,20 +50,25 @@ def add_service_template(service_id):
|
||||
def edit_service_template(service_id, template_id):
|
||||
template = tdao.get_service_template_or_404(service_id, template_id)['data']
|
||||
template['template_content'] = template['content']
|
||||
form = TemplateForm(**template)
|
||||
form = form_objects[template['template_type']](**template)
|
||||
|
||||
if form.validate_on_submit():
|
||||
tdao.update_service_template(
|
||||
template_id, form.name.data,
|
||||
form.template_content.data, service_id)
|
||||
return redirect(url_for('.choose_sms_template', service_id=service_id))
|
||||
return redirect(url_for(
|
||||
'.choose_template',
|
||||
service_id=service_id,
|
||||
template_type=template['template_type']
|
||||
))
|
||||
|
||||
return render_template(
|
||||
'views/edit-template.html',
|
||||
h1='Edit template',
|
||||
'views/edit-{}-template.html'.format(template['template_type']),
|
||||
form=form,
|
||||
service_id=service_id,
|
||||
template_id=template_id)
|
||||
template_id=template_id,
|
||||
template_type=template['template_type']
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/templates/<int:template_id>/delete", methods=['GET', 'POST'])
|
||||
@@ -74,13 +78,17 @@ def delete_service_template(service_id, template_id):
|
||||
|
||||
if request.method == 'POST':
|
||||
tdao.delete_service_template(service_id, template_id)
|
||||
return redirect(url_for('.manage_service_templates', service_id=service_id))
|
||||
return redirect(url_for(
|
||||
'.choose_template',
|
||||
service_id=service_id,
|
||||
template_type=template['template_type']
|
||||
))
|
||||
|
||||
template['template_content'] = template['content']
|
||||
form = TemplateForm(**template)
|
||||
form = form_objects[template['template_type']](**template)
|
||||
flash('Are you sure you want to delete ‘{}’?'.format(form.name.data), 'delete')
|
||||
return render_template(
|
||||
'views/edit-template.html',
|
||||
'views/edit-{}-template.html'.format(template['template_type']),
|
||||
h1='Edit template',
|
||||
form=form,
|
||||
service_id=service_id,
|
||||
|
||||
@@ -3,8 +3,8 @@
|
||||
<a href="{{ url_for('.service_dashboard', service_id=service_id) }}">{{ session.get('service_name', 'Service') }}</a>
|
||||
</h2>
|
||||
<ul>
|
||||
<li><a href="{{ url_for('.choose_sms_template', service_id=service_id) }}">Send text messages</a></li>
|
||||
<li><a href="{{ url_for('.send_email', service_id=service_id) }}">Send emails</a></li>
|
||||
<li><a href="{{ url_for('.choose_template', service_id=service_id, template_type='sms') }}">Send text messages</a></li>
|
||||
<li><a href="{{ url_for('.choose_template', service_id=service_id, template_type='email') }}">Send emails</a></li>
|
||||
</ul>
|
||||
<ul>
|
||||
<li><a href="{{ url_for('.manage_users', service_id=service_id) }}">Manage team</a></li>
|
||||
|
||||
38
app/templates/views/choose-email-template.html
Normal file
38
app/templates/views/choose-email-template.html
Normal file
@@ -0,0 +1,38 @@
|
||||
{% extends "withnav_template.html" %}
|
||||
{% from "components/email-message.html" import email_message %}
|
||||
{% from "components/page-footer.html" import page_footer %}
|
||||
{% from "components/textbox.html" import textbox %}
|
||||
|
||||
{% block page_title %}
|
||||
Send emails – GOV.UK Notify
|
||||
{% endblock %}
|
||||
|
||||
{% block maincolumn_content %}
|
||||
|
||||
<h1 class="heading-large">Send emails</h1>
|
||||
|
||||
<form method="POST" enctype="multipart/form-data">
|
||||
|
||||
{% if templates %}
|
||||
<div class="grid-row">
|
||||
{% for template in templates %}
|
||||
<div class="column-two-thirds">
|
||||
{{ email_message(template.subject, template.formatted_as_markup, name=template.name) }}
|
||||
</div>
|
||||
<div class="column-one-third">
|
||||
<div class="sms-message-use-links">
|
||||
<a href="{{ url_for(".send_sms", service_id=service_id, template_id=template.id) }}">Add recipients</a>
|
||||
<a href="{{ url_for(".send_sms_to_self", service_id=service_id, template_id=template.id) }}">Send yourself a test</a>
|
||||
<a href="{{ url_for(".edit_service_template", service_id=service_id, template_id=template.id) }}">Edit template</a>
|
||||
</div>
|
||||
</div>
|
||||
{% endfor %}
|
||||
</div>
|
||||
{% endif %}
|
||||
|
||||
<p>
|
||||
<a href="{{ url_for('.add_service_template', service_id=service_id, template_type='sms') }}" class="button">Add a new template</a>
|
||||
</p>
|
||||
|
||||
</form>
|
||||
{% endblock %}
|
||||
@@ -40,7 +40,7 @@
|
||||
{% endif %}
|
||||
|
||||
<p>
|
||||
<a href="{{ url_for('.add_service_template', service_id=service_id) }}" class="button">Add a new template</a>
|
||||
<a href="{{ url_for('.add_service_template', service_id=service_id, template_type='sms') }}" class="button">Add a new template</a>
|
||||
</p>
|
||||
|
||||
</form>
|
||||
|
||||
@@ -8,12 +8,15 @@
|
||||
|
||||
{% block maincolumn_content %}
|
||||
|
||||
<h1 class="heading-large">{{ h1 }}</h1>
|
||||
<h1 class="heading-large">Edit email template</h1>
|
||||
|
||||
<form method="post">
|
||||
<div class="grid-row">
|
||||
<div class="column-two-thirds">
|
||||
{{ textbox(form.name, width='1-1') }}
|
||||
{% if 'email' == template_type %}
|
||||
{{ textbox(form.subject, width='1-1') }}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="grid-row">
|
||||
@@ -31,7 +34,7 @@
|
||||
'Save',
|
||||
delete_link=url_for('.delete_service_template', service_id=service_id, template_id=template_id) if template_id or None,
|
||||
delete_link_text='Delete this template',
|
||||
back_link=url_for('.choose_sms_template', service_id=service_id),
|
||||
back_link=url_for('.choose_template', template_type=template_type, service_id=service_id),
|
||||
back_link_text='Cancel'
|
||||
) }}
|
||||
</form>
|
||||
43
app/templates/views/edit-sms-template.html
Normal file
43
app/templates/views/edit-sms-template.html
Normal file
@@ -0,0 +1,43 @@
|
||||
{% extends "withnav_template.html" %}
|
||||
{% from "components/textbox.html" import textbox %}
|
||||
{% from "components/page-footer.html" import page_footer %}
|
||||
|
||||
{% block page_title %}
|
||||
{{ h1 }} – GOV.UK Notify
|
||||
{% endblock %}
|
||||
|
||||
{% block maincolumn_content %}
|
||||
|
||||
<h1 class="heading-large">Edit text message template</h1>
|
||||
|
||||
<form method="post">
|
||||
<div class="grid-row">
|
||||
<div class="column-two-thirds">
|
||||
{{ textbox(form.name, width='1-1') }}
|
||||
{% if 'email' == template_type %}
|
||||
{{ textbox(form.subject, width='1-1') }}
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="grid-row">
|
||||
<div class="column-two-thirds">
|
||||
{{ textbox(form.template_content, highlight_tags=True, width='1-1') }}
|
||||
</div>
|
||||
<div class="column-one-third">
|
||||
<label for='template_content' class='edit-template-placeholder-hint'>
|
||||
Add placeholders using double brackets, eg Your thing
|
||||
is due on ((date))
|
||||
</label>
|
||||
</div>
|
||||
</div>
|
||||
{{ page_footer(
|
||||
'Save',
|
||||
delete_link=url_for('.delete_service_template', service_id=service_id, template_id=template_id) if template_id or None,
|
||||
delete_link_text='Delete this template',
|
||||
back_link=url_for('.choose_template', service_id=service_id, template_type=template_type),
|
||||
back_link_text='Cancel'
|
||||
) }}
|
||||
</form>
|
||||
|
||||
|
||||
{% endblock %}
|
||||
@@ -1,17 +0,0 @@
|
||||
{% extends "withnav_template.html" %}
|
||||
|
||||
{% block page_title %}
|
||||
Send email – GOV.UK Notify
|
||||
{% endblock %}
|
||||
|
||||
{% block maincolumn_content %}
|
||||
|
||||
<h1 class="heading-large">Send email</h1>
|
||||
|
||||
<p>This page will be where we construct email messages</p>
|
||||
|
||||
<p>
|
||||
<a class="button" href="check-email" role="button">Continue</a>
|
||||
</p>
|
||||
|
||||
{% endblock %}
|
||||
Reference in New Issue
Block a user