mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-09-07 11:28:25 -04:00
Use context managers for StringIO
It’s possible that leaving a StringIO stream open could cause a memory leak. Using a context manager closes it automatically when it’s finished with.
This commit is contained in:
@@ -146,20 +146,16 @@ def send_messages(service_id, template_id):
|
|||||||
@user_has_permissions('send_texts', 'send_emails', 'send_letters', 'manage_templates', any_=True)
|
@user_has_permissions('send_texts', 'send_emails', 'send_letters', 'manage_templates', any_=True)
|
||||||
def get_example_csv(service_id, template_id):
|
def get_example_csv(service_id, template_id):
|
||||||
template = Template(service_api_client.get_service_template(service_id, template_id)['data'])
|
template = Template(service_api_client.get_service_template(service_id, template_id)['data'])
|
||||||
# Good practice to use context managers
|
with io.StringIO() as output:
|
||||||
# http://stackoverflow.com/questions/9718950/do-i-have-to-do-stringio-close
|
writer = csv.writer(output)
|
||||||
# For this instance it may not be a problem but someone else looking at the
|
writer.writerows(
|
||||||
# code may assume its always safe when it might not be.
|
[
|
||||||
output = io.StringIO()
|
[first_column_heading[template.template_type]] +
|
||||||
writer = csv.writer(output)
|
list(template.placeholders)
|
||||||
writer.writerows(
|
] +
|
||||||
[
|
get_example_csv_rows(template)
|
||||||
[first_column_heading[template.template_type]] +
|
)
|
||||||
list(template.placeholders)
|
return output.getvalue(), 200, {'Content-Type': 'text/csv; charset=utf-8'}
|
||||||
] +
|
|
||||||
get_example_csv_rows(template)
|
|
||||||
)
|
|
||||||
return output.getvalue(), 200, {'Content-Type': 'text/csv; charset=utf-8'}
|
|
||||||
|
|
||||||
|
|
||||||
@main.route("/services/<service_id>/send/<template_id>/to-self", methods=['GET'])
|
@main.route("/services/<service_id>/send/<template_id>/to-self", methods=['GET'])
|
||||||
@@ -167,29 +163,27 @@ def get_example_csv(service_id, template_id):
|
|||||||
@user_has_permissions('send_texts', 'send_emails', 'send_letters')
|
@user_has_permissions('send_texts', 'send_emails', 'send_letters')
|
||||||
def send_message_to_self(service_id, template_id):
|
def send_message_to_self(service_id, template_id):
|
||||||
template = Template(service_api_client.get_service_template(service_id, template_id)['data'])
|
template = Template(service_api_client.get_service_template(service_id, template_id)['data'])
|
||||||
# Good practice to use context managers
|
|
||||||
# http://stackoverflow.com/questions/9718950/do-i-have-to-do-stringio-close
|
|
||||||
# For this instance it may not be a problem but someone else looking at the
|
|
||||||
# code may assume its always safe when it might not be.
|
|
||||||
output = io.StringIO()
|
|
||||||
writer = csv.writer(output)
|
|
||||||
writer.writerow(
|
|
||||||
[first_column_heading[template.template_type]] +
|
|
||||||
list(template.placeholders)
|
|
||||||
)
|
|
||||||
if template.template_type == 'sms':
|
|
||||||
writer.writerow(
|
|
||||||
[current_user.mobile_number] + _get_fake_personalisation(template.placeholders, 1)
|
|
||||||
)
|
|
||||||
if template.template_type == 'email':
|
|
||||||
writer.writerow(
|
|
||||||
[current_user.email_address] + _get_fake_personalisation(template.placeholders, 1)
|
|
||||||
)
|
|
||||||
|
|
||||||
filedata = {
|
with io.StringIO() as output:
|
||||||
'file_name': 'Test run',
|
writer = csv.writer(output)
|
||||||
'data': output.getvalue()
|
writer.writerow(
|
||||||
}
|
[first_column_heading[template.template_type]] +
|
||||||
|
list(template.placeholders)
|
||||||
|
)
|
||||||
|
if template.template_type == 'sms':
|
||||||
|
writer.writerow(
|
||||||
|
[current_user.mobile_number] + _get_fake_personalisation(template.placeholders, 1)
|
||||||
|
)
|
||||||
|
if template.template_type == 'email':
|
||||||
|
writer.writerow(
|
||||||
|
[current_user.email_address] + _get_fake_personalisation(template.placeholders, 1)
|
||||||
|
)
|
||||||
|
|
||||||
|
filedata = {
|
||||||
|
'file_name': 'Test run',
|
||||||
|
'data': output.getvalue()
|
||||||
|
}
|
||||||
|
|
||||||
upload_id = str(uuid.uuid4())
|
upload_id = str(uuid.uuid4())
|
||||||
|
|
||||||
s3upload(upload_id, service_id, filedata, current_app.config['AWS_REGION'])
|
s3upload(upload_id, service_id, filedata, current_app.config['AWS_REGION'])
|
||||||
|
|||||||
Reference in New Issue
Block a user