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 or not data['count_of_recipients'] or not data['recipients'].has_recipient_columns or data['recipients'].duplicate_recipient_column_headers or data['recipients'].missing_column_headers or data['sent_previously']): return render_template('views/check/column-errors.html', **data) if data['row_errors']: return render_template('views/check/row-errors.html', **data) if (data['errors'] or data['trying_to_send_letters_in_trial_mode']): return render_template('views/check/column-errors.html', **data) metadata_kwargs = { 'notification_count': data['count_of_recipients'], 'template_id': template_id, 'valid': True, 'original_file_name': data.get('original_file_name', ''), } if session.get('sender_id') and data['template'].template_type != 'letter': # sender_id is not an option for sending letters. metadata_kwargs['sender_id'] = session['sender_id'] set_metadata_on_csv_upload(service_id, upload_id, **metadata_kwargs) return render_template('views/check/ok.html', **data)
def copy_to_uploads(self): metadata = self.get_metadata(self.service_id, self.id) new_upload_id = s3upload( self.service_id, {'data': self.contents}, current_app.config['AWS_REGION'], ) set_metadata_on_csv_upload( self.service_id, new_upload_id, **metadata, ) return new_upload_id
def test_sets_metadata(client, mocker): mocked_s3_object = Mock() mocked_get_s3_object = mocker.patch( 'app.s3_client.s3_csv_client.get_csv_upload', return_value=mocked_s3_object, ) set_metadata_on_csv_upload('1234', '5678', foo='bar', baz=True) mocked_get_s3_object.assert_called_once_with('1234', '5678') mocked_s3_object.copy_from.assert_called_once_with( CopySource=current_app.config['CSV_UPLOAD_BUCKET_NAME'] + '/service-1234-notify/5678.csv', Metadata={'baz': 'True', 'foo': 'bar'}, MetadataDirective='REPLACE', ServerSideEncryption='AES256', )
def set_metadata(service_id, upload_id, **kwargs): return set_metadata_on_csv_upload( service_id, upload_id, bucket=ContactList.get_bucket_name(), **kwargs, )
def test_sets_metadata(client, mocker): mocked_s3_object = Mock() mocked_get_s3_object = mocker.patch( 'app.s3_client.s3_csv_client.get_csv_upload', return_value=mocked_s3_object, ) set_metadata_on_csv_upload('1234', '5678', foo='bar', baz=True) mocked_get_s3_object.assert_called_once_with('1234', '5678', bucket=None) mocked_s3_object.copy_from.assert_called_once_with( CopySource='test-notifications-csv-upload/service-1234-notify/5678.csv', Metadata={ 'baz': 'True', 'foo': 'bar' }, MetadataDirective='REPLACE', ServerSideEncryption='AES256', )
def test_sets_metadata(client, mocker): mocked_s3_object = Mock() mocked_get_s3_object = mocker.patch( "app.s3_client.s3_csv_client.get_csv_upload", return_value=mocked_s3_object, ) set_metadata_on_csv_upload("1234", "5678", foo="bar", baz=True) mocked_get_s3_object.assert_called_once_with("1234", "5678") mocked_s3_object.copy_from.assert_called_once_with( CopySource=current_app.config["CSV_UPLOAD_BUCKET_NAME"] + "/service-1234-notify/5678.csv", Metadata={ "baz": "True", "foo": "bar" }, MetadataDirective="REPLACE", ServerSideEncryption="AES256", )
def check_messages(service_id, template_id, upload_id, row_index=2): data = _check_messages(service_id, template_id, upload_id, row_index) if ( data['recipients'].too_many_rows or not data['count_of_recipients'] or not data['recipients'].has_recipient_columns or data['recipients'].duplicate_recipient_column_headers or data['recipients'].missing_column_headers or data['sent_previously'] ): return render_template('views/check/column-errors.html', **data) if data['row_errors']: return render_template('views/check/row-errors.html', **data) if ( data['errors'] or data['trying_to_send_letters_in_trial_mode'] ): return render_template('views/check/column-errors.html', **data) data['original_file_name'] = SanitiseASCII.encode(data.get('original_file_name', '')) metadata_kwargs = { 'notification_count': data['count_of_recipients'], 'template_id': str(template_id), 'valid': True, 'original_file_name': unicode_truncate( data['original_file_name'], 1600, ), } if session.get('sender_id'): metadata_kwargs['sender_id'] = session['sender_id'] set_metadata_on_csv_upload(service_id, upload_id, **metadata_kwargs) return render_template('views/check/ok.html', **data)
def check_messages(service_id, template_id, upload_id, row_index=2): data = _check_messages(service_id, template_id, upload_id, row_index) if (data["recipients"].too_many_rows or not data["count_of_recipients"] or not data["recipients"].has_recipient_columns or data["recipients"].duplicate_recipient_column_headers or data["recipients"].missing_column_headers or data["sent_previously"]): return render_template("views/check/column-errors.html", **data) if data["row_errors"]: return render_template("views/check/row-errors.html", **data) if data["errors"] or data["trying_to_send_letters_in_trial_mode"]: return render_template("views/check/column-errors.html", **data) data["original_file_name"] = SanitiseASCII.encode( data.get("original_file_name", "")) metadata_kwargs = { "notification_count": data["count_of_recipients"], "template_id": str(template_id), "valid": True, "original_file_name": unicode_truncate( data["original_file_name"], 1600, ), } if session.get("sender_id"): metadata_kwargs["sender_id"] = session["sender_id"] set_metadata_on_csv_upload(service_id, upload_id, **metadata_kwargs) return render_template("views/check/ok.html", **data)
def send_messages(service_id, template_id): db_template = current_service.get_template_with_user_permission_or_403( template_id, current_user) email_reply_to = None sms_sender = None if db_template['template_type'] == 'email': email_reply_to = get_email_reply_to_address_from_session() elif db_template['template_type'] == 'sms': sms_sender = get_sms_sender_from_session() if db_template[ 'template_type'] not in current_service.available_template_types: return redirect( url_for('.action_blocked', service_id=service_id, notification_type=db_template['template_type'], return_to='view_template', template_id=template_id)) template = get_template( db_template, current_service, show_recipient=True, letter_preview_url=url_for( 'no_cookie.view_letter_template_preview', service_id=service_id, template_id=template_id, filetype='png', page_count=get_page_count_for_letter(db_template), ), email_reply_to=email_reply_to, sms_sender=sms_sender, ) form = CsvUploadForm() if form.validate_on_submit(): try: upload_id = s3upload(service_id, Spreadsheet.from_file_form(form).as_dict, current_app.config['AWS_REGION']) file_name_metadata = unicode_truncate( SanitiseASCII.encode(form.file.data.filename), 1600) set_metadata_on_csv_upload(service_id, upload_id, original_file_name=file_name_metadata) return redirect( url_for( '.check_messages', service_id=service_id, upload_id=upload_id, template_id=template.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)) column_headings = get_spreadsheet_column_headings_from_template(template) return render_template( 'views/send.html', template=template, column_headings=list(ascii_uppercase[:len(column_headings)]), example=[column_headings, get_example_csv_rows(template)], form=form, allowed_file_extensions=Spreadsheet.ALLOWED_FILE_EXTENSIONS)