Merge remote-tracking branch 'origin/master' into implement_send_sms_and_email

This commit is contained in:
Nicholas Staples
2016-01-20 09:48:25 +00:00
15 changed files with 308 additions and 93 deletions

View File

@@ -1,9 +1,5 @@
from flask import url_for
from datetime import datetime
from client.errors import HTTPError, InvalidResponse
from sqlalchemy.orm import load_only
from flask.ext.login import current_user
from app import (db, notifications_api_client)
from app import notifications_api_client
from app.main.utils import BrowsableItem

View File

@@ -0,0 +1,49 @@
from flask import url_for
from app import notifications_api_client
from app.main.utils import BrowsableItem
def insert_service_template(name, type_, content, service_id):
return notifications_api_client.create_service_template(
name, type_, content, service_id)
def update_service_template(id_, name, type_, content, service_id):
return notifications_api_client.update_service_template(
id_, name, type_, content, service_id)
def get_service_templates(service_id):
return notifications_api_client.get_service_templates(service_id)
def get_service_template(service_id, template_id):
return notifications_api_client.get_service_template(
service_id, template_id)
def delete_service_template(service_id, template_id):
return notifications_api_client.delete_service_template(
service_id, template_id)
class TemplatesBrowsableItem(BrowsableItem):
@property
def title(self):
return self._item['name']
@property
def link(self):
return url_for(
'main.edit_service_template',
service_id=self._item['service'],
template_id=self._item['id'])
@property
def destructive(self):
return False
@property
def hint(self):
return "Some service template hint here"

View File

@@ -6,7 +6,8 @@ from wtforms import (
PasswordField,
ValidationError,
TextAreaField,
FileField
FileField,
SelectField
)
from wtforms.validators import DataRequired, Email, Length, Regexp
@@ -187,8 +188,13 @@ class ConfirmPasswordForm(Form):
class TemplateForm(Form):
template_name = StringField(u'Template name')
template_body = TextAreaField(u'Message')
name = StringField(
u'Template name',
validators=[DataRequired(message="Template name cannot be empty")])
template_type = SelectField(u'Template type', choices=[('sms', 'SMS')])
content = TextAreaField(
u'Message',
validators=[DataRequired(message="Template content cannot be empty")])
class ForgotPasswordForm(Form):

View File

@@ -37,7 +37,7 @@ def register():
role_id=1)
users_dao.insert_user(user)
user_api_client = UserApiClient(current_app.config['NOTIFY_API_URL'],
user_api_client = UserApiClient(current_app.config['API_HOST_NAME'],
current_app.config['ADMIN_CLIENT_USER_NAME'],
current_app.config['ADMIN_CLIENT_SECRET'])
try:

View File

@@ -3,80 +3,99 @@ from flask_login import login_required
from app.main import main
from app.main.forms import TemplateForm
from ._templates import templates
from app.main.dao.services_dao import get_service_by_id
from app.main.dao import templates_dao as dao
from client.errors import HTTPError
@main.route("/services/<int:service_id>/templates")
@login_required
def manage_templates(service_id):
def manage_service_templates(service_id):
try:
templates = dao.get_service_templates(service_id)['data']
except HTTPError as e:
if e.status_code == 404:
abort(404)
else:
raise e
return render_template(
'views/manage-templates.html',
service_id=service_id,
templates=templates,
)
templates=templates)
@main.route("/services/<int:service_id>/templates/add", methods=['GET', 'POST'])
@login_required
def add_template(service_id):
def add_service_template(service_id):
try:
service = dao.get_service_by_id(service_id)['data']
except HTTPError as e:
if e.status_code == 404:
abort(404)
else:
raise e
form = TemplateForm()
if request.method == 'GET':
return render_template(
'views/edit-template.html',
h1='Add template',
form=form,
service_id=service_id
)
elif request.method == 'POST':
return redirect(url_for('.manage_templates', service_id=service_id))
if form.validate_on_submit():
dao.insert_service_template(
form.name.data, form.template_type.data, form.content.data, service_id)
return redirect(url_for(
'.manage_service_templates', service_id=service_id))
return render_template(
'views/edit-template.html',
h1='Add template',
form=form,
service_id=service_id)
@main.route("/services/<int:service_id>/templates/<int:template_id>", methods=['GET', 'POST'])
@login_required
def edit_template(service_id, template_id):
def edit_service_template(service_id, template_id):
try:
template = dao.get_service_template(service_id, template_id)['data']
except HTTPError as e:
if e.status_code == 404:
abort(404)
else:
raise e
form = TemplateForm(form_data=template)
form = TemplateForm()
if form.validate_on_submit():
dao.update_service_template(
template_id, form.name.data, form.template_type.data,
form.content.data, service_id)
return redirect(url_for('.manage_service_templates', service_id=service_id))
form.template_name.data = templates[template_id - 1]['name']
form.template_body.data = templates[template_id - 1]['body']
if request.method == 'GET':
return render_template(
'views/edit-template.html',
h1='Edit template',
form=form,
service_id=service_id,
template_id=template_id
)
elif request.method == 'POST':
return redirect(url_for('.manage_templates', service_id=service_id))
return render_template(
'views/edit-template.html',
h1='Edit template',
form=form,
service_id=service_id,
template_id=template_id)
@main.route("/services/<int:service_id>/templates/<int:template_id>/delete", methods=['GET', 'POST'])
@login_required
def delete_template(service_id, template_id):
form = TemplateForm()
form.template_name.data = templates[template_id - 1]['name']
form.template_body.data = templates[template_id - 1]['body']
if request.method == 'GET':
flash('Are you sure you want to delete {}?'.format(form.template_name.data), 'delete')
return render_template(
'views/edit-template.html',
h1='Edit template',
form=form,
service_id=service_id,
template_id=template_id
)
elif request.method == 'POST':
if request.form.get('delete'):
return redirect(url_for('.manage_templates', service_id=service_id))
def delete_service_template(service_id, template_id):
try:
template = dao.get_service_template(service_id, template_id)['data']
except HTTPError as e:
if e.status_code == 404:
abort(404)
else:
return redirect(url_for('.manage_templates', service_id=service_id))
raise e
form = TemplateForm(form_data=template)
if form.validate_on_submit():
dao.delete_service_template(service_id, template_id)
return redirect(url_for('.manage_service_templates', service_id=service_id))
flash('Are you sure you want to delete {}?'.format(form.name.data), 'delete')
return render_template(
'views/edit-template.html',
h1='Edit template',
form=form,
service_id=service_id,
template_id=template_id)

