mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-02-05 10:53:28 -05:00
Merge pull request #124 from alphagov/create-job
Uploading csv creates job via api
This commit is contained in:
@@ -1,6 +1,5 @@
|
||||
import csv
|
||||
import re
|
||||
import os
|
||||
|
||||
from datetime import date
|
||||
|
||||
@@ -9,14 +8,13 @@ from flask import (
|
||||
render_template,
|
||||
redirect,
|
||||
url_for,
|
||||
session,
|
||||
flash,
|
||||
current_app,
|
||||
abort
|
||||
)
|
||||
|
||||
from flask_login import login_required
|
||||
from werkzeug import secure_filename
|
||||
from client.errors import HTTPError
|
||||
|
||||
from app.main import main
|
||||
from app.main.forms import CsvUploadForm
|
||||
@@ -25,6 +23,7 @@ from app.main.uploader import (
|
||||
s3download
|
||||
)
|
||||
from app.main.dao import templates_dao
|
||||
from app import job_api_client
|
||||
|
||||
|
||||
@main.route("/services/<int:service_id>/sms/send", methods=['GET', 'POST'])
|
||||
@@ -38,7 +37,8 @@ def send_sms(service_id):
|
||||
upload_id = s3upload(service_id, filedata)
|
||||
return redirect(url_for('.check_sms',
|
||||
service_id=service_id,
|
||||
upload_id=upload_id))
|
||||
upload_id=upload_id,
|
||||
file_name=filedata['file_name']))
|
||||
except ValueError as e:
|
||||
message = 'There was a problem uploading: {}'.format(
|
||||
csv_file.filename)
|
||||
@@ -67,18 +67,23 @@ def check_sms(service_id, upload_id):
|
||||
if request.method == 'GET':
|
||||
contents = s3download(service_id, upload_id)
|
||||
upload_result = _get_numbers(contents)
|
||||
# TODO get original file name
|
||||
file_name = request.args.get('file_name')
|
||||
return render_template(
|
||||
'views/check-sms.html',
|
||||
upload_result=upload_result,
|
||||
filename='someupload_file_name.csv',
|
||||
file_name=file_name,
|
||||
message_template='''
|
||||
((name)), we’ve received your ((thing)). We’ll contact you again within 1 week.
|
||||
''',
|
||||
service_id=service_id
|
||||
)
|
||||
elif request.method == 'POST':
|
||||
# TODO create the job with template, file location etc.
|
||||
file_name = request.form['original_file_name']
|
||||
|
||||
# TODO need a real template id picked from form
|
||||
template_id = 1
|
||||
|
||||
job_api_client.create_job(service_id, template_id, file_name)
|
||||
return redirect(url_for('main.view_job',
|
||||
service_id=service_id,
|
||||
job_id=upload_id))
|
||||
@@ -89,7 +94,7 @@ def _get_filedata(file):
|
||||
if len(lines) < 2: # must be at least header and one line
|
||||
message = 'The file {} contained no data'.format(file.filename)
|
||||
raise ValueError(message)
|
||||
return {'filename': file.filename, 'data': lines}
|
||||
return {'file_name': file.filename, 'data': lines}
|
||||
|
||||
|
||||
def _format_filename(filename):
|
||||
@@ -100,7 +105,7 @@ def _format_filename(filename):
|
||||
|
||||
|
||||
def _get_numbers(contents):
|
||||
pattern = re.compile(r'^\+44\s?\d{4}\s?\d{6}$') # need better validation
|
||||
pattern = re.compile(r'^\+44\s?\d{4}\s?\d{6}$') # TODO need better validation
|
||||
reader = csv.DictReader(
|
||||
contents.split('\n'),
|
||||
lineterminator='\n',
|
||||
|
||||
Reference in New Issue
Block a user