2016-01-11 15:00:51 +00:00
|
|
|
|
import csv
|
2016-02-12 10:55:21 +00:00
|
|
|
|
import io
|
2016-01-29 15:35:35 +00:00
|
|
|
|
import uuid
|
2016-01-11 15:00:51 +00:00
|
|
|
|
|
|
|
|
|
|
from flask import (
|
|
|
|
|
|
request,
|
|
|
|
|
|
render_template,
|
|
|
|
|
|
redirect,
|
|
|
|
|
|
url_for,
|
|
|
|
|
|
flash,
|
2016-02-01 11:28:36 +00:00
|
|
|
|
abort,
|
2016-02-02 22:26:49 +00:00
|
|
|
|
session,
|
2016-02-17 14:20:55 +00:00
|
|
|
|
current_app
|
2016-01-11 15:00:51 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
2016-02-17 14:20:55 +00:00
|
|
|
|
from flask_login import login_required, current_user
|
2016-02-11 15:27:08 +00:00
|
|
|
|
from notifications_python_client.errors import HTTPError
|
2016-02-17 15:49:07 +00:00
|
|
|
|
from utils.template import Template, NeededByTemplateError, NoPlaceholderForDataError
|
2015-12-10 21:15:20 +00:00
|
|
|
|
|
|
|
|
|
|
from app.main import main
|
2016-01-11 15:00:51 +00:00
|
|
|
|
from app.main.forms import CsvUploadForm
|
2016-01-14 16:00:13 +00:00
|
|
|
|
from app.main.uploader import (
|
|
|
|
|
|
s3upload,
|
|
|
|
|
|
s3download
|
|
|
|
|
|
)
|
2016-01-26 16:57:02 +00:00
|
|
|
|
from app.main.dao import templates_dao
|
2016-02-22 17:17:18 +00:00
|
|
|
|
from app.main.dao import services_dao
|
2016-01-29 10:27:23 +00:00
|
|
|
|
from app import job_api_client
|
2016-02-29 17:03:56 +00:00
|
|
|
|
from app.utils import (
|
|
|
|
|
|
validate_recipient, InvalidPhoneError, InvalidEmailError, user_has_permissions)
|
2016-02-22 17:17:18 +00:00
|
|
|
|
|
2016-02-25 16:37:39 +00:00
|
|
|
|
page_headings = {
|
2016-02-29 17:03:56 +00:00
|
|
|
|
'manage_service': {
|
|
|
|
|
|
'email': 'Send emails',
|
|
|
|
|
|
'sms': 'Send text messages'},
|
|
|
|
|
|
'manage_templates': {
|
|
|
|
|
|
'email': 'Manage templates',
|
|
|
|
|
|
'sms': 'Manage templates'
|
|
|
|
|
|
}
|
2016-02-25 16:37:39 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
2015-12-10 21:15:20 +00:00
|
|
|
|
|
2016-02-25 17:34:49 +00:00
|
|
|
|
@main.route("/services/<service_id>/send/letters", methods=['GET'])
|
|
|
|
|
|
def letters_stub(service_id):
|
|
|
|
|
|
return render_template(
|
|
|
|
|
|
'views/letters.html', service_id=service_id
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-22 14:45:13 +00:00
|
|
|
|
@main.route("/services/<service_id>/send/<template_type>", methods=['GET'])
|
2016-02-29 17:03:56 +00:00
|
|
|
|
@login_required
|
|
|
|
|
|
@user_has_permissions('send_messages', 'manage_templates', or_=True)
|
2016-02-22 14:45:13 +00:00
|
|
|
|
def choose_template(service_id, template_type):
|
2016-02-22 17:17:18 +00:00
|
|
|
|
|
2016-02-25 16:37:39 +00:00
|
|
|
|
service = services_dao.get_service_by_id_or_404(service_id)
|
2016-02-22 17:17:18 +00:00
|
|
|
|
|
2016-02-22 14:45:13 +00:00
|
|
|
|
if template_type not in ['email', 'sms']:
|
|
|
|
|
|
abort(404)
|
2016-02-23 11:43:40 +00:00
|
|
|
|
try:
|
|
|
|
|
|
jobs = job_api_client.get_job(service_id)['data']
|
|
|
|
|
|
except HTTPError as e:
|
|
|
|
|
|
if e.status_code == 404:
|
|
|
|
|
|
abort(404)
|
|
|
|
|
|
else:
|
|
|
|
|
|
raise e
|
2016-02-29 17:03:56 +00:00
|
|
|
|
# TODO fix up how page_heading is loaded.
|
|
|
|
|
|
page_heading = page_headings['manage_service'][template_type] if current_user.has_permissions(session.get('service_id', ''), 'manage_service') else \
|
|
|
|
|
|
page_headings['manage_templates'][template_type]
|
2016-02-08 16:36:53 +00:00
|
|
|
|
return render_template(
|
2016-02-25 16:37:39 +00:00
|
|
|
|
'views/choose-template.html',
|
2016-02-08 16:36:53 +00:00
|
|
|
|
templates=[
|
2016-02-26 09:33:45 +00:00
|
|
|
|
Template(
|
|
|
|
|
|
template,
|
|
|
|
|
|
prefix=service['name']
|
|
|
|
|
|
) for template in templates_dao.get_service_templates(service_id)['data']
|
2016-02-22 14:45:13 +00:00
|
|
|
|
if template['template_type'] == template_type
|
2016-02-08 16:36:53 +00:00
|
|
|
|
],
|
2016-02-25 16:37:39 +00:00
|
|
|
|
template_type=template_type,
|
2016-02-29 17:03:56 +00:00
|
|
|
|
page_heading=page_heading,
|
2016-02-25 16:37:39 +00:00
|
|
|
|
service=service,
|
2016-02-23 11:43:40 +00:00
|
|
|
|
has_jobs=len(jobs),
|
2016-02-08 16:36:53 +00:00
|
|
|
|
service_id=service_id
|
|
|
|
|
|
)
|
2016-02-02 17:28:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-02-22 14:45:13 +00:00
|
|
|
|
@main.route("/services/<service_id>/send/<int:template_id>", methods=['GET', 'POST'])
|
2016-01-11 15:00:51 +00:00
|
|
|
|
@login_required
|
2016-02-29 17:03:56 +00:00
|
|
|
|
@user_has_permissions('send_messages')
|
2016-02-22 17:17:18 +00:00
|
|
|
|
def send_messages(service_id, template_id):
|
2016-02-08 16:36:53 +00:00
|
|
|
|
|
2016-01-11 15:00:51 +00:00
|
|
|
|
form = CsvUploadForm()
|
2016-01-13 17:32:40 +00:00
|
|
|
|
if form.validate_on_submit():
|
2016-01-11 15:00:51 +00:00
|
|
|
|
try:
|
2016-02-26 10:54:06 +00:00
|
|
|
|
csv_file = form.file
|
2016-01-14 16:00:13 +00:00
|
|
|
|
filedata = _get_filedata(csv_file)
|
2016-01-29 15:35:35 +00:00
|
|
|
|
upload_id = str(uuid.uuid4())
|
2016-02-02 22:26:49 +00:00
|
|
|
|
s3upload(upload_id, service_id, filedata, current_app.config['AWS_REGION'])
|
2016-02-01 11:28:36 +00:00
|
|
|
|
session['upload_data'] = {"template_id": template_id, "original_file_name": filedata['file_name']}
|
2016-02-22 17:17:18 +00:00
|
|
|
|
return redirect(url_for('.check_messages',
|
2016-01-13 17:32:40 +00:00
|
|
|
|
service_id=service_id,
|
2016-02-01 11:28:36 +00:00
|
|
|
|
upload_id=upload_id))
|
2016-01-14 16:00:13 +00:00
|
|
|
|
except ValueError as e:
|
2016-02-26 10:54:06 +00:00
|
|
|
|
flash('There was a problem uploading: {}'.format(csv_file.data.filename))
|
2016-01-14 16:00:13 +00:00
|
|
|
|
flash(str(e))
|
2016-02-22 17:17:18 +00:00
|
|
|
|
return redirect(url_for('.send_messages', service_id=service_id, template_id=template_id))
|
2016-01-11 15:00:51 +00:00
|
|
|
|
|
2016-02-22 17:17:18 +00:00
|
|
|
|
service = services_dao.get_service_by_id_or_404(service_id)
|
2016-02-08 16:36:53 +00:00
|
|
|
|
template = Template(
|
2016-02-26 09:33:45 +00:00
|
|
|
|
templates_dao.get_service_template_or_404(service_id, template_id)['data'],
|
|
|
|
|
|
prefix=service['name']
|
2016-02-08 16:36:53 +00:00
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
return render_template(
|
2016-02-22 17:17:18 +00:00
|
|
|
|
'views/send.html',
|
2016-02-08 16:36:53 +00:00
|
|
|
|
template=template,
|
2016-02-25 11:30:09 +00:00
|
|
|
|
column_headers=['to'] + template.placeholders_as_markup,
|
2016-02-08 16:36:53 +00:00
|
|
|
|
form=form,
|
2016-02-22 17:17:18 +00:00
|
|
|
|
service=service,
|
2016-02-08 16:36:53 +00:00
|
|
|
|
service_id=service_id
|
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-22 14:45:13 +00:00
|
|
|
|
@main.route("/services/<service_id>/send/<template_id>.csv", methods=['GET'])
|
2016-02-08 16:36:53 +00:00
|
|
|
|
@login_required
|
2016-02-29 17:03:56 +00:00
|
|
|
|
@user_has_permissions('send_messages', 'manage_templates', or_=True)
|
2016-02-08 16:36:53 +00:00
|
|
|
|
def get_example_csv(service_id, template_id):
|
|
|
|
|
|
template = templates_dao.get_service_template_or_404(service_id, template_id)['data']
|
2016-02-17 14:20:55 +00:00
|
|
|
|
placeholders = list(Template(template).placeholders)
|
2016-02-08 16:36:53 +00:00
|
|
|
|
output = io.StringIO()
|
2016-02-17 14:20:55 +00:00
|
|
|
|
writer = csv.writer(output)
|
2016-02-25 11:30:09 +00:00
|
|
|
|
writer.writerow(['to'] + placeholders)
|
2016-02-25 17:16:02 +00:00
|
|
|
|
writer.writerow([
|
|
|
|
|
|
{
|
|
|
|
|
|
'email': current_user.email_address,
|
|
|
|
|
|
'sms': current_user.mobile_number
|
|
|
|
|
|
}[template['template_type']]
|
|
|
|
|
|
] + ["test {}".format(header) for header in placeholders])
|
2016-02-25 11:30:09 +00:00
|
|
|
|
return output.getvalue(), 200, {'Content-Type': 'text/csv; charset=utf-8'}
|
2015-12-10 21:15:20 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-02-22 14:45:13 +00:00
|
|
|
|
@main.route("/services/<service_id>/send/<template_id>/to-self", methods=['GET'])
|
2016-02-12 10:55:21 +00:00
|
|
|
|
@login_required
|
2016-02-29 17:03:56 +00:00
|
|
|
|
@user_has_permissions('send_messages')
|
2016-02-22 17:17:18 +00:00
|
|
|
|
def send_message_to_self(service_id, template_id):
|
2016-02-18 17:25:04 +00:00
|
|
|
|
template = templates_dao.get_service_template_or_404(service_id, template_id)['data']
|
|
|
|
|
|
placeholders = list(Template(template).placeholders)
|
2016-02-12 10:55:21 +00:00
|
|
|
|
output = io.StringIO()
|
|
|
|
|
|
writer = csv.writer(output)
|
2016-02-25 11:30:09 +00:00
|
|
|
|
writer.writerow(['to'] + placeholders)
|
2016-02-18 17:25:04 +00:00
|
|
|
|
writer.writerow([current_user.mobile_number] + ["test {}".format(header) for header in placeholders])
|
2016-02-12 10:55:21 +00:00
|
|
|
|
filedata = {
|
|
|
|
|
|
'file_name': 'Test run',
|
|
|
|
|
|
'data': output.getvalue().splitlines()
|
|
|
|
|
|
}
|
|
|
|
|
|
upload_id = str(uuid.uuid4())
|
|
|
|
|
|
s3upload(upload_id, service_id, filedata, current_app.config['AWS_REGION'])
|
|
|
|
|
|
session['upload_data'] = {"template_id": template_id, "original_file_name": filedata['file_name']}
|
|
|
|
|
|
|
2016-02-22 17:17:18 +00:00
|
|
|
|
return redirect(url_for('.check_messages',
|
2016-02-12 10:55:21 +00:00
|
|
|
|
service_id=service_id,
|
|
|
|
|
|
upload_id=upload_id))
|
|
|
|
|
|
|
|
|
|
|
|
|
2016-02-22 14:45:13 +00:00
|
|
|
|
@main.route("/services/<service_id>/check/<upload_id>",
|
2016-01-14 16:00:13 +00:00
|
|
|
|
methods=['GET', 'POST'])
|
2016-01-11 15:00:51 +00:00
|
|
|
|
@login_required
|
2016-02-29 17:03:56 +00:00
|
|
|
|
@user_has_permissions('send_messages')
|
2016-02-22 17:17:18 +00:00
|
|
|
|
def check_messages(service_id, upload_id):
|
|
|
|
|
|
|
|
|
|
|
|
upload_data = session['upload_data']
|
|
|
|
|
|
template_id = upload_data.get('template_id')
|
2016-02-25 16:37:39 +00:00
|
|
|
|
service = services_dao.get_service_by_id_or_404(service_id)
|
2016-01-29 12:19:50 +00:00
|
|
|
|
|
2016-01-11 15:00:51 +00:00
|
|
|
|
if request.method == 'GET':
|
2016-01-14 16:00:13 +00:00
|
|
|
|
contents = s3download(service_id, upload_id)
|
2016-02-05 16:13:06 +00:00
|
|
|
|
if not contents:
|
|
|
|
|
|
flash('There was a problem reading your upload file')
|
2016-02-17 15:49:07 +00:00
|
|
|
|
raw_template = templates_dao.get_service_template_or_404(service_id, template_id)['data']
|
|
|
|
|
|
upload_result = _get_rows(contents, raw_template)
|
2016-02-22 14:52:16 +00:00
|
|
|
|
session['upload_data']['notification_count'] = len(upload_result['rows'])
|
2016-02-08 16:36:53 +00:00
|
|
|
|
template = Template(
|
2016-02-17 15:49:07 +00:00
|
|
|
|
raw_template,
|
|
|
|
|
|
values=upload_result['rows'][0] if upload_result['valid'] else {},
|
2016-02-26 09:33:45 +00:00
|
|
|
|
drop_values={'to'},
|
|
|
|
|
|
prefix=service['name']
|
2016-02-08 16:36:53 +00:00
|
|
|
|
)
|
2015-12-10 21:15:20 +00:00
|
|
|
|
return render_template(
|
2016-02-25 16:37:39 +00:00
|
|
|
|
'views/check.html',
|
2016-01-11 15:00:51 +00:00
|
|
|
|
upload_result=upload_result,
|
2016-02-08 16:36:53 +00:00
|
|
|
|
template=template,
|
2016-02-25 16:37:39 +00:00
|
|
|
|
page_heading=page_headings[template.template_type],
|
2016-02-26 09:35:07 +00:00
|
|
|
|
column_headers=['to'] + list(template.placeholders_as_markup),
|
2016-02-08 16:36:53 +00:00
|
|
|
|
original_file_name=upload_data.get('original_file_name'),
|
2016-02-17 14:20:55 +00:00
|
|
|
|
service_id=service_id,
|
2016-02-25 16:37:39 +00:00
|
|
|
|
service=service,
|
2016-02-17 14:20:55 +00:00
|
|
|
|
form=CsvUploadForm()
|
2015-12-10 21:15:20 +00:00
|
|
|
|
)
|
|
|
|
|
|
elif request.method == 'POST':
|
2016-02-01 11:28:36 +00:00
|
|
|
|
original_file_name = upload_data.get('original_file_name')
|
2016-02-22 14:52:16 +00:00
|
|
|
|
notification_count = upload_data.get('notification_count')
|
2016-02-01 11:28:36 +00:00
|
|
|
|
session.pop('upload_data')
|
|
|
|
|
|
try:
|
2016-02-22 14:52:16 +00:00
|
|
|
|
job_api_client.create_job(upload_id, service_id, template_id, original_file_name, notification_count)
|
2016-02-01 11:28:36 +00:00
|
|
|
|
except HTTPError as e:
|
|
|
|
|
|
if e.status_code == 404:
|
|
|
|
|
|
abort(404)
|
|
|
|
|
|
else:
|
|
|
|
|
|
raise e
|
2016-01-29 10:27:23 +00:00
|
|
|
|
|
2016-02-04 15:26:43 +00:00
|
|
|
|
flash('We’ve started sending your messages', 'default_with_tick')
|
2016-02-22 17:17:18 +00:00
|
|
|
|
return redirect(
|
|
|
|
|
|
url_for('main.view_job', service_id=service_id, job_id=upload_id)
|
|
|
|
|
|
)
|
2016-01-14 16:00:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def _get_filedata(file):
|
2016-02-26 10:54:06 +00:00
|
|
|
|
import itertools
|
|
|
|
|
|
reader = csv.reader(
|
|
|
|
|
|
file.data.getvalue().decode('utf-8').splitlines(),
|
|
|
|
|
|
quoting=csv.QUOTE_NONE,
|
|
|
|
|
|
skipinitialspace=True
|
|
|
|
|
|
)
|
|
|
|
|
|
lines = []
|
|
|
|
|
|
for row in reader:
|
|
|
|
|
|
non_empties = itertools.dropwhile(lambda x: x.strip() == '', row)
|
|
|
|
|
|
has_content = []
|
|
|
|
|
|
for item in non_empties:
|
|
|
|
|
|
has_content.append(item)
|
|
|
|
|
|
if has_content:
|
|
|
|
|
|
lines.append(row)
|
|
|
|
|
|
|
|
|
|
|
|
if len(lines) < 2: # must be header row and at least one data row
|
|
|
|
|
|
message = 'The file {} contained no data'.format(file.data.filename)
|
2016-01-13 17:32:40 +00:00
|
|
|
|
raise ValueError(message)
|
2016-02-26 10:54:06 +00:00
|
|
|
|
|
|
|
|
|
|
content_lines = []
|
|
|
|
|
|
for row in lines:
|
|
|
|
|
|
content_lines.append(','.join(row).rstrip(','))
|
|
|
|
|
|
return {'file_name': file.data.filename, 'data': content_lines}
|
2016-01-13 17:32:40 +00:00
|
|
|
|
|
|
|
|
|
|
|
2016-02-17 15:49:07 +00:00
|
|
|
|
def _get_rows(contents, raw_template):
|
2016-01-14 16:00:13 +00:00
|
|
|
|
reader = csv.DictReader(
|
|
|
|
|
|
contents.split('\n'),
|
2016-02-18 15:50:28 +00:00
|
|
|
|
quoting=csv.QUOTE_NONE,
|
|
|
|
|
|
skipinitialspace=True
|
2016-02-17 15:49:07 +00:00
|
|
|
|
)
|
|
|
|
|
|
valid = True
|
|
|
|
|
|
rows = []
|
|
|
|
|
|
for row in reader:
|
|
|
|
|
|
rows.append(row)
|
2016-02-01 16:57:40 +00:00
|
|
|
|
try:
|
2016-02-22 17:17:18 +00:00
|
|
|
|
validate_recipient(
|
2016-02-25 16:37:39 +00:00
|
|
|
|
row.get('to', ''),
|
2016-02-22 17:17:18 +00:00
|
|
|
|
template_type=raw_template['template_type']
|
|
|
|
|
|
)
|
2016-02-25 11:30:09 +00:00
|
|
|
|
Template(raw_template, values=row, drop_values={'to'}).replaced
|
2016-02-22 17:17:18 +00:00
|
|
|
|
except (InvalidEmailError, InvalidPhoneError, NeededByTemplateError, NoPlaceholderForDataError):
|
2016-02-17 15:49:07 +00:00
|
|
|
|
valid = False
|
|
|
|
|
|
return {"valid": valid, "rows": rows}
|