From aade8504fac735bcf9e24f1cef9475c5c908b77a Mon Sep 17 00:00:00 2001 From: Scott Idem Date: Tue, 1 Oct 2024 15:04:12 -0400 Subject: [PATCH] Minor fixes. Cleaned up logging and send_email test mode. --- app/ae_obj_types_def.py | 32 ++++++++++++++ app/lib_general.py | 89 +++++++++++++++++++++----------------- app/models/event_models.py | 1 + app/routers/util_email.py | 15 ++++++- 4 files changed, 96 insertions(+), 41 deletions(-) diff --git a/app/ae_obj_types_def.py b/app/ae_obj_types_def.py index c80acc3..b1da501 100644 --- a/app/ae_obj_types_def.py +++ b/app/ae_obj_types_def.py @@ -122,6 +122,38 @@ obj_type_kv_li = { # 'tbl_name_update': 'event', # 'base_name': Event_Base, # 'base_name_alt': Event_Meeting_Flat_Base + 'exp_default': [ + 'event_id_random', + # 'external_person_id', + 'conference', 'type', + 'name', 'description', + # 'format', 'timezone', 'recurring', 'recurring_pattern', 'recurring_start_time', 'recurring_end_time', 'recurring_text', + # 'weekday_sunday', 'weekday_monday', 'weekday_tuesday', 'weekday_wednesday', 'weekday_thursday', 'weekday_friday', 'weekday_saturday', + # location_address_json, + 'location_text', 'physical', 'virtual', + 'attend_url', 'attend_url_text', 'attend_url_passcode', 'attend_phone', 'attend_phone_passcode', 'attend_text', + # attend_json, + 'contact_1_id', 'contact_2_id', + # contact_li_json, + 'enable', 'hide', 'priority', 'sort', 'group', 'notes', 'created_on', 'updated_on', + # default_qry_str, + ], + 'exp_idaa': [ + 'event_id_random', + 'external_person_id', # IDAA Novi ID + 'conference', 'type', + 'name', 'description', + 'format', 'timezone', 'recurring', 'recurring_pattern', 'recurring_start_time', 'recurring_end_time', 'recurring_text', + 'weekday_sunday', 'weekday_monday', 'weekday_tuesday', 'weekday_wednesday', 'weekday_thursday', 'weekday_friday', 'weekday_saturday', + # location_address_json, + 'location_text', 'physical', 'virtual', + 'attend_url', 'attend_url_passcode', 'attend_phone', 'attend_phone_passcode', 'attend_text', + # attend_json, + 'contact_1_id', 'contact_2_id', + # contact_li_json, + 'enable', 'hide', 'priority', 'sort', 'group', 'notes', 'created_on', 'updated_on', + # default_qry_str, + ], }, 'event_badge': { 'tbl': 'event_badge', diff --git a/app/lib_general.py b/app/lib_general.py index 16d1b7e..1fb17c0 100644 --- a/app/lib_general.py +++ b/app/lib_general.py @@ -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() ### diff --git a/app/models/event_models.py b/app/models/event_models.py index 9c2fb6e..8daeb98 100644 --- a/app/models/event_models.py +++ b/app/models/event_models.py @@ -381,6 +381,7 @@ class Event_Meeting_Flat_Base(BaseModel): attend_phone: Optional[str] attend_phone_passcode: Optional[str] attend_text: Optional[str] + attend_json: Optional[Union[Json, None]] # NOT FINISHED YET diff --git a/app/routers/util_email.py b/app/routers/util_email.py index 975600a..7b53c09 100644 --- a/app/routers/util_email.py +++ b/app/routers/util_email.py @@ -24,7 +24,7 @@ router = APIRouter() async def util_email_send_obj( email_send_obj: Email_Send_Base, - test: bool = False, + test: Optional[bool] = False, return_obj: bool = True, @@ -56,7 +56,18 @@ async def util_email_send_obj( # NOTE: This all works, but I think it could be done better??? - if send_email(from_email=from_email, from_name=from_name, to_email=to_email, to_name=to_name, bcc_email=bcc_email, bcc_name=bcc_name, subject=subject, body_text=body_text, body_html=body_html, test=test): + if send_email( + from_email = from_email, + from_name = from_name, + to_email = to_email, + to_name = to_name, + bcc_email = bcc_email, + bcc_name = bcc_name, + subject = subject, + body_text = body_text, + body_html = body_html, + test = test + ): status_code = 200 status_message = f'Email was sent to <{to_email}>.' else: