Minor fixes. Cleaned up logging and send_email test mode.
This commit is contained in:
@@ -122,6 +122,38 @@ obj_type_kv_li = {
|
|||||||
# 'tbl_name_update': 'event',
|
# 'tbl_name_update': 'event',
|
||||||
# 'base_name': Event_Base,
|
# 'base_name': Event_Base,
|
||||||
# 'base_name_alt': Event_Meeting_Flat_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': {
|
'event_badge': {
|
||||||
'tbl': 'event_badge',
|
'tbl': 'event_badge',
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
# from __future__ import annotations
|
import html2text, jwt, os, pandas, pathlib, time
|
||||||
import datetime, html2text, jwt, os, pandas, pathlib, pytz, redis, time
|
# No longer needed here: datetime, pytz, redis
|
||||||
from passlib.hash import argon2
|
from passlib.hash import argon2
|
||||||
|
|
||||||
# Import smtplib for the actual sending function
|
# Import smtplib for the actual sending function
|
||||||
@@ -8,10 +8,10 @@ import smtplib, ssl
|
|||||||
# Import the email package modules needed
|
# Import the email package modules needed
|
||||||
from email.message import EmailMessage
|
from email.message import EmailMessage
|
||||||
from email.headerregistry import Address
|
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 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 typing import Dict, List, Optional, Set, Union
|
||||||
|
|
||||||
from app.log import log, logging, logger_reset
|
from app.log import log, logging, logger_reset
|
||||||
@@ -382,11 +382,15 @@ def create_export_file(
|
|||||||
data_dataframe = pandas.DataFrame(data_dict_list)
|
data_dataframe = pandas.DataFrame(data_dict_list)
|
||||||
log.debug(data_dataframe)
|
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]
|
missing_cols = [col for col in column_name_li if col not in data_dataframe.columns]
|
||||||
if missing_cols:
|
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:
|
try:
|
||||||
if export_type == 'CSV':
|
if export_type == 'CSV':
|
||||||
log.info('Saving dataframe to CSV file')
|
log.info('Saving dataframe to CSV file')
|
||||||
@@ -474,11 +478,16 @@ def send_email(
|
|||||||
bcc_name: str = '',
|
bcc_name: str = '',
|
||||||
body_text: 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())
|
log.debug(locals())
|
||||||
|
|
||||||
|
if test:
|
||||||
|
log.setLevel(logging.DEBUG)
|
||||||
|
log.debug('[TESTING] Running with send_email() in TEST mode')
|
||||||
|
|
||||||
message = EmailMessage()
|
message = EmailMessage()
|
||||||
if subject:
|
if subject:
|
||||||
message['Subject'] = subject
|
message['Subject'] = subject
|
||||||
@@ -596,45 +605,47 @@ def send_email(
|
|||||||
log.info('Creating SMTP SSL connection...')
|
log.info('Creating SMTP SSL connection...')
|
||||||
context = ssl.create_default_context()
|
context = ssl.create_default_context()
|
||||||
|
|
||||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
||||||
log.info('SMTP configuration, connect, and send')
|
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:
|
if test:
|
||||||
log.setLevel(logging.DEBUG)
|
log.info('[TESTING] Email will NOT actually be sent! [TEST MODE]')
|
||||||
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
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
with smtplib.SMTP_SSL(settings.SMTP['server'], settings.SMTP['port'], context=context) as server:
|
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'])
|
server.login(settings.SMTP['username'], settings.SMTP['password'])
|
||||||
log.debug('SMTP send message...')
|
log.info('SMTP send message...')
|
||||||
server.send_message(message)
|
if not test:
|
||||||
log.info('Email sent!')
|
log.info('Email sent! Returning True')
|
||||||
|
server.send_message(message)
|
||||||
|
else:
|
||||||
|
log.info('[TESTING] Email (NOT) sent! Returning True [TEST MODE]')
|
||||||
return True
|
return True
|
||||||
except:
|
except:
|
||||||
#except SMTPException:
|
log.error('Error: Unable to send email. Returning False')
|
||||||
log.error('Error: unable to send email')
|
|
||||||
return False
|
return False
|
||||||
# ### END ### API Lib General ### send_email() ###
|
# ### END ### API Lib General ### send_email() ###
|
||||||
|
|||||||
@@ -381,6 +381,7 @@ class Event_Meeting_Flat_Base(BaseModel):
|
|||||||
attend_phone: Optional[str]
|
attend_phone: Optional[str]
|
||||||
attend_phone_passcode: Optional[str]
|
attend_phone_passcode: Optional[str]
|
||||||
attend_text: Optional[str]
|
attend_text: Optional[str]
|
||||||
|
attend_json: Optional[Union[Json, None]]
|
||||||
|
|
||||||
# NOT FINISHED YET
|
# NOT FINISHED YET
|
||||||
|
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ router = APIRouter()
|
|||||||
async def util_email_send_obj(
|
async def util_email_send_obj(
|
||||||
email_send_obj: Email_Send_Base,
|
email_send_obj: Email_Send_Base,
|
||||||
|
|
||||||
test: bool = False,
|
test: Optional[bool] = False,
|
||||||
|
|
||||||
return_obj: bool = True,
|
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???
|
# 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_code = 200
|
||||||
status_message = f'Email was sent to <{to_email}>.'
|
status_message = f'Email was sent to <{to_email}>.'
|
||||||
else:
|
else:
|
||||||
|
|||||||
Reference in New Issue
Block a user