2016-01-11 15:00:51 +00:00
|
|
|
|
import csv
|
2016-01-29 15:35:35 +00:00
|
|
|
|
import uuid
|
2016-02-05 16:13:06 +00:00
|
|
|
|
import botocore
|
2016-02-08 16:36:53 +00:00
|
|
|
|
import re
|
|
|
|
|
|
import io
|
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-01-29 10:27:23 +00:00
|
|
|
|
from app import job_api_client
|
2016-02-17 15:49:07 +00:00
|
|
|
|
from app.utils import (
|
2016-02-01 16:57:40 +00:00
|
|
|
|
validate_phone_number,
|
|
|
|
|
|
InvalidPhoneError
|
|
|
|
|
|
)
|
2015-12-11 13:52:58 +00:00
|
|
|
|
|
2015-12-10 21:15:20 +00:00
|
|
|
|
|
2016-02-04 14:02:16 +00:00
|
|
|
|
@main.route("/services/<service_id>/sms/send", methods=['GET'])
|
2016-02-02 17:28:30 +00:00
|
|
|
|
def choose_sms_template(service_id):
|
2016-02-08 16:36:53 +00:00
|
|
|
|
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
|
|
|
|
|
|
)
|
2016-02-02 17:28:30 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@main.route("/services/<service_id>/sms/send/<template_id>", methods=['GET', 'POST'])
|
2016-01-11 15:00:51 +00:00
|
|
|
|
@login_required
|
2016-02-02 17:28:30 +00:00
|
|
|
|
def send_sms(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-01-15 17:46:09 +00:00
|
|
|
|
return redirect(url_for('.check_sms',
|
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-02 17:28:30 +00:00
|
|
|
|
return redirect(url_for('.send_sms', service_id=service_id, template_id=template_id))
|
2016-01-11 15:00:51 +00:00
|
|
|
|
|
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
|
|
|
|
)
|
|
|
|
|
|
|
2016-02-17 14:20:55 +00:00
|
|
|
|
example_data = [dict(
|
|
|
|
|
|
phone=current_user.mobile_number,
|
|
|
|
|
|
**{
|
|
|
|
|
|
header: "test {}".format(header) for header in template.placeholders
|
|
|
|
|
|
}
|
|
|
|
|
|
)]
|
|
|
|
|
|
|
2016-02-08 16:36:53 +00:00
|
|
|
|
return render_template(
|
|
|
|
|
|
'views/send-sms.html',
|
|
|
|
|
|
template=template,
|
2016-02-17 14:20:55 +00:00
|
|
|
|
column_headers=['phone'] + template.placeholders_as_markup,
|
|
|
|
|
|
placeholders=template.placeholders,
|
|
|
|
|
|
example_data=example_data,
|
2016-02-08 16:36:53 +00:00
|
|
|
|
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']
|
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)
|
|
|
|
|
|
writer.writerow(['phone'] + placeholders)
|
|
|
|
|
|
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-02 14:24:08 +00:00
|
|
|
|
@main.route("/services/<service_id>/sms/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-01-19 09:49:01 +00:00
|
|
|
|
def check_sms(service_id, upload_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-01 11:28:36 +00:00
|
|
|
|
upload_data = session['upload_data']
|
|
|
|
|
|
template_id = upload_data.get('template_id')
|
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-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-08 16:36:53 +00:00
|
|
|
|
drop_values={'phone'}
|
|
|
|
|
|
)
|
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-17 15:49:07 +00:00
|
|
|
|
column_headers=['phone number'] + list(
|
|
|
|
|
|
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
|
|
|
|
upload_data = session['upload_data']
|
|
|
|
|
|
original_file_name = upload_data.get('original_file_name')
|
|
|
|
|
|
template_id = upload_data.get('template_id')
|
|
|
|
|
|
session.pop('upload_data')
|
|
|
|
|
|
try:
|
|
|
|
|
|
job_api_client.create_job(upload_id, service_id, template_id, original_file_name)
|
|
|
|
|
|
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-01-19 09:49:01 +00:00
|
|
|
|
return redirect(url_for('main.view_job',
|
2016-01-14 16:00:13 +00:00
|
|
|
|
service_id=service_id,
|
|
|
|
|
|
job_id=upload_id))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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:
|
|
|
|
|
|
validate_phone_number(row['phone'])
|
2016-02-17 15:49:07 +00:00
|
|
|
|
Template(raw_template, values=row, drop_values={'phone'}).replaced
|
|
|
|
|
|
except (InvalidPhoneError, NeededByTemplateError, NoPlaceholderForDataError):
|
|
|
|
|
|
valid = False
|
|
|
|
|
|
return {"valid": valid, "rows": rows}
|