def test_create_email_notification_with_csv_file_upload( notifications_client, rmock): endpoint = "{0}/v2/notifications/email".format(TEST_HOST) rmock.request("POST", endpoint, json={"status": "success"}, status_code=200) with open('tests/test_files/test.csv', 'rb') as f: notifications_client.send_email_notification( email_address="*****@*****.**", template_id="456", personalisation={ 'name': 'chris', 'doc': prepare_upload(f, is_csv=True) }) assert rmock.last_request.json() == { 'template_id': '456', 'email_address': '*****@*****.**', 'personalisation': { 'name': 'chris', 'doc': { 'file': 'VGhpcyBpcyBhIGNzdiwK', 'is_csv': True } } }
def test_create_email_notification_with_document_file_upload( notifications_client, rmock): endpoint = "{0}/v2/notifications/email".format(TEST_HOST) rmock.request("POST", endpoint, json={"status": "success"}, status_code=200) with open('tests/test_files/test.pdf', 'rb') as f: notifications_client.send_email_notification( email_address="*****@*****.**", template_id="456", personalisation={ 'name': 'chris', 'doc': prepare_upload(f) }) assert rmock.last_request.json() == { 'template_id': '456', 'email_address': '*****@*****.**', 'personalisation': { 'name': 'chris', 'doc': { 'file': 'JVBERi0xLjUgdGVzdAo=' } } }
def test_create_email_notification_with_document_stream_upload( notifications_client, rmock): endpoint = "{0}/v2/notifications/email".format(TEST_HOST) rmock.request("POST", endpoint, json={"status": "success"}, status_code=200) if hasattr(io, 'BytesIO'): mock_file = io.BytesIO(b'file-contents') else: mock_file = io.StringIO('file-contents') notifications_client.send_email_notification( email_address="*****@*****.**", template_id="456", personalisation={ 'name': 'chris', 'doc': prepare_upload(mock_file) }) assert rmock.last_request.json() == { 'template_id': '456', 'email_address': '*****@*****.**', 'personalisation': { 'name': 'chris', 'doc': { 'file': 'ZmlsZS1jb250ZW50cw==', 'is_csv': False } } }
def __send_email__(self, emails, data): notifications_client = NotificationsAPIClient(settings.NOTIFY_API_KEY) FILE_NAME = 'report.csv' with open(FILE_NAME, 'w') as csvFile: f = csv.writer(csvFile) f.writerows(data['csv']) csvFile.close() for to in emails: with open(FILE_NAME, 'rb') as f: response = notifications_client.send_email_notification( email_address=to, template_id=settings.NOTIFY_TEMPLATE_ID, personalisation={ 'subject': data['subject'], 'content': data['content'], 'summary': data['summary'], 'report': prepare_upload(f), 'signature': data['signature'] } ) os.remove(FILE_NAME)
def send(file, email): logging.info("email %s", email) response = notifications_client.send_email_notification( email_address=email, # required string template_id=template_id, # required UUID string personalisation={'link_to_file': prepare_upload(file)} ) __import__('pprint').pprint(response) logging.info("done sending email notification")
def send_buy_from_uk_enquiries_as_csv( form_url='/international/trade/contact/'): """A method to create last seven days data for buy from uk contact details""" today = timezone.now() week_ago = today - timedelta(days=7) submissions = models.Submission.objects.filter(created__gte=week_ago, form_url=form_url) with open('email.csv', 'w+') as csvfile: filewriter = csv.writer(csvfile) filewriter.writerow([ 'Sender', 'body', 'sector', 'source', 'country', 'given_name', 'family_name', 'country_name', 'phone_number', 'source_other', 'email_address', 'organisation_name', 'organisation_size', 'email_contact_consent', 'telephone_contact_consent', 'Form', 'Created' ]) for submission in submissions: filewriter.writerow([ submission.sender, submission.data['body'], submission.data['sector'], submission.data['source'], submission.data['country'], submission.data['given_name'], submission.data['family_name'], submission.data['country_name'], submission.data['phone_number'], submission.data['source_other'], submission.data['email_address'], submission.data['organisation_name'], submission.data['organisation_size'], submission.data['email_contact_consent'], submission.data['telephone_contact_consent'], submission.form_url, submission.created, ]) with open('email.csv', 'rb') as f: send_gov_notify_email( template_id=settings.BUY_FROM_UK_ENQUIRY_TEMPLATE_ID, email_address=settings.BUY_FROM_UK_EMAIL_ADDRESS, personalisation={'link_to_file': prepare_upload(f, is_csv=True)}, email_reply_to_id=settings.BUY_FROM_UK_REPLY_TO_EMAIL_ADDRESS)
def test_prepare_upload_raises_an_error_for_large_files(): with pytest.raises(ValueError) as exc: prepare_upload(io.BytesIO(b'a' * 3 * 1024 * 1024)) assert 'larger than 2MB' in str(exc.value)
def test_prepare_upload_generates_expected_dict(is_csv): file_content = b'a' * 256 file_dict = prepare_upload(io.BytesIO(file_content), is_csv=is_csv) assert file_dict['is_csv'] == is_csv assert file_dict['file'] == base64.b64encode(file_content).decode('ascii')