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
This commit is contained in:
Imdad Ahad
2017-04-20 12:31:49 +01:00
parent 9fbd43c072
commit 4dfd5448ad
3 changed files with 37 additions and 10 deletions

View File

@@ -69,7 +69,6 @@ def get_all_notifications_for_service_job(service_id, job_id):
data = notifications_filter_schema.load(request.args).data data = notifications_filter_schema.load(request.args).data
page = data['page'] if 'page' in data else 1 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') page_size = data['page_size'] if 'page_size' in data else current_app.config.get('PAGE_SIZE')
pagination = get_notifications_for_job( pagination = get_notifications_for_job(
service_id, service_id,
job_id, job_id,
@@ -78,18 +77,17 @@ def get_all_notifications_for_service_job(service_id, job_id):
page_size=page_size) page_size=page_size)
kwargs = request.args.to_dict() 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['service_id'] = service_id
kwargs['job_id'] = job_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( return jsonify(
notifications=notification_with_template_schema.dump(pagination.items, many=True).data, notifications=notifications,
page_size=page_size, page_size=page_size,
total=pagination.total, total=pagination.total,
links=pagination_links( links=pagination_links(

View File

@@ -490,6 +490,7 @@ class NotificationsFilterSchema(ma.Schema):
include_jobs = fields.Boolean(required=False) include_jobs = fields.Boolean(required=False)
include_from_test_key = fields.Boolean(required=False) include_from_test_key = fields.Boolean(required=False)
older_than = fields.UUID(required=False) older_than = fields.UUID(required=False)
format_for_csv = fields.String()
@pre_load @pre_load
def handle_multidict(self, in_data): def handle_multidict(self, in_data):

View File

@@ -715,3 +715,31 @@ def create_10_jobs(db, session, service, template):
for _ in range(10): for _ in range(10):
the_time.tick(timedelta(hours=1)) the_time.tick(timedelta(hours=1))
create_job(db, session, service, template) 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'])