Minor fixes. Cleaned up logging and send_email test mode.

This commit is contained in:
Scott Idem
2024-10-01 15:04:12 -04:00
parent 58e331f85c
commit aade8504fa
4 changed files with 96 additions and 41 deletions

View File

@@ -1,5 +1,5 @@
# from __future__ import annotations
import datetime, html2text, jwt, os, pandas, pathlib, pytz, redis, time
import html2text, jwt, os, pandas, pathlib, time
# No longer needed here: datetime, pytz, redis
from passlib.hash import argon2
# Import smtplib for the actual sending function
@@ -8,10 +8,10 @@ import smtplib, ssl
# Import the email package modules needed
from email.message import EmailMessage
from email.headerregistry import Address
from email.utils import make_msgid
# from email.utils import make_msgid
from fastapi import APIRouter, Depends, Header, HTTPException, Response, status
from pydantic import BaseModel, EmailStr, Field
# from pydantic import BaseModel, EmailStr, Field
from typing import Dict, List, Optional, Set, Union
from app.log import log, logging, logger_reset
@@ -382,11 +382,15 @@ def create_export_file(
data_dataframe = pandas.DataFrame(data_dict_list)
log.debug(data_dataframe)
# Need to deal with this error when a field is empty (null/None) for every record in the export: KeyError("Not all names specified in 'columns' are found")
# There does not seem to be a Panda option to ignore missing columns
missing_cols = [col for col in column_name_li if col not in data_dataframe.columns]
if missing_cols:
raise KeyError(f"The following columns are not found in the DataFrame: {', '.join(missing_cols)}")
# Remove the missing columns from the column list
column_name_li = [col for col in column_name_li if col not in missing_cols]
# raise KeyError(f"The following columns are not found in the DataFrame: {', '.join(missing_cols)}")
# Need to deal with this error: KeyError("Not all names specified in 'columns' are found")
try:
if export_type == 'CSV':
log.info('Saving dataframe to CSV file')
@@ -474,11 +478,16 @@ def send_email(
bcc_name: str = '',
body_text: str = '',
test: bool = False
test: bool = False,
log_lvl: int = logging.WARNING, # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
):
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.setLevel(log_lvl)
log.debug(locals())
if test:
log.setLevel(logging.DEBUG)
log.debug('[TESTING] Running with send_email() in TEST mode')
message = EmailMessage()
if subject:
message['Subject'] = subject
@@ -596,45 +605,47 @@ def send_email(
log.info('Creating SMTP SSL connection...')
context = ssl.create_default_context()
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.info('SMTP configuration, connect, and send')
log.info(f'Server: {settings.SMTP["server"]} Port: {settings.SMTP["port"]} Username: {settings.SMTP["username"]}')
log.setLevel(logging.DEBUG)
log.info(settings.SMTP['server'])
log.info(settings.SMTP['port'])
log.info(settings.SMTP['username'])
log.info(settings.SMTP['password'])
# log.info(settings.SMTP['server'])
# log.info(settings.SMTP['port'])
# log.info(settings.SMTP['username'])
# log.info(settings.SMTP['password'])
# if test:
# try:
# with smtplib.SMTP_SSL(settings.SMTP['server'], settings.SMTP['port'], context=context) as server:
# log.info('[TESTING] SMTP log in...')
# server.login(settings.SMTP['username'], settings.SMTP['password'])
# log.info('[TESTING] SMTP send message... [WILL NOT SEND IN TEST MODE]')
# # server.send_message(message)
# log.info('[TESTING] Email (NOT) sent!')
# return True
# # except SMTPException:
# except:
# log.error('[TEST] Error: Unable to send email. Returning False')
# return False
log.info('Trying smtplib.SMTP_SSL in send_email()...')
if test:
log.setLevel(logging.DEBUG)
log.info('[TEST] Test mode enabled')
log.info(settings.SMTP['server'])
log.info(settings.SMTP['port'])
log.info(settings.SMTP['username'])
log.info(settings.SMTP['password'])
try:
with smtplib.SMTP_SSL(settings.SMTP['server'], settings.SMTP['port'], context=context) as server:
log.debug('[TEST] SMTP log in...')
server.login(settings.SMTP['username'], settings.SMTP['password'])
log.debug('[TEST] SMTP send message... [WILL NOT SEND IN TEST MODE]')
# server.send_message(message)
log.info('[TEST] Email sent!')
return True
except:
#except SMTPException:
log.error('[TEST] Error: unable to send email')
return False
log.info('[TESTING] Email will NOT actually be sent! [TEST MODE]')
try:
with smtplib.SMTP_SSL(settings.SMTP['server'], settings.SMTP['port'], context=context) as server:
log.debug('SMTP log in...')
log.info('SMTP log in...')
log.debug(f'Server: {settings.SMTP["server"]} Port: {settings.SMTP["port"]} Username: {settings.SMTP["username"]} Password: {settings.SMTP["password"]}')
server.login(settings.SMTP['username'], settings.SMTP['password'])
log.debug('SMTP send message...')
server.send_message(message)
log.info('Email sent!')
log.info('SMTP send message...')
if not test:
log.info('Email sent! Returning True')
server.send_message(message)
else:
log.info('[TESTING] Email (NOT) sent! Returning True [TEST MODE]')
return True
except:
#except SMTPException:
log.error('Error: unable to send email')
log.error('Error: Unable to send email. Returning False')
return False
# ### END ### API Lib General ### send_email() ###