def upload_contact_list(service_id): form = CsvUploadForm() if form.validate_on_submit(): try: upload_id = ContactList.upload( current_service.id, Spreadsheet.from_file_form(form).as_dict, ) file_name_metadata = unicode_truncate( SanitiseASCII.encode(form.file.data.filename), 1600) ContactList.set_metadata(current_service.id, upload_id, original_file_name=file_name_metadata) return redirect( url_for( '.check_contact_list', service_id=service_id, upload_id=upload_id, )) except (UnicodeDecodeError, BadZipFile, XLRDError): flash( 'Could not read {}. Try using a different file format.'.format( form.file.data.filename)) except (XLDateError): flash(( '{} contains numbers or dates that Notify cannot understand. ' 'Try formatting all columns as ‘text’ or export your file as CSV.' ).format(form.file.data.filename)) return render_template( 'views/uploads/contact-list/upload.html', form=form, allowed_file_extensions=Spreadsheet.ALLOWED_FILE_EXTENSIONS, )
def check_contact_list(service_id, upload_id): form = CsvUploadForm() contents = ContactList.download(service_id, upload_id) first_row = contents.splitlines()[0].strip().rstrip( ',') if contents else '' original_file_name = ContactList.get_metadata(service_id, upload_id).get( 'original_file_name', '') template_type = { 'emailaddress': 'email', 'phonenumber': 'sms', }.get(Columns.make_key(first_row)) recipients = RecipientCSV( contents, template=get_sample_template(template_type or 'sms'), whitelist=itertools.chain.from_iterable( [user.name, user.mobile_number, user.email_address] for user in current_service.active_users) if current_service.trial_mode else None, allow_international_sms=current_service.has_permission( 'international_sms'), max_initial_rows_shown=50, max_errors_shown=50, ) non_empty_column_headers = list(filter(None, recipients.column_headers)) if len(non_empty_column_headers ) > 1 or not template_type or not recipients: return render_template( 'views/uploads/contact-list/too-many-columns.html', recipients=recipients, 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): return render_template( 'views/uploads/contact-list/column-errors.html', 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) if row_errors: return render_template( 'views/uploads/contact-list/row-errors.html', recipients=recipients, original_file_name=original_file_name, row_errors=row_errors, form=form, allowed_file_extensions=Spreadsheet.ALLOWED_FILE_EXTENSIONS) if recipients.has_errors: return render_template( 'views/uploads/contact-list/column-errors.html', recipients=recipients, original_file_name=original_file_name, form=form, allowed_file_extensions=Spreadsheet.ALLOWED_FILE_EXTENSIONS) metadata_kwargs = { 'row_count': len(recipients), 'valid': True, 'original_file_name': original_file_name, 'template_type': template_type } ContactList.set_metadata(service_id, upload_id, **metadata_kwargs) return render_template( 'views/uploads/contact-list/ok.html', recipients=recipients, original_file_name=original_file_name, upload_id=upload_id, )