Work on person creation email and bug fixes

This commit is contained in:
Scott Idem
2021-12-03 19:02:50 -05:00
parent 630739aae6
commit 39eb3baebd
4 changed files with 128 additions and 11 deletions

View File

@@ -5,7 +5,7 @@ from typing import Dict, List, Optional, Set, Union
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
from app.db_sql import redis_lookup_id_random, sql_insert, sql_insert_or_update, sql_select, sql_update
from app.lib_general import log, logging
from app.lib_general import log, logging, send_email
# from app.methods.address_methods import load_address_obj
from app.methods.contact_methods import create_contact_obj, create_update_contact_obj, create_update_contact_obj_v4, load_contact_obj, update_contact_obj
@@ -547,8 +547,8 @@ def create_update_person_obj_v4b(
# fail_any = fail_any,
):
if isinstance(create_user_obj_result, int):
log.info(f'User created. User ID: {user_id}')
log.debug(update_user_obj_result)
log.info(f'User created. User ID: {create_user_obj_result}')
log.debug(create_user_obj_result)
user_id = create_user_obj_result
# Need to update the person with the new user_id
update_person_obj = True
@@ -574,7 +574,7 @@ def create_update_person_obj_v4b(
else:
log.error(f'Person not updated with current User ID. Person ID: {person_id} User ID: {user_id}')
if fail_any: return False
log.debug(person_dict_up_result)
log.debug(person_data_up_result)
person_outline['user_id'] = user_id
else:
@@ -1412,3 +1412,83 @@ def update_person_obj(
# else:
# return False
# # ### END ### API Person Methods ### create_update_person_obj_v4b() ###
# ### BEGIN ### Person Methods ### email_person_create_url() ###
# This emails the actual one time use account creation URL for a person.
# Updated 2021-12-03
def email_person_create_url(
account_id: int|str,
person_id: int|str,
root_url: str,
):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
else: return False
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
else: return False
if person_obj := load_person_obj(
person_id = person_id,
):
log.info('Person object loaded')
else: return False
log.debug(person_obj)
from app.methods.account_cfg_methods import load_account_cfg_obj
if account_cfg := load_account_cfg_obj(
account_id = account_id,
):
log.info('Account config loaded')
else: return False
log.debug(account_cfg)
person_id_random = person_obj.id_random # NOTE: Not person_id_random because of alias
from_email = account_cfg.default_no_reply_email
from_name = account_cfg.default_no_reply_name
to_name = person_obj.display_name
to_email = person_obj.email
bcc_email = account_cfg.confirm_email
bcc_name = account_cfg.confirm_name
help_tech_email = account_cfg.help_tech_email
help_tech_name = account_cfg.help_tech_name
account_short_name = account_cfg.account_short_name
person_create_url = f'{root_url}person/{person_id_random}/create'
# person_create_auth_key_url = f'{root_url}?user_id={user_id_random}&auth_key={new_auth_key}'
subject = f'{account_short_name}: One Time Use Create Account Link'
# subject = f'{account_short_name}: One Time Use Create Account Link ({new_auth_key})'
body_html = f"""
<p>{to_name},</p>
<p>If you did not request this account creation link, please delete this email. It is suggested that you delete this email after the account creation link has been used or if a new link has been requested.</p>
<p>The link below can only be used once. If you would like try again using this method, you must <a href="NOT READY YET">request a new account creation link</a>. If you request multiple links, only the newest link will work.</p>
<p><strong><a href="{person_create_url}" style="appearance: button; display: inline-block; text-align: center; text-decoration: none; padding: .2rem .4rem; border: solid thin gray; border-radius: .2rem; background-color: lightyellow; color: black; font-size: larger;">Click to Finish Account Creation With One Time Use Link</a></strong></p>
<p>Or copy and paste the link:<br>
<strong style="background-color: lightyellow; color: black; font-size: larger;"><a href="{person_create_url}">{person_create_url}</a></strong></p>
<p>If you have questions about this email or trouble with this one time use link, you can email <a href="mailto:{help_tech_email}">{help_tech_name} ({help_tech_email})</a>.</p>
<p>Thank you!</p>
"""
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=None, body_html=body_html):
log.info(f'An email with a one time use sign in link was sent to {to_email}.')
return True
else:
log.info(f'An email with a one time use sign in link was not sent to {to_email}.')
return False
# ### END ### User ### email_user_auth_key_url() ###