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.address_methods import create_address_obj, create_update_address_obj, update_address_obj from app.models.common_field_schema import default_num_bytes from app.models.contact_models import Contact_Base # ### BEGIN ### API Contact Methods ### load_contact_obj() ### def load_contact_obj( contact_id: int|str, limit: int = 1000, by_alias: bool = True, exclude_unset: bool = True, model_as_dict: bool = False, enabled: str = 'enabled', # enabled, disabled, all inc_address:bool=False ) -> Contact_Base|bool: # log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass else: return False if contact_rec := sql_select(table_name='v_contact', record_id=contact_id): pass else: return False try: contact_obj = Contact_Base(**contact_rec) log.debug(contact_obj) except ValidationError as e: log.error(e.json()) return False if inc_address: #log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL address_id = contact_rec.get('address_id', None) log.debug(address_id) from app.methods.address_methods import load_address_obj if address_result := load_address_obj( address_id = address_id, by_alias = by_alias, exclude_unset = exclude_unset, model_as_dict = model_as_dict, ): contact_obj.address = address_result else: contact_obj.address = None if model_as_dict: return contact_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member else: return contact_obj # ### END ### API Contact Methods ### load_contact_obj() ### # ### BEGIN ### API Contact Methods ### get_contact_rec_list() ### def get_contact_rec_list( for_obj_type: str, for_obj_id: str, limit: int = 1000, enabled: str = 'enabled', # enabled, disabled, all ) -> list|bool: # log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type): pass else: return False data = {} data[f'{for_obj_type}_id'] = for_obj_id # data['for_obj_type'] = for_obj_type sql_obj_type_id = f'`tbl`.{for_obj_type}_id = :{for_obj_type}_id' # if enabled in ['enabled', 'disabled', 'all']: # if enabled == 'enabled': # data['enable'] = True # sql_enabled = f'AND `tbl`.enable = :enable' # elif enabled == 'disabled': # data['enable'] = False # sql_enabled = f'AND `tbl`.enable = :enable' # elif enabled == 'all': # sql_enabled = '' sql_enabled = '' if limit: data['limit'] = limit sql_limit = f'LIMIT :limit' else: sql_limit = '' sql = f""" SELECT `tbl`.id AS 'contact_id', `tbl`.id_random AS 'contact_id_random' FROM `contact` AS `tbl` WHERE {sql_obj_type_id} {sql_enabled} ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC {sql_limit}; """ if contact_rec_li_result := sql_select(data=data, sql=sql, as_list=True): contact_rec_li = contact_rec_li_result else: contact_rec_li = [] # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(contact_rec_li_result) return contact_rec_li # ### END ### API Contact Methods ### get_contact_rec_list() ### # ### BEGIN ### API Contact Methods ### create_contact_obj() ### def create_contact_obj(contact_obj_new:Contact_Base): log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if not contact_obj_new: return False contact_obj_data = contact_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'address', 'created_on', 'updated_on'}) if contact_obj_in_result := sql_insert(data=contact_obj_data, table_name='contact', rm_id_random=True, id_random_length=8): pass else: return False log.debug(contact_obj_in_result) contact_id = contact_obj_in_result if contact_obj_new.address: address_obj_new = contact_obj_new.address address_obj_new.for_type = 'contact' address_obj_new.for_id = contact_id create_address_obj_result = create_address_obj(address_obj_new=address_obj_new) if isinstance(create_address_obj_result, int): address_id = create_address_obj_result # Need to update the contact with the new address_id contact_obj_up = {} contact_obj_up['id'] = contact_id contact_obj_up['address_id'] = address_id if contact_obj_up_result := sql_update(data=contact_obj_up, table_name='contact'): pass else: return False log.debug(contact_obj_up_result) else: log.debug(f'No address_id was returned when tyring to create_address_obj(): {create_address_obj_result}') address_id = None log.debug(f'Returning the new contact_id: {contact_id}') return contact_id # ### END ### API Contact Methods ### create_contact_obj() ### # ### BEGIN ### API Contact Methods ### update_contact_obj() ### def update_contact_obj( contact_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value. contact_obj_up: Contact_Base, create_missing_obj: bool = False, ) -> bool: log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass else: return False contact_obj_up.id = contact_id log.debug(contact_obj_up) # log.debug(contact_obj_up.dict(by_alias=True, exclude_unset=True)) log.debug(contact_obj_up.dict(by_alias=False, exclude_unset=True)) # log.debug(contact_obj_up.dict(by_alias=False, exclude_unset=False)) #contact_dict_up = contact_obj_up.dict(by_alias=False, exclude_unset=True) if contact_obj_up.address_id and contact_obj_up.address: address_id = contact_obj_up.address_id address_obj_up = contact_obj_up.address log.debug(address_id) log.debug(address_obj_up) if address_obj_up_result := update_address_obj( address_id=address_id, address_obj_up=address_obj_up, create_missing_obj=create_missing_obj, ): log.debug(address_obj_up_result) else: log.debug(address_obj_up_result) return False elif contact_obj_up.address and not contact_obj_up.address.id: # NOTE: This will blindly create a new address even if there was one associated but the contact.address_id was not found. address_obj_in = contact_obj_up.address log.debug(address_obj_in) if address_obj_in_result := create_address_obj(address_obj_new=address_obj_in): # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(address_obj_in_result) contact_obj_up.address_id = address_obj_in_result else: # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(address_obj_in_result) return False contact_dict_up = contact_obj_up.dict(by_alias=False, exclude_unset=True, exclude={'address'}) log.debug(contact_dict_up) if contact_obj_up_result := sql_update(data=contact_dict_up, table_name='contact', rm_id_random=True): log.debug(contact_obj_up_result) return True else: log.debug(contact_obj_up_result) return False # ### END ### API Contact Methods ### update_contact_obj() ### # ### BEGIN ### API Contact Methods ### create_update_contact_obj() ### def create_update_contact_obj( contact_id: int|str|None, # Ideally the int ID should be passed. This allows for updating of the id_random value. contact_obj: Contact_Base, process_address: bool = False, process_organization: bool = False, ) -> bool: log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if contact_id: if contact_id := redis_lookup_id_random(record_id_random=contact_id, table_name='contact'): pass else: return False contact_obj.id = contact_id else: # Insert record now and update later contact_dict_in = contact_obj.dict(by_alias=False, exclude_unset=True, exclude={'address', 'contact', 'organization', 'user'}) log.debug(contact_dict_in) contact_in_result = sql_insert( data = contact_dict_in, table_name = 'contact', rm_id_random = True, id_random_length = default_num_bytes, ) log.debug(contact_in_result) if isinstance(contact_in_result, bool) and contact_in_result is True: return contact_in_result elif isinstance(contact_in_result, int): contact_id = contact_in_result contact_obj.id = contact_id else: return False # This should not happen. # Process address data if process_address and contact_obj.address: address_obj = contact_obj.address address_obj.for_type = 'contact' address_obj.for_id = contact_id address_id = contact_obj.address_id_random address_result = create_update_address_obj( address_id = address_id, address_obj = address_obj, ) log.debug(address_result) if isinstance(address_result, bool) and address_result is True: pass # Do not need to update contact object. elif isinstance(address_result, bool) and address_result is False: pass # Do not need to update contact object. elif isinstance(address_result, int): contact_obj.address_id = address_result else: log.warning('Something may have gone wrong while trying to create or update a address.') # Process contact data contact_dict_up = contact_obj.dict(by_alias=False, exclude_unset=True, exclude={'address'}) log.debug(contact_dict_up) # Update record contact_up_result = sql_update( data = contact_dict_up, table_name = 'contact', rm_id_random = True, ) log.debug(contact_up_result) if isinstance(contact_up_result, bool) and contact_up_result is True: return contact_id elif isinstance(contact_up_result, bool) and contact_up_result is False: return False elif isinstance(contact_up_result, int): return contact_up_result else: return False # ### END ### API Contact Methods ### create_update_contact_obj() ###