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, update_address_obj from app.models.contact_models import Contact_Base # ### 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 ### load_contact_obj() ### def load_contact_obj( contact_id:int|str, limit: int = 1000, 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_dict := load_address_obj( address_id = address_id, model_as_dict = model_as_dict, ): contact_obj.address = address_dict else: contact_obj.address = None if model_as_dict: return contact_obj.dict(by_alias=True, exclude_unset=True) # pylint: disable=no-member else: return contact_obj # ### END ### API Contact Methods ### load_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() ###