Streamline

This commit is contained in:
zandercymatics 2024-06-17 14:18:33 -06:00
parent adc2013c7c
commit de6ff1d7d1
No known key found for this signature in database
GPG key ID: FF4636ABEC9682B7

View file

@ -23,7 +23,6 @@ class Command(BaseCommand):
"which is based off of all existing Domains." "which is based off of all existing Domains."
) )
current_date = datetime.now().strftime("%m%d%Y") current_date = datetime.now().strftime("%m%d%Y")
email_to: str
def add_arguments(self, parser): def add_arguments(self, parser):
"""Add our two filename arguments.""" """Add our two filename arguments."""
@ -35,31 +34,27 @@ class Command(BaseCommand):
def handle(self, **options): def handle(self, **options):
"""Grabs the directory then creates domain-metadata.csv in that directory""" """Grabs the directory then creates domain-metadata.csv in that directory"""
self.email_to = options.get("emailTo") zip_filename = f"domain-metadata-{self.current_date}.zip"
email_to = options.get("emailTo")
# Don't email to DEFAULT_FROM_EMAIL when not prod. # Don't email to DEFAULT_FROM_EMAIL when not prod.
if not settings.IS_PRODUCTION and self.email_to == settings.DEFAULT_FROM_EMAIL: if not settings.IS_PRODUCTION and email_to == settings.DEFAULT_FROM_EMAIL:
raise ValueError( raise ValueError(
"The --emailTo arg must be specified in non-prod environments, " "The --emailTo arg must be specified in non-prod environments, "
"and the arg must not equal the DEFAULT_FROM_EMAIL value (aka: help@get.gov)." "and the arg must not equal the DEFAULT_FROM_EMAIL value (aka: help@get.gov)."
) )
logger.info("Generating report...") logger.info("Generating report...")
zip_filename = f"domain-metadata-{self.current_date}.zip"
try: try:
self.email_current_metadata_report(zip_filename) self.email_current_metadata_report(zip_filename, email_to)
except Exception as err: except Exception as err:
# TODO - #1317: Notify operations when auto report generation fails # TODO - #1317: Notify operations when auto report generation fails
raise err raise err
else: else:
logger.info(f"Success! Created {zip_filename} and successfully sent out an email!") logger.info(f"Success! Created {zip_filename} and successfully sent out an email!")
def email_current_metadata_report(self, zip_filename): def email_current_metadata_report(self, zip_filename, email_to):
"""Creates a current-metadata.csv file under the specified directory, """Emails a password protected zip containing domain-metadata and domain-request-metadata"""
then uploads it to a AWS S3 bucket. This is done for resiliency
reasons in the event our application goes down and/or the email
cannot send -- we'll still be able to grab info from the S3
instance"""
reports = { reports = {
"Domain report": { "Domain report": {
"report_filename": f"domain-metadata-{self.current_date}.csv", "report_filename": f"domain-metadata-{self.current_date}.csv",
@ -81,7 +76,7 @@ class Command(BaseCommand):
send_templated_email( send_templated_email(
template_name="emails/metadata_body.txt", template_name="emails/metadata_body.txt",
subject_template_name="emails/metadata_subject.txt", subject_template_name="emails/metadata_subject.txt",
to_address=self.email_to, to_address=email_to,
context={"current_date_str": datetime.now().strftime("%Y-%m-%d")}, context={"current_date_str": datetime.now().strftime("%Y-%m-%d")},
attachment_file=encrypted_zip_in_bytes, attachment_file=encrypted_zip_in_bytes,
) )
@ -96,13 +91,10 @@ class Command(BaseCommand):
zip_filename, "w", compression=pyzipper.ZIP_DEFLATED, encryption=pyzipper.WZ_AES zip_filename, "w", compression=pyzipper.ZIP_DEFLATED, encryption=pyzipper.WZ_AES
) as f_out: ) as f_out:
f_out.setpassword(str.encode(password)) f_out.setpassword(str.encode(password))
for report_name, report_value in reports.items(): for report_name, report in reports.items():
report_filename = report_value["report_filename"] logger.info(f"Generating {report_name}")
report_function = report_value["report_function"] report = self.write_and_return_report(report["report_function"])
f_out.writestr(report["report_filename"], report)
report = self.write_and_return_report(report_function)
f_out.writestr(report_filename, report)
logger.info(f"Generated {report_name}")
# Get the final report for emailing purposes # Get the final report for emailing purposes
with open(zip_filename, "rb") as file_data: with open(zip_filename, "rb") as file_data: