From b73a5d871007e19daa1291e50901394b800991af Mon Sep 17 00:00:00 2001 From: Adam Shimali Date: Mon, 1 Feb 2016 11:28:36 +0000 Subject: [PATCH] Pass selected template id to api when creating job. Moved query params for original file name and template id to session. --- app/main/views/sms.py | 35 +++++++++++++++++------------ app/notify_client/job_api_client.py | 4 ++-- app/templates/views/check-sms.html | 1 - tests/app/main/views/test_sms.py | 28 ++++++++++++----------- 4 files changed, 38 insertions(+), 30 deletions(-) diff --git a/app/main/views/sms.py b/app/main/views/sms.py index 8055f4c75..8cde9214f 100644 --- a/app/main/views/sms.py +++ b/app/main/views/sms.py @@ -10,7 +10,8 @@ from flask import ( redirect, url_for, flash, - abort + abort, + session ) from flask_login import login_required @@ -36,11 +37,12 @@ def send_sms(service_id): csv_file = form.file.data filedata = _get_filedata(csv_file) upload_id = str(uuid.uuid4()) + template_id = request.form.get('template') s3upload(upload_id, service_id, filedata) + session['upload_data'] = {"template_id": template_id, "original_file_name": filedata['file_name']} return redirect(url_for('.check_sms', service_id=service_id, - upload_id=upload_id, - file_name=filedata['file_name'])) + upload_id=upload_id)) except ValueError as e: message = 'There was a problem uploading: {}'.format( csv_file.filename) @@ -70,24 +72,29 @@ def check_sms(service_id, upload_id): if request.method == 'GET': contents = s3download(service_id, upload_id) upload_result = _get_numbers(contents) - file_name = request.args.get('file_name') + 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'] return render_template( 'views/check-sms.html', upload_result=upload_result, - file_name=file_name, - message_template=''' - ((name)), we’ve received your ((thing)). We’ll contact you again within 1 week. - ''', + message_template=template['content'], service_id=service_id ) elif request.method == 'POST': + 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 - file_name = request.form['file_name'] - # TODO - template id should come from form but is not wired in yet. - # that will be done in another story - template_id = 1 - - job_api_client.create_job(upload_id, service_id, template_id, file_name) return redirect(url_for('main.view_job', service_id=service_id, job_id=upload_id)) diff --git a/app/notify_client/job_api_client.py b/app/notify_client/job_api_client.py index 37350b29f..69ab377cb 100644 --- a/app/notify_client/job_api_client.py +++ b/app/notify_client/job_api_client.py @@ -20,12 +20,12 @@ class JobApiClient(BaseAPIClient): else: return self.get(url='/service/{}/job'.format(service_id)) - def create_job(self, job_id, service_id, template_id, file_name): + def create_job(self, job_id, service_id, template_id, original_file_name): data = { "id": job_id, "service": service_id, "template": template_id, - "original_file_name": file_name, + "original_file_name": original_file_name, "bucket_name": "service-{}-{}-notify".format(service_id, job_id), "file_name": "{}.csv".format(job_id) } diff --git a/app/templates/views/check-sms.html b/app/templates/views/check-sms.html index 80a216ca4..89726050e 100644 --- a/app/templates/views/check-sms.html +++ b/app/templates/views/check-sms.html @@ -59,7 +59,6 @@ back_link = url_for(".send_sms", service_id=service_id) )}} - {% endif %} {% endblock %} diff --git a/tests/app/main/views/test_sms.py b/tests/app/main/views/test_sms.py index 95d493eba..220890f4e 100644 --- a/tests/app/main/views/test_sms.py +++ b/tests/app/main/views/test_sms.py @@ -27,7 +27,8 @@ def test_upload_csvfile_with_invalid_phone_shows_check_page_with_errors(app_, api_user_active, mock_get_user, mock_get_user_by_email, - mock_login): + mock_login, + mock_get_service_template): contents = 'phone\n+44 123\n+44 456' file_data = (BytesIO(contents.encode('utf-8')), 'invalid.csv') @@ -53,7 +54,8 @@ def test_upload_csvfile_with_valid_phone_shows_first3_and_last3_numbers(app_, api_user_active, mock_get_user, mock_get_user_by_email, - mock_login): + mock_login, + mock_get_service_template): contents = 'phone\n+44 7700 900981\n+44 7700 900982\n+44 7700 900983\n+44 7700 900984\n+44 7700 900985\n+44 7700 900986\n+44 7700 900987\n+44 7700 900988\n+44 7700 900989' # noqa file_data = (BytesIO(contents.encode('utf-8')), 'valid.csv') @@ -89,7 +91,8 @@ def test_upload_csvfile_with_valid_phone_shows_all_if_6_or_less_numbers(app_, api_user_active, mock_get_user, mock_get_user_by_email, - mock_login): + mock_login, + mock_get_service_template): contents = 'phone\n+44 7700 900981\n+44 7700 900982\n+44 7700 900983\n+44 7700 900984\n+44 7700 900985\n+44 7700 900986' # noqa @@ -125,23 +128,22 @@ def test_create_job_should_call_api(app_, mock_login, job_data, mock_create_job, - mock_get_job): + mock_get_job, + mock_get_service_template): - import uuid service_id = service_one['id'] - job_id = str(uuid.uuid4()) - file_name = job_data['file_name'] - - # TODO - template id should come from form but is not wired in yet. - # that will be done in another story - template_id = 1 + job_id = job_data['id'] + original_file_name = job_data['original_file_name'] + template_id = job_data['template'] with app_.test_request_context(): with app_.test_client() as client: client.login(api_user_active) - url = url_for('main.check_sms', service_id=service_one['id'], upload_id=job_id, file_name=file_name) + with client.session_transaction() as session: + session['upload_data'] = {'original_file_name': original_file_name, 'template_id': template_id} + url = url_for('main.check_sms', service_id=service_one['id'], upload_id=job_id) response = client.post(url, data=job_data, follow_redirects=True) assert response.status_code == 200 - mock_create_job.assert_called_with(job_id, service_id, template_id, file_name) + mock_create_job.assert_called_with(job_id, service_id, template_id, original_file_name) assert job_data['bucket_name'] == "service-{}-{}-notify".format(service_id, job_id)