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-02-05 16:13:06 +00:00
|
|
|
|
import botocore
|
2016-01-13 17:32:40 +00:00
|
|
|
|
|
|
|
|
|
|
from datetime import date
|
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-01-13 17:32:40 +00:00
|
|
|
|
from werkzeug import secure_filename
|
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-22 17:17:18 +00:00
|
|
|
|
from app.utils import validate_recipient, InvalidPhoneError, InvalidEmailError
|
|
|
|
|
|
|
|
|
|
|
|
first_column_header = {
|
|
|
|
|
|
'email': 'email',
|
|
|
|
|
|
'sms': 'phone'
|
|
|
|
|
|
}
|
2015-12-11 13:52:58 +00:00
|
|
|
|
|
2015-12-10 21:15:20 +00:00
|
|
|
|
|
2016-02-22 14:45:13 +00:00
|
|
|
|
@main.route("/services/<service_id>/send/<template_type>", methods=['GET'])
|
|
|
|
|
|
def choose_template(service_id, template_type):
|
2016-02-22 17:17:18 +00:00
|
|
|
|
|
|
|
|
|
|
services_dao.get_service_by_id_or_404(service_id)
|
|
|
|
|
|
|
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-08 16:36:53 +00:00
|
|
|
|
return render_template(
|
2016-02-22 14:45:13 +00:00
|
|
|
|
'views/choose-{}-template.html'.format(template_type),
|
2016-02-08 16:36:53 +00:00
|
|
|
|
templates=[
|
|
|
|
|
|
Template(template) 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-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-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-01-13 17:32:40 +00:00
|
|
|
|
csv_file = form.file.data
|
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-08 16:36:53 +00:00
|
|
|
|
flash('There was a problem uploading: {}'.format(csv_file.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-17 15:49:07 +00:00
|
|
|
|
templates_dao.get_service_template_or_404(service_id, template_id)['data']
|
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-22 17:17:18 +00:00
|
|
|
|
column_headers=[first_column_header[template.template_type]] + 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
|
|
|
|
|
|
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-22 17:17:18 +00:00
|
|
|
|
writer.writerow([first_column_header[template['template_type']]] + placeholders)
|
2016-02-17 14:20:55 +00:00
|
|
|
|
writer.writerow([current_user.mobile_number] + ["test {}".format(header) for header in placeholders])
|
2016-01-26 16:57:02 +00:00
|
|
|
|
|
2016-02-08 16:36:53 +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-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-22 17:17:18 +00:00
|
|
|
|
writer.writerow([first_column_header[template['template_type']]] + 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-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-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']
|
2016-02-22 17:17:18 +00:00
|
|
|
|
recipient_type = first_column_header[raw_template['template_type']]
|
2016-02-17 15:49:07 +00:00
|
|
|
|
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-22 17:17:18 +00:00
|
|
|
|
drop_values={recipient_type}
|
2016-02-08 16:36:53 +00:00
|
|
|
|
)
|
2015-12-10 21:15:20 +00:00
|
|
|
|
return render_template(
|
|
|
|
|
|
'views/check-sms.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-22 17:17:18 +00:00
|
|
|
|
column_headers=[recipient_type] + list(
|
2016-02-17 15:49:07 +00:00
|
|
|
|
template.placeholders if upload_result['valid'] else 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,
|
|
|
|
|
|
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):
|
|
|
|
|
|
lines = file.read().decode('utf-8').splitlines()
|
|
|
|
|
|
if len(lines) < 2: # must be at least header and one line
|
|
|
|
|
|
message = 'The file {} contained no data'.format(file.filename)
|
2016-01-13 17:32:40 +00:00
|
|
|
|
raise ValueError(message)
|
2016-01-29 10:27:23 +00:00
|
|
|
|
return {'file_name': file.filename, 'data': 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'),
|
|
|
|
|
|
lineterminator='\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
|
|
|
|
recipient_column = first_column_header[raw_template['template_type']]
|
|
|
|
|
|
validate_recipient(
|
|
|
|
|
|
row[recipient_column],
|
|
|
|
|
|
template_type=raw_template['template_type']
|
|
|
|
|
|
)
|
|
|
|
|
|
Template(raw_template, values=row, drop_values={recipient_column}).replaced
|
|
|
|
|
|
except (InvalidEmailError, InvalidPhoneError, NeededByTemplateError, NoPlaceholderForDataError):
|
2016-02-17 15:49:07 +00:00
|
|
|
|
valid = False
|
|
|
|
|
|
return {"valid": valid, "rows": rows}
|