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 load_address_obj from app.methods.contact_methods import create_contact_obj, load_contact_obj, update_contact_obj from app.methods.organization_methods import load_organization_obj, update_organization_obj from app.methods.user_methods import create_user_obj, load_user_obj, update_user_obj from app.models.person_models import Person_Base # ### BEGIN ### API Person Methods ### create_person_obj() ### def create_person_obj(person_obj_new:Person_Base): log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if not person_obj_new: return False person_obj_data = person_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'contact', 'organization', 'created_on', 'updated_on'}) if person_obj_in_result := sql_insert(data=person_obj_data, table_name='person', rm_id_random=True, id_random_length=8): pass else: return False #log.setLevel(logging.DEBUG) log.debug(person_obj_in_result) person_id = person_obj_in_result if person_obj_new.contact: contact_obj_new = person_obj_new.contact contact_obj_new.for_type = 'person' contact_obj_new.for_id = person_id create_contact_obj_result = create_contact_obj(contact_obj_new=contact_obj_new) if isinstance(create_contact_obj_result, int): contact_id = create_contact_obj_result log.debug(f'Update person with new contact_id: {contact_id}') # Need to update the person with the new contact_id person_obj_up = {} person_obj_up['id'] = person_id person_obj_up['contact_id'] = contact_id if person_obj_up_result := sql_update(data=person_obj_up, table_name='person'): pass else: return False log.debug(person_obj_up_result) else: log.debug(f'No contact_id was returned when tyring to create_contact_obj(): {create_contact_obj_result}') contact_id = None log.debug(f'Returning the new person_id: {person_id}') return person_id # ### END ### API Person Methods ### create_person_obj() ### # ### BEGIN ### API Person Methods ### load_person_obj() ### def load_person_obj( person_id: int|str, inc_address: bool = False, # Under contact inc_contact: bool = False, inc_organization: bool = False, inc_user: bool = False ) -> Person_Base|bool: log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass else: return False if person_rec := sql_select(table_name='v_person', record_id=person_id): pass else: return False #log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(person_rec) try: person_obj = Person_Base(**person_rec) log.debug(person_obj) except ValidationError as e: log.error(e.json()) if inc_contact: contact_id = person_rec.get('contact_id', None) if contact_obj_result := load_contact_obj(contact_id=contact_id): contact_obj = contact_obj_result #person_obj.contact = contact_obj.dict(by_alias=True, exclude_unset=True) person_obj.contact = contact_obj else: person_obj.contact = None if inc_address: address_id = contact_obj.address_id if address_obj_result := load_address_obj(address_id=address_id): address_obj = address_obj_result # person_rec['contact'].address = address_obj # log.debug(person_rec) #person_obj.contact.address = address_obj.dict(by_alias=True, exclude_unset=True) person_obj.contact.address = address_obj else: person_obj.contact.address = None if inc_organization: organization_id = person_rec.get('organization_id', None) if organization_obj_result := load_organization_obj(organization_id=organization_id): organization_obj = organization_obj_result # person_rec['organization'] = organization_obj # log.debug(person_rec) #person_obj.organization = organization_obj.dict(by_alias=True, exclude_unset=True) person_obj.organization = organization_obj else: person_obj.organization = None if inc_user: user_id = person_rec.get('user_id', None) if user_obj_result := load_user_obj(user_id=user_id): user_obj = user_obj_result # person_rec['user'] = user_obj # log.debug(person_rec) #person_obj.user = user_obj.dict(by_alias=True, exclude_unset=True) person_obj.user = user_obj else: person_obj.user = None return person_obj # ### END ### API Person Methods ### load_person_obj() ### # ### BEGIN ### API Person Methods ### update_person_obj() ### def update_person_obj( person_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value. person_obj_up: Person_Base, create_missing_obj: bool = False, ) -> bool: log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass else: return False person_obj_up.id = person_id log.debug(person_obj_up) # log.debug(person_obj_up.dict(by_alias=True, exclude_unset=True)) log.debug(person_obj_up.dict(by_alias=False, exclude_unset=True)) # log.debug(person_obj_up.dict(by_alias=False, exclude_unset=False)) if person_obj_up.contact_id and person_obj_up.contact: contact_id = person_obj_up.contact_id contact_obj_up = person_obj_up.contact log.debug(contact_id) log.debug(contact_obj_up) if contact_obj_up_result := update_contact_obj( contact_id=contact_id, contact_obj_up=contact_obj_up, create_missing_obj=create_missing_obj, ): log.debug(contact_obj_up_result) else: log.debug(contact_obj_up_result) return False elif person_obj_up.contact and not person_obj_up.contact.id: # NOTE: This will blindly create a new contact even if there was one associated but the person.contact_id was not found. contact_obj_in = person_obj_up.contact log.debug(contact_obj_in) if contact_obj_in_result := create_contact_obj(contact_obj_new=contact_obj_in): # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(contact_obj_in_result) person_obj_up.contact_id = contact_obj_in_result else: # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(contact_obj_in_result) return False if person_obj_up.organization_id and person_obj_up.organization: organization_id = person_obj_up.organization_id organization_obj_up = person_obj_up.organization log.debug(organization_id) log.debug(organization_obj_up) if organization_obj_up_result := update_organization_obj( organization_id=organization_id, organization_obj_up=organization_obj_up, create_missing_obj=create_missing_obj, ): log.debug(organization_obj_up_result) else: log.debug(organization_obj_up_result) return False elif person_obj_up.organization and not person_obj_up.organization.id: # NOTE: This will blindly create a new organization even if there was one associated but the person.organization_id was not found. organization_obj_in = person_obj_up.organization log.debug(organization_obj_in) if organization_obj_in_result := create_organization_obj(organization_obj_new=organization_obj_in): # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(organization_obj_in_result) person_obj_up.organization_id = organization_obj_in_result else: # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(organization_obj_in_result) return False if person_obj_up.user_id and person_obj_up.user: log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL user_id = person_obj_up.user_id user_obj_up = person_obj_up.user log.debug(user_id) log.debug(user_obj_up) if user_obj_up_result := update_user_obj( user_id=user_id, user_obj_up=user_obj_up, create_missing_obj=create_missing_obj, ): log.debug(user_obj_up_result) else: log.debug(user_obj_up_result) return False elif person_obj_up.user and not person_obj_up.user.id: # NOTE: This will blindly create a new user even if there was one associated but the person.user_id was not found. user_obj_in = person_obj_up.user log.debug(user_obj_in) if user_obj_in_result := create_user_obj(user_obj_new=user_obj_in): # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(user_obj_in_result) person_obj_up.user_id = user_obj_in_result else: # log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(user_obj_in_result) return False person_dict_up = person_obj_up.dict(by_alias=False, exclude_unset=True, exclude={'contact', 'organization', 'user'}) log.debug(person_dict_up) if person_obj_up_result := sql_update(data=person_dict_up, table_name='person', rm_id_random=True): log.debug(person_obj_up_result) return True else: log.debug(person_obj_up_result) return False # ### END ### API Person Methods ### update_person_obj() ###