Working on event_person and session proposals
This commit is contained in:
@@ -5,12 +5,66 @@ from typing import Dict, List, Optional, Set, Union
|
||||
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
|
||||
|
||||
from ..lib_general import *
|
||||
from ..db_sql import redis_lookup_id_random, sql_select
|
||||
from ..db_sql import redis_lookup_id_random, sql_insert, sql_select, sql_update
|
||||
|
||||
from .user_model import User_Base, User_Out_Base, User_New_Base
|
||||
from .user_role_model import User_Role_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API User Methods ### create_user_obj() ###
|
||||
def create_user_obj(user_obj_new:User_Base):
|
||||
log.setLevel(logging.WARNING) # 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'})
|
||||
# log.debug(user_obj_data)
|
||||
|
||||
user_obj_data = user_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'new_password'})
|
||||
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...')
|
||||
sql_select_user = f"""
|
||||
SELECT user.id, user.id_random, user.name, user.email
|
||||
FROM `user` AS user
|
||||
WHERE user.account_id = :account_id and user.username = :username
|
||||
"""
|
||||
|
||||
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.")
|
||||
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
|
||||
else:
|
||||
return False
|
||||
|
||||
log.setLevel(logging.DEBUG)
|
||||
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:
|
||||
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}')
|
||||
return user_id
|
||||
# ### END ### API User Methods ### create_user_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API User Methods ### load_user_obj() ###
|
||||
def load_user_obj(user_id:int|str, inc_roles:bool=False, inc_contact:bool=False, inc_organization:bool=False, inc_person:bool=False) -> User_Base:
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
|
||||
Reference in New Issue
Block a user