Work on event, event_session, event_presentation, event_presenter, user, and person routes, methods, and models

This commit is contained in:
Scott Idem
2021-08-21 16:04:17 -04:00
parent 67b8435e08
commit 824bdd29a2
19 changed files with 631 additions and 80 deletions

View File

@@ -1,5 +1,5 @@
from __future__ import annotations
import datetime
import datetime, random
from typing import Dict, List, Optional, Set, Union
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
@@ -21,22 +21,27 @@ from app.models.user_models import User_Base, User_New_Base, User_Out_Base
# ### BEGIN ### API User Methods ### create_user_obj() ###
# NOTE: This will create a new user and also hash a new password string if given.
# NOTE: This uses the User_New_Base model, not User_Base or User_Out_Base
# Reviewed and updated 2021-08-21
# Reviewed and updated 2021-08-10
def create_user_obj(user_obj_new:User_New_Base) -> int|bool:
def create_user_obj(
user_obj_new: User_New_Base,
allow_update: bool = False, # Allow updating the user account if one is found
avoid_dup_username: bool = False, # Avoid creating a duplicate by modifying the supplied username
return_dict: bool = False,
) -> bool|dict|int:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if not user_obj_new:
return False
# user_obj_data = user_obj_new.dict(by_alias=False, exclude_defaults=False, include={'password'}, exclude={'new_password'})
user_obj_data = user_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'new_password'})
user_obj_data = user_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'new_password', 'organization', 'person', 'created_on', 'updated_on'})
log.debug(user_obj_data)
user_obj_data['password'] = user_obj_new.password # There has to be a better way to do this??? It thinks "password" is unset and so is excluded?
log.debug(user_obj_data)
log.info('Checking if the username is already in use for the account...')
account_id = user_obj_data.get('account_id', None)
username = user_obj_data.get('username', None)
log.info(f'Checking if the username is already in use for the account... Account: {account_id} Username: {username}')
sql_select_user = f"""
SELECT user.id, user.id_random, user.name, user.email
FROM `user` AS user
@@ -45,30 +50,47 @@ def create_user_obj(user_obj_new:User_New_Base) -> int|bool:
if sql_select_result := sql_select(sql=sql_select_user, data=user_obj_data, rm_id_random=True):
if isinstance(sql_select_result, list):
log.exception(f"Multiple user accounts already exists with this username ({user_obj_data['username']}). The database needs to be checked.")
log.exception(f'Multiple user accounts already exists with this username. The database needs to be checked. Account ID: {account_id} Username: {username}')
return False
log.info('A user account already exists with this username. Updating instead of inserting.')
user_id = sql_select_result.get('id', None)
user_obj_data['id'] = user_id
if user_obj_up_result := sql_update(data=user_obj_data, table_name='user', rm_id_random=True): pass
log.info('A user account already exists with this username. Current User ID: {user_id} Username: {username}')
if allow_update:
log.info('Updating instead of inserting. Current User ID: {user_id} Username: {username}')
user_obj_data['id'] = user_id
if user_obj_up_result := sql_update(data=user_obj_data, table_name='user', rm_id_random=True):
log.info(f'User updated with new user data. User ID: {user_id}')
else:
log.warning(f'User not updated with new user data. User ID: {user_id}')
log.debug(user_obj_up_result)
return False
else:
return False
log.info('Updating is now allowed. Current User ID: {user_id} Username: {username}')
if avoid_dup_username:
log.info('Avoiding duplicate username is now allowed. Suggested Username: {username}')
new_username = username+'-'+str(random.randint(10, 99))
user_obj_data['username'] = new_username
if user_obj_in_result := sql_insert(data=user_obj_data, table_name='user', rm_id_random=True, id_random_length=8): pass
else:
log.warning(f'User not created.')
log.debug(user_obj_in_result)
return False
user_id = user_obj_in_result
else:
log.warning(f'Updating is not allowed and avoid duplicate username is not allowed. Username: {username}')
return False
#log.setLevel(logging.DEBUG)
log.debug(user_obj_up_result)
# log.debug(user_obj_up_result)
log.debug(f'Returning the existing user_id: {user_id}')
else:
if user_obj_in_result := sql_insert(data=user_obj_data, table_name='user', rm_id_random=True, id_random_length=8): pass
else:
log.warning(f'User not created.')
log.debug(user_obj_in_result)
return False
log.setLevel(logging.DEBUG)
log.debug(user_obj_in_result)
user_id = user_obj_in_result
log.debug(f'Returning the new user_id: {user_id}')
log.debug(f'Returning the new user_id: {user_id}')
return user_id
# ### END ### API User Methods ### create_user_obj() ###