Biz logic

This commit is contained in:
Rebecca Hsieh 2024-02-28 09:17:59 -08:00
parent 010853a5f0
commit fdb7fb6594
No known key found for this signature in database
2 changed files with 78 additions and 4 deletions

View file

@ -2,10 +2,12 @@
import logging
import os
import pyzipper
from django.core.management import BaseCommand
from registrar.utility import csv_export
from registrar.utility.s3_bucket import S3ClientHelper
from ...utility.email import send_templated_email, EmailSendingError
logger = logging.getLogger(__name__)
@ -59,9 +61,27 @@ class Command(BaseCommand):
We want to make sure to upload to s3 for back up
And now we also want to get the file and encrypt it so we can send it in an email
"""
# metadata_file = s3_client.get_file(file_name)
# metadata_file.encryptthisthingherewithpyzipper
# email.blasend_email(metadata_file)
unencrypted_metadata_input = s3_client.get_file(file_name)
# Encrypt metadata into a zip file
# pre-setting zip file name
encrypted_metadata_output = 'encrypted_metadata.zip'
# set this to be an env var somewhere
password = b'somepasswordhere'
# encrypted_metadata is the encrypted output
encrypted_metadata = _encrypt_metadata(unencrypted_metadata_input, encrypted_metadata_output, password)
print("encrypted_metadata is:", encrypted_metadata)
# Send the metadata file that is zipped
# Q: Would we set the vars I set in email.py here to pass in to the helper function or best way to invoke
# send_templated_email(encrypted_metadata, attachment=True)
def _encrypt_metadata(input_file, output_file, password):
with open(input_file, 'rb') as f_in:
with pyzipper.AESZipFile(output_file, 'w', compression=pyzipper.ZIP_LZMA, encryption=pyzipper.WZ_AES) as f_out:
f_out.setpassword(password)
f_out.writestr(input_file, f_in.read())
return output_file

View file

@ -40,6 +40,8 @@ def send_templated_email(template_name: str, subject_template_name: str, to_addr
except Exception as exc:
raise EmailSendingError("Could not access the SES client.") from exc
# Are we okay with passing in "attachment" var in as boolean parameter
# If so, TODO: add attachment boolean to other functions
try:
#if not attachment:
ses_client.send_email(
@ -55,5 +57,57 @@ def send_templated_email(template_name: str, subject_template_name: str, to_addr
# else: # has attachment
# same as above but figure out how to attach a file
# via boto3 "boto3 SES file attachment"
# we also want this to only send to the help email
# from email.mime.multipart import MIMEMultipart
# from email.mime.text import MIMEText
# from email.mime.application import MIMEApplication
# sender_email = 'sender@example.com'
# recipient_email = 'help@get.gov'
# subject = 'DOTGOV-Full Domain Metadata'
# body = 'Domain metadata email, should have an attachment included change here later.'
# attachment_path = 'path/to/attachment/file.pdf'
# aws_region = 'sesv2'
# response = send_email_with_attachment(sender_email, recipient_email, subject, body, attachment_path, aws_region)
# print(response)
except Exception as exc:
raise EmailSendingError("Could not send SES email.") from exc
# def send_email_with_attachment(sender, recipient, subject, body, attachment_path, aws_region):
# # Create a multipart/mixed parent container
# msg = MIMEMultipart('mixed')
# msg['Subject'] = subject
# msg['From'] = sender_email
# msg['To'] = recipient_email
# # Add the text part
# text_part = MIMEText(body, 'plain')
# msg.attach(text_part)
# # Add the attachment part
# with open(attachment_path, 'rb') as attachment_file:
# attachment_data = attachment_file.read()
# attachment_part = MIMEApplication(attachment_data)
# attachment_part.add_header('Content-Disposition', f'attachment; filename="{attachment_path}"')
# msg.attach(attachment_part)
# # Send the email
# response = ses_client.send_raw_email(
# Source=sender,
# Destinations=[recipient],
# RawMessage={'Data': msg.as_string()}
# )
# ses_client.send_email(
# FromEmailAddress=settings.DEFAULT_FROM_EMAIL,
# Destination={"ToAddresses": [to_address]},
# Content={
# "Simple": {
# "Subject": {"Data": subject},
# "Body": {"Text": {"Data": email_body}},
# },
# },
# )