View File

@@ -6,7 +6,7 @@
<li><a href="{{ url_for('.send_sms', 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('.view_jobs', service_id=service_id) }}">Activity</a></li>
<li><a href="{{ url_for('.manage_templates', service_id=service_id) }}">Templates</a></li>
<li><a href="{{ url_for('.manage_service_templates', service_id=service_id) }}">Templates</a></li>
</ul>
<ul>
<li><a href="{{ url_for('.api_keys', service_id=service_id) }}">API keys and documentation</a></li>

View File

@@ -11,12 +11,12 @@ GOV.UK Notify | Edit template
<h1 class="heading-xlarge">{{ h1 }}</h1>
<form method="post">
{{ textbox(form.template_name) }}
{{ textbox(form.template_body, highlight_tags=True) }}
{{ textbox(form.name) }}
{{ textbox(form.content, highlight_tags=True) }}
{{ page_footer(
'Save',
delete_link=url_for('.delete_template', service_id=service_id, template_id=template_id) if template_id or None,
back_link=url_for('.manage_templates', service_id=service_id),
delete_link=url_for('.delete_service_template', service_id=service_id, template_id=template_id) if template_id or None,
back_link=url_for('.manage_service_templates', service_id=service_id),
back_link_text='Back to templates'
) }}
</form>

View File

@@ -38,7 +38,7 @@ GOV.UK Notify | Notifications activity
</ul>
<p>
Sent with template <a href="{{ url_for('.edit_template', service_id=service_id, template_id=1) }}">{{ template_used }}</a> at {{ uploaded_file_time }}
Sent with template <a href="{{ url_for('.edit_service_template', service_id=service_id, template_id=1) }}">{{ template_used }}</a> at {{ uploaded_file_time }}
</p>
{% call(item) list_table(

View File

@@ -15,21 +15,21 @@ GOV.UK Notify | Manage templates
<h1 class="heading-xlarge">Templates</h1>
<p>
<a href="{{ url_for('.add_template', service_id=service_id) }}">Create new template</a>
<a href="{{ url_for('.add_service_template', service_id=service_id) }}">Create new template</a>
{% for template in templates %}
{% if template.type == 'sms' %}
{{ sms_message(
template.body,
template.content,
name=template.name,
edit_link=url_for('.edit_template', service_id=service_id, template_id=loop.index)
edit_link=url_for('.edit_service_template', service_id=template.service, template_id=template.id)
) }}
{% elif template.type == 'email' %}
{{ email_message(
template.subject,
template.body,
"template subject",
template.content,
name=template.name,
edit_link=url_for('.edit_template', service_id=service_id, template_id=loop.index)
edit_link=url_for('.edit_service_template', service_id=template.service, template_id=template.id)
) }}
{% endif %}
{% endfor %}

View File

@@ -23,7 +23,7 @@
</fieldset>
<p>
or <a href="{{ url_for(".add_template", service_id=service_id) }}">create a new template</a>
or <a href="{{ url_for('.add_service_template', service_id=service_id) }}">create a new template</a>
</p>
<h2 class="heading-medium">2. Add recipients</h2>