From 4dfd5448adcbf762d702e0bd39d086bcee7a7811 Mon Sep 17 00:00:00 2001 From: Imdad Ahad Date: Thu, 20 Apr 2017 12:31:49 +0100 Subject: [PATCH] Make get all notications return in csv format too: * This adds functionality (via an extra req param) to the * existing get all notifications method allowing us to specify * when we want the API to return in csv/non-csv format --- app/job/rest.py | 18 ++++++++---------- app/schemas.py | 1 + tests/app/job/test_rest.py | 28 ++++++++++++++++++++++++++++ 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/app/job/rest.py b/app/job/rest.py index e1a982b7e..cb0ec9539 100644 --- a/app/job/rest.py +++ b/app/job/rest.py @@ -69,7 +69,6 @@ def get_all_notifications_for_service_job(service_id, job_id): data = notifications_filter_schema.load(request.args).data page = data['page'] if 'page' in data else 1 page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE') - pagination = get_notifications_for_job( service_id, job_id, @@ -78,18 +77,17 @@ def get_all_notifications_for_service_job(service_id, job_id): page_size=page_size) kwargs = request.args.to_dict() - - if page_size > 50: - current_app.logger.info('Current page: {}'.format(page)) - current_app.logger.info('Page size: {}'.format(page_size)) - current_app.logger.info('Total pages in pagination: {}'.format(pagination.pages)) - current_app.logger.info('Total notifications in pagination query: {}'.format(pagination.total)) - current_app.logger.info('Arguments for next query: {}'.format(kwargs)) - kwargs['service_id'] = service_id kwargs['job_id'] = job_id + + notifications = None + if data.get('format_for_csv'): + notifications = [notification.serialize_for_csv() for notification in pagination.items] + else: + notifications = notification_with_template_schema.dump(pagination.items, many=True).data + return jsonify( - notifications=notification_with_template_schema.dump(pagination.items, many=True).data, + notifications=notifications, page_size=page_size, total=pagination.total, links=pagination_links( diff --git a/app/schemas.py b/app/schemas.py index 203ba0934..9402a7e44 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -490,6 +490,7 @@ class NotificationsFilterSchema(ma.Schema): include_jobs = fields.Boolean(required=False) include_from_test_key = fields.Boolean(required=False) older_than = fields.UUID(required=False) + format_for_csv = fields.String() @pre_load def handle_multidict(self, in_data): diff --git a/tests/app/job/test_rest.py b/tests/app/job/test_rest.py index 05e9d00e5..7fbac98d8 100644 --- a/tests/app/job/test_rest.py +++ b/tests/app/job/test_rest.py @@ -715,3 +715,31 @@ def create_10_jobs(db, session, service, template): for _ in range(10): the_time.tick(timedelta(hours=1)) create_job(db, session, service, template) + + +def test_get_all_notifications_for_job_returns_csv_format( + client, + notify_db, + notify_db_session, +): + job = create_job(notify_db, notify_db_session) + notification = create_notification( + notify_db, + notify_db_session, + job=job, + job_row_number=1, + created_at=datetime.utcnow(), + ) + + response = client.get( + path='/service/{}/job/{}/notifications?'.format(notification.service.id, job.id, True), + headers=[create_authorization_header()], + query_string={'format_for_csv': True} + ) + assert response.status_code == 200 + + resp = json.loads(response.get_data(as_text=True)) + assert len(resp['notifications']) == 1 + notification = resp['notifications'][0] + assert set(notification.keys()) == \ + set(['created_at', 'template_type', 'template_name', 'job_name', 'status', 'row_number', 'recipient'])