diff --git a/app/__init__.py b/app/__init__.py index 4c7b35cf9..ec0ce3771 100644 --- a/app/__init__.py +++ b/app/__init__.py @@ -528,6 +528,15 @@ def format_number_in_pounds_as_currency(number): return f"{number * 100:.0f}p" +def format_list_items(items, format_string, *args, **kwargs): + """ + Apply formatting to each item in an iterable. Returns a list. + Each item is made available in the format_string as the 'item' keyword argument. + example usage: ['png','svg','pdf']|format_list_items('{0}. {item}', [1,2,3]) -> ['1. png', '2. svg', '3. pdf'] + """ + return [format_string.format(*args, item=item, **kwargs) for item in items] + + @login_manager.user_loader def load_user(user_id): return User.from_id(user_id) @@ -818,6 +827,7 @@ def add_template_filters(application): format_thousands, id_safe, convert_to_boolean, + format_list_items, ]: application.add_template_filter(fn) diff --git a/app/main/views/send.py b/app/main/views/send.py index 4c06ffbdf..43b43f197 100644 --- a/app/main/views/send.py +++ b/app/main/views/send.py @@ -193,7 +193,8 @@ def send_messages(service_id, template_id): template=template, column_headings=list(ascii_uppercase[:len(column_headings)]), example=[column_headings, get_example_csv_rows(template)], - form=form + form=form, + allowed_file_extensions=Spreadsheet.ALLOWED_FILE_EXTENSIONS ) @@ -709,6 +710,7 @@ def _check_messages(service_id, template_id, upload_id, preview_row, letters_as_ def check_messages(service_id, template_id, upload_id, row_index=2): data = _check_messages(service_id, template_id, upload_id, row_index) + data['allowed_file_extensions'] = Spreadsheet.ALLOWED_FILE_EXTENSIONS if ( data['recipients'].too_many_rows diff --git a/app/main/views/uploads.py b/app/main/views/uploads.py index 0f5db2448..b2609bde6 100644 --- a/app/main/views/uploads.py +++ b/app/main/views/uploads.py @@ -313,6 +313,7 @@ def uploaded_letter_preview(service_id, file_id): message=error_message, error_code=error_shortcode, form=form, + allowed_file_extensions=Spreadsheet.ALLOWED_FILE_EXTENSIONS, postal_address=postal_address, re_upload_form=re_upload_form ) @@ -413,6 +414,7 @@ def upload_contact_list(service_id): return render_template( 'views/uploads/contact-list/upload.html', form=form, + allowed_file_extensions=Spreadsheet.ALLOWED_FILE_EXTENSIONS, ) @@ -455,6 +457,7 @@ def check_contact_list(service_id, upload_id): original_file_name=original_file_name, template_type=template_type, form=form, + allowed_file_extensions=Spreadsheet.ALLOWED_FILE_EXTENSIONS ) if recipients.too_many_rows or not len(recipients): @@ -463,6 +466,7 @@ def check_contact_list(service_id, upload_id): recipients=recipients, original_file_name=original_file_name, form=form, + allowed_file_extensions=Spreadsheet.ALLOWED_FILE_EXTENSIONS ) row_errors = get_errors_for_csv(recipients, template_type) @@ -473,6 +477,7 @@ def check_contact_list(service_id, upload_id): original_file_name=original_file_name, row_errors=row_errors, form=form, + allowed_file_extensions=Spreadsheet.ALLOWED_FILE_EXTENSIONS ) if recipients.has_errors: @@ -481,6 +486,7 @@ def check_contact_list(service_id, upload_id): recipients=recipients, original_file_name=original_file_name, form=form, + allowed_file_extensions=Spreadsheet.ALLOWED_FILE_EXTENSIONS ) metadata_kwargs = { diff --git a/app/templates/components/file-upload.html b/app/templates/components/file-upload.html index 1908f1e48..b24f55264 100644 --- a/app/templates/components/file-upload.html +++ b/app/templates/components/file-upload.html @@ -2,6 +2,7 @@ {% macro file_upload( field, + allowed_file_extensions, action=None, button_text="Choose file", alternate_link=None, @@ -25,7 +26,8 @@ {% endif %} {{ field(**{ - 'class': 'file-upload-field' + 'class': 'file-upload-field', + 'accept': allowed_file_extensions|format_list_items('.{item}')|join(',')|e }) }}