mirror of
https://github.com/GSA/notifications-api.git
synced 2026-02-03 18:01:08 -05:00
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:
@@ -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(
|
||||
|
||||
@@ -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):
|
||||
|
||||
@@ -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'])
|
||||
|
||||
Reference in New Issue
Block a user