mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-06 00:28:25 -04:00
Fix bug.
This commit is contained in:
68
app/utils.py
68
app/utils.py
@@ -128,20 +128,47 @@ def get_errors_for_csv(recipients, template_type):
|
|||||||
|
|
||||||
def generate_notifications_csv(**kwargs):
|
def generate_notifications_csv(**kwargs):
|
||||||
from app import notification_api_client
|
from app import notification_api_client
|
||||||
fieldnames, list_of_keys = _create_key_and_fieldnames_for_csv(kwargs)
|
|
||||||
|
|
||||||
if 'page' not in kwargs:
|
if 'page' not in kwargs:
|
||||||
kwargs['page'] = 1
|
kwargs['page'] = 1
|
||||||
|
|
||||||
|
if kwargs['job_id']:
|
||||||
|
fieldnames = ['Row number', 'Recipient', 'Template', 'Type', 'Job', 'Status', 'Time']
|
||||||
|
else:
|
||||||
|
fieldnames = ['Recipient', 'Template', 'Type', 'Job', 'Status', 'Time']
|
||||||
|
|
||||||
yield ','.join(fieldnames) + '\n'
|
yield ','.join(fieldnames) + '\n'
|
||||||
|
|
||||||
while kwargs['page']:
|
while kwargs['page']:
|
||||||
|
# if job_id then response looks different
|
||||||
notifications_resp = notification_api_client.get_notifications_for_service(**kwargs)
|
notifications_resp = notification_api_client.get_notifications_for_service(**kwargs)
|
||||||
notifications = notifications_resp['notifications']
|
notifications = notifications_resp['notifications']
|
||||||
|
if kwargs['job_id']:
|
||||||
for notification in notifications:
|
for notification in notifications:
|
||||||
values = [notification[x] for x in list_of_keys]
|
values = [
|
||||||
line = ','.join(str(i) for i in values) + '\n'
|
notification['row_number'],
|
||||||
yield line
|
notification['recipient'],
|
||||||
|
notification['template_name'],
|
||||||
|
notification['template_type'],
|
||||||
|
notification['job_name'],
|
||||||
|
notification['status'],
|
||||||
|
notification['created_at']
|
||||||
|
]
|
||||||
|
line = ','.join(str(i) for i in values) + '\n'
|
||||||
|
yield line
|
||||||
|
else:
|
||||||
|
# Change here
|
||||||
|
for notification in notifications:
|
||||||
|
values = [
|
||||||
|
notification['to'],
|
||||||
|
notification['template']['name'],
|
||||||
|
notification['template']['template_type'],
|
||||||
|
notification.get('job_name', None),
|
||||||
|
notification['status'],
|
||||||
|
notification['created_at'],
|
||||||
|
notification['updated_at']
|
||||||
|
]
|
||||||
|
line = ','.join(str(i) for i in values) + '\n'
|
||||||
|
yield line
|
||||||
if notifications_resp['links'].get('next'):
|
if notifications_resp['links'].get('next'):
|
||||||
kwargs['page'] += 1
|
kwargs['page'] += 1
|
||||||
else:
|
else:
|
||||||
@@ -149,33 +176,6 @@ def generate_notifications_csv(**kwargs):
|
|||||||
raise Exception("Should never reach here")
|
raise Exception("Should never reach here")
|
||||||
|
|
||||||
|
|
||||||
def _create_key_and_fieldnames_for_csv(kwargs):
|
|
||||||
# if job_id then response looks different
|
|
||||||
if kwargs['job_id']:
|
|
||||||
list_of_keys = [
|
|
||||||
'row_number',
|
|
||||||
'recipient',
|
|
||||||
'template_name',
|
|
||||||
'template_type',
|
|
||||||
'job_name',
|
|
||||||
'status',
|
|
||||||
'created_at'
|
|
||||||
]
|
|
||||||
fieldnames = ['Row number', 'Recipient', 'Template', 'Type', 'Job', 'Status', 'Time']
|
|
||||||
else:
|
|
||||||
list_of_keys = [
|
|
||||||
'recipient',
|
|
||||||
'template_name',
|
|
||||||
'template_type',
|
|
||||||
'job_name',
|
|
||||||
'status',
|
|
||||||
'created_at',
|
|
||||||
'updated_at'
|
|
||||||
]
|
|
||||||
fieldnames = ['Recipient', 'Template', 'Type', 'Job', 'Status', 'Time', 'Completed at']
|
|
||||||
return fieldnames, list_of_keys
|
|
||||||
|
|
||||||
|
|
||||||
def get_page_from_request():
|
def get_page_from_request():
|
||||||
if 'page' in request.args:
|
if 'page' in request.args:
|
||||||
try:
|
try:
|
||||||
|
|||||||
@@ -43,12 +43,15 @@ def _get_notifications_csv(
|
|||||||
data = {
|
data = {
|
||||||
'notifications': [{
|
'notifications': [{
|
||||||
"row_number": row_number,
|
"row_number": row_number,
|
||||||
|
"to": recipient,
|
||||||
"recipient": recipient,
|
"recipient": recipient,
|
||||||
"template_name": template_name,
|
"template_name": template_name,
|
||||||
"template_type": template_type,
|
"template_type": template_type,
|
||||||
|
"template": {"name": template_name, "template_type": template_type},
|
||||||
"job_name": job_name,
|
"job_name": job_name,
|
||||||
"status": status,
|
"status": status,
|
||||||
"created_at": created_at
|
"created_at": created_at,
|
||||||
|
"updated_at": None
|
||||||
} for i in range(rows)],
|
} for i in range(rows)],
|
||||||
'total': rows,
|
'total': rows,
|
||||||
'page_size': 50,
|
'page_size': 50,
|
||||||
@@ -136,7 +139,8 @@ def test_generate_notifications_csv_only_calls_once_if_no_next_link(_get_notific
|
|||||||
assert _get_notifications_csv_mock.call_count == 1
|
assert _get_notifications_csv_mock.call_count == 1
|
||||||
|
|
||||||
|
|
||||||
def test_generate_notifications_csv_calls_twice_if_next_link(mocker):
|
@pytest.mark.parametrize("job_id", ["some", None])
|
||||||
|
def test_generate_notifications_csv_calls_twice_if_next_link(mocker, job_id):
|
||||||
service_id = '1234'
|
service_id = '1234'
|
||||||
response_with_links = _get_notifications_csv(service_id, rows=7, with_links=True)
|
response_with_links = _get_notifications_csv(service_id, rows=7, with_links=True)
|
||||||
response_with_no_links = _get_notifications_csv(service_id, rows=3, with_links=False)
|
response_with_no_links = _get_notifications_csv(service_id, rows=3, with_links=False)
|
||||||
@@ -149,7 +153,7 @@ def test_generate_notifications_csv_calls_twice_if_next_link(mocker):
|
|||||||
]
|
]
|
||||||
)
|
)
|
||||||
|
|
||||||
csv_content = generate_notifications_csv(service_id=service_id, job_id=fake_uuid)
|
csv_content = generate_notifications_csv(service_id=service_id, job_id=fake_uuid if job_id else None)
|
||||||
csv = DictReader(StringIO('\n'.join(csv_content)))
|
csv = DictReader(StringIO('\n'.join(csv_content)))
|
||||||
|
|
||||||
assert len(list(csv)) == 10
|
assert len(list(csv)) == 10
|
||||||
|
|||||||
Reference in New Issue
Block a user