mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-08-18 05:29:38 -04:00
Use Template to replace/highlight placeholders
This commit brings in the `Template` util, added here: https://github.com/alphagov/notifications-utils/pull/1 It also does a fair bit of tidying up, which I’ve unfortunately squashed into this one massive commit. The main change is moving 404 handling into the templates dao, so that every view isn’t littered with `try: … except(HTTPError)`. It also adds new features, in a prototypy sort of way, which are: - download a prefilled example CSV - show all the columns for your template on the 'check' page
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
from flask import url_for
|
||||
from flask import url_for, abort
|
||||
from app import notifications_api_client
|
||||
from app.main.utils import BrowsableItem
|
||||
|
||||
@@ -17,9 +17,24 @@ 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 get_service_templates_or_404(service_id):
|
||||
try:
|
||||
get_service_templates(service_id)
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
|
||||
|
||||
def get_service_template_or_404(service_id, template_id):
|
||||
try:
|
||||
return notifications_api_client.get_service_template(service_id, template_id)
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
|
||||
|
||||
def delete_service_template(service_id, template_id):
|
||||
|
||||
@@ -8,6 +8,7 @@ from flask import (
|
||||
)
|
||||
from flask_login import login_required
|
||||
from notifications_python_client.errors import HTTPError
|
||||
from utils.template import Template
|
||||
|
||||
from app import job_api_client
|
||||
from app.main import main
|
||||
@@ -38,7 +39,6 @@ def view_jobs(service_id):
|
||||
def view_job(service_id, job_id):
|
||||
try:
|
||||
job = job_api_client.get_job(service_id, job_id)['data']
|
||||
template = templates_dao.get_service_template(service_id, job['template'])['data']
|
||||
messages = []
|
||||
return render_template(
|
||||
'views/job.html',
|
||||
@@ -55,7 +55,9 @@ def view_job(service_id, job_id):
|
||||
cost=u'£0.00',
|
||||
uploaded_file_name=job['original_file_name'],
|
||||
uploaded_file_time=job['created_at'],
|
||||
template=template,
|
||||
template=Template(
|
||||
templates_dao.get_service_template_or_404(service_id, job['template'])['data']
|
||||
),
|
||||
service_id=service_id
|
||||
)
|
||||
except HTTPError as e:
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import csv
|
||||
import uuid
|
||||
import botocore
|
||||
import re
|
||||
import io
|
||||
|
||||
from datetime import date
|
||||
|
||||
@@ -12,12 +14,14 @@ from flask import (
|
||||
flash,
|
||||
abort,
|
||||
session,
|
||||
current_app
|
||||
current_app,
|
||||
Markup
|
||||
)
|
||||
|
||||
from flask_login import login_required
|
||||
from werkzeug import secure_filename
|
||||
from notifications_python_client.errors import HTTPError
|
||||
from utils.template import Template
|
||||
|
||||
from app.main import main
|
||||
from app.main.forms import CsvUploadForm
|
||||
@@ -35,22 +39,19 @@ from app.main.utils import (
|
||||
|
||||
@main.route("/services/<service_id>/sms/send", methods=['GET'])
|
||||
def choose_sms_template(service_id):
|
||||
try:
|
||||
templates = 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/choose-sms-template.html',
|
||||
templates=templates,
|
||||
service_id=service_id)
|
||||
return render_template(
|
||||
'views/choose-sms-template.html',
|
||||
templates=[
|
||||
Template(template) for template in templates_dao.get_service_templates(service_id)['data']
|
||||
],
|
||||
service_id=service_id
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/sms/send/<template_id>", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def send_sms(service_id, template_id):
|
||||
|
||||
form = CsvUploadForm()
|
||||
if form.validate_on_submit():
|
||||
try:
|
||||
@@ -63,24 +64,33 @@ def send_sms(service_id, template_id):
|
||||
service_id=service_id,
|
||||
upload_id=upload_id))
|
||||
except ValueError as e:
|
||||
message = 'There was a problem uploading: {}'.format(
|
||||
csv_file.filename)
|
||||
flash(message)
|
||||
flash('There was a problem uploading: {}'.format(csv_file.filename))
|
||||
flash(str(e))
|
||||
return redirect(url_for('.send_sms', service_id=service_id, template_id=template_id))
|
||||
|
||||
try:
|
||||
template = templates_dao.get_service_template(service_id, template_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
template = Template(
|
||||
templates_dao.get_service_template_or_404(service_id, template_id)['data']
|
||||
)
|
||||
|
||||
return render_template('views/send-sms.html',
|
||||
template=template,
|
||||
form=form,
|
||||
service_id=service_id)
|
||||
return render_template(
|
||||
'views/send-sms.html',
|
||||
template=template,
|
||||
column_headers=['phone number'] + template.placeholders_as_markup,
|
||||
form=form,
|
||||
service_id=service_id
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/sms/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']
|
||||
output = io.StringIO()
|
||||
csv.writer(output).writerow(
|
||||
['phone number'] + Template(template).list_placeholders
|
||||
)
|
||||
|
||||
return(output.getvalue(), 200, {'Content-Type': 'text/csv; charset=utf-8'})
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/sms/check/<upload_id>",
|
||||
@@ -89,21 +99,23 @@ def send_sms(service_id, template_id):
|
||||
def check_sms(service_id, upload_id):
|
||||
|
||||
if request.method == 'GET':
|
||||
|
||||
contents = s3download(service_id, upload_id)
|
||||
if not contents:
|
||||
flash('There was a problem reading your upload file')
|
||||
upload_result = _get_numbers(contents)
|
||||
upload_data = session['upload_data']
|
||||
original_file_name = upload_data.get('original_file_name')
|
||||
template_id = upload_data.get('template_id')
|
||||
template = templates_dao.get_service_template(service_id, template_id)['data']
|
||||
template = Template(
|
||||
templates_dao.get_service_template_or_404(service_id, template_id)['data'],
|
||||
values=upload_result['valid'][0] if upload_result['valid'] else {},
|
||||
drop_values={'phone'}
|
||||
)
|
||||
return render_template(
|
||||
'views/check-sms.html',
|
||||
upload_result=upload_result,
|
||||
message_template=template['content'],
|
||||
original_file_name=original_file_name,
|
||||
template_id=template_id,
|
||||
template=template,
|
||||
column_headers=['phone number'] + template.placeholders_as_markup,
|
||||
original_file_name=upload_data.get('original_file_name'),
|
||||
service_id=service_id
|
||||
)
|
||||
elif request.method == 'POST':
|
||||
@@ -153,3 +165,16 @@ def _get_numbers(contents):
|
||||
except InvalidPhoneError:
|
||||
rejects.append({"line_number": i+2, "phone": row['phone']})
|
||||
return {"valid": valid, "rejects": rejects}
|
||||
|
||||
|
||||
def _get_column_headers(template_content, marked_up=False):
|
||||
headers = re.findall(
|
||||
r"\(\(([^\)]+)\)\)", # anything that looks like ((registration number))
|
||||
template_content
|
||||
)
|
||||
if marked_up:
|
||||
return [
|
||||
Markup("<span class='placeholder'>{}</span>".format(header))
|
||||
for header in headers
|
||||
]
|
||||
return headers
|
||||
|
||||
@@ -1,13 +1,15 @@
|
||||
from flask import request, render_template, redirect, url_for, flash, abort
|
||||
from flask_login import login_required
|
||||
|
||||
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 import job_api_client
|
||||
from app.main.dao.services_dao import get_service_by_id
|
||||
from app.main.dao import templates_dao as tdao
|
||||
from app.main.dao import services_dao as sdao
|
||||
from notifications_python_client.errors import HTTPError
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/templates")
|
||||
@@ -15,7 +17,6 @@ from notifications_python_client.errors import HTTPError
|
||||
def manage_service_templates(service_id):
|
||||
try:
|
||||
jobs = job_api_client.get_job(service_id)['data']
|
||||
templates = tdao.get_service_templates(service_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
@@ -25,7 +26,11 @@ def manage_service_templates(service_id):
|
||||
'views/manage-templates.html',
|
||||
service_id=service_id,
|
||||
has_jobs=bool(jobs),
|
||||
templates=[tdao.TemplatesBrowsableItem(x) for x in templates])
|
||||
templates=[
|
||||
Template(template)
|
||||
for template in tdao.get_service_templates(service_id)['data']
|
||||
]
|
||||
)
|
||||
|
||||
|
||||
@main.route("/services/<service_id>/templates/add", methods=['GET', 'POST'])
|
||||
@@ -56,14 +61,7 @@ def add_service_template(service_id):
|
||||
@main.route("/services/<service_id>/templates/<int:template_id>", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def edit_service_template(service_id, template_id):
|
||||
try:
|
||||
template = tdao.get_service_template(service_id, template_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
|
||||
template = tdao.get_service_template_or_404(service_id, template_id)['data']
|
||||
template['template_content'] = template['content']
|
||||
form = TemplateForm(**template)
|
||||
|
||||
@@ -84,21 +82,14 @@ def edit_service_template(service_id, template_id):
|
||||
@main.route("/services/<service_id>/templates/<int:template_id>/delete", methods=['GET', 'POST'])
|
||||
@login_required
|
||||
def delete_service_template(service_id, template_id):
|
||||
try:
|
||||
template = tdao.get_service_template(service_id, template_id)['data']
|
||||
except HTTPError as e:
|
||||
if e.status_code == 404:
|
||||
abort(404)
|
||||
else:
|
||||
raise e
|
||||
|
||||
template['template_content'] = template['content']
|
||||
form = TemplateForm(**template)
|
||||
template = tdao.get_service_template_or_404(service_id, template_id)['data']
|
||||
|
||||
if request.method == 'POST':
|
||||
tdao.delete_service_template(service_id, template_id)
|
||||
return redirect(url_for('.manage_service_templates', service_id=service_id))
|
||||
|
||||
template['template_content'] = template['content']
|
||||
form = TemplateForm(**template)
|
||||
flash('Are you sure you want to delete ‘{}’?'.format(form.name.data), 'delete')
|
||||
return render_template(
|
||||
'views/edit-template.html',
|
||||
|
||||
Reference in New Issue
Block a user