127 lines
5.0 KiB
Python
127 lines
5.0 KiB
Python
from __future__ import annotations
|
|
import datetime
|
|
|
|
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_select, sql_update
|
|
from app.lib_general import log, logging
|
|
|
|
from app.methods.event_registration_cfg_methods import load_event_registration_cfg_obj
|
|
|
|
from app.models.event_cfg_models import Event_Cfg_Base
|
|
|
|
|
|
# ### BEGIN ### API Event Cfg Methods ### load_event_cfg_obj() ###
|
|
def load_event_cfg_obj(
|
|
event_id: int|str,
|
|
inc_event_registration_cfg: bool = False,
|
|
limit: int = 1000,
|
|
by_alias: bool = True,
|
|
exclude_unset: bool = True,
|
|
model_as_dict: bool = False,
|
|
) -> Event_Cfg_Base|bool:
|
|
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if event_id := redis_lookup_id_random(record_id_random=event_id, table_name='event'): pass
|
|
else: return False
|
|
|
|
if event_cfg_rec := sql_select(table_name='v_event_cfg', record_id=event_id): pass
|
|
else: return False
|
|
|
|
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(event_cfg_rec)
|
|
|
|
try:
|
|
event_cfg_obj = Event_Cfg_Base(**event_cfg_rec)
|
|
log.debug(event_cfg_obj)
|
|
except ValidationError as e:
|
|
log.error(e.json())
|
|
return False
|
|
|
|
# Updated 2021-06-30
|
|
if inc_event_registration_cfg:
|
|
if event_registration_cfg_result := load_event_registration_cfg_obj(
|
|
event_id = event_id,
|
|
by_alias = by_alias,
|
|
exclude_unset = exclude_unset,
|
|
model_as_dict = model_as_dict,
|
|
):
|
|
event_cfg_obj.event_registration_cfg = event_registration_cfg_result
|
|
else: event_cfg_obj.event_registration_cfg = None
|
|
|
|
if model_as_dict:
|
|
return event_cfg_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member
|
|
else:
|
|
return event_cfg_obj
|
|
# ### END ### API Event Cfg Methods ### load_event_cfg_obj() ###
|
|
|
|
|
|
# ### BEGIN ### API Event Cfg Methods ### create_update_event_cfg_obj_v4() ###
|
|
# Updated 2021-09-28
|
|
def create_update_event_cfg_obj_v4(
|
|
event_cfg_dict_obj: Event_Base|dict,
|
|
event_cfg_id: int|str|None = None,
|
|
event_id: int|str|None = None,
|
|
create_sub_obj: bool = False, # Is this needed for a config?
|
|
fail_any: bool = False, # Fail if any thing goes wrong for sub objects
|
|
return_outline: bool = False,
|
|
) -> int|bool:
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
log.info('Checking requirements...')
|
|
if event_id := redis_lookup_id_random(record_id_random=event_id, table_name='event'): pass
|
|
else: return False
|
|
|
|
log.debug(type(event_cfg_dict_obj))
|
|
if isinstance(event_cfg_dict_obj, dict):
|
|
event_cfg_dict = event_cfg_dict_obj
|
|
if event_id:
|
|
event_cfg_dict['event_id'] = event_id
|
|
# if event_cfg_id:
|
|
# event_cfg_dict['event_cfg_id'] = event_cfg_id
|
|
try:
|
|
event_cfg_obj = Event_Cfg_Base(**event_cfg_dict)
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(event_cfg_obj)
|
|
except ValidationError as e:
|
|
log.error(e.json())
|
|
return False
|
|
else:
|
|
event_cfg_obj = event_cfg_dict_obj
|
|
if event_id:
|
|
# NOTE: Can't update the ID alias if it was never set.
|
|
event_cfg_obj.id = event_id
|
|
|
|
event_cfg_dict = event_cfg_obj.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'registration_cfg', 'created_on', 'updated_on'}) # NOTE this exclude list should be reviewed again
|
|
|
|
if event_id:
|
|
if event_cfg_dict_up_result := sql_update(data=event_cfg_dict, table_name='event', rm_id_random=True): pass # NOTE: This is not using the event_cfg table at this time. -STI 2021-09-28
|
|
else:
|
|
log.warning(f'Event (Cfg) not updated. Event ID: {event_id}')
|
|
log.debug(event_cfg_dict_up_result)
|
|
return False
|
|
log.debug(event_cfg_dict_up_result)
|
|
else: # NOTE NOTE NOTE NOTE: This section is for future use NOTE NOTE NOTE NOTE
|
|
if event_cfg_dict_in_result := sql_insert(data=event_cfg_dict, table_name='event', rm_id_random=True, id_random_length=default_num_bytes): pass # NOTE: This is not using the event_cfg table at this time. -STI 2021-09-28
|
|
else:
|
|
log.warning(f'Event (Cfg) not created.')
|
|
log.debug(event_cfg_dict_in_result)
|
|
return False
|
|
log.debug(event_cfg_dict_in_result)
|
|
|
|
event_id = event_cfg_dict_in_result
|
|
|
|
event_outline = {}
|
|
event_outline['event_id'] = event_id
|
|
|
|
if return_outline:
|
|
log.debug(f'Returning the Event Cfg Outline: {event_cfg_outline}')
|
|
return event_cfg_outline
|
|
else:
|
|
log.debug(f'Returning the Event (Cfg) ID: {event_id}')
|
|
return event_id
|
|
# ### END ### API Event Cfg Methods ### create_update_event_cfg_obj_v4() ###
|