114 lines
4.9 KiB
Python
114 lines
4.9 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 ..lib_general import *
|
|
from ..db_sql import redis_lookup_id_random, sql_insert, sql_select, sql_update
|
|
|
|
from .person_model import Person_Base
|
|
from ..models.address_methods import load_address_obj
|
|
from ..models.contact_methods import create_contact_obj
|
|
|
|
|
|
# ### 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_contact:bool=False, inc_address:bool=False, inc_organization:bool=False, inc_user:bool=False) -> Person_Base:
|
|
log.setLevel(logging.WARNING) # 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):
|
|
#log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(person_rec)
|
|
|
|
if inc_contact:
|
|
if contact_rec := sql_select(table_name='v_contact', field_name='contact_id', field_value=person_rec.get('contact_id', None)):
|
|
person_rec['contact'] = contact_rec
|
|
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(contact_rec)
|
|
|
|
if inc_address:
|
|
address_id = contact_rec.get('address_id', None)
|
|
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)
|
|
# if address_rec := sql_select(table_name='v_address', field_name='address_id', field_value=contact_rec.get('address_id', None)):
|
|
# person_rec['contact']['address'] = address_rec
|
|
# #log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
# log.debug(address_rec)
|
|
|
|
if inc_organization:
|
|
if organization_rec := sql_select(table_name='v_organization', field_name='organization_id', field_value=person_rec.get('organization_id', None)):
|
|
person_rec['organization'] = organization_rec
|
|
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(organization_rec)
|
|
|
|
if inc_user:
|
|
if user_rec := sql_select(table_name='v_user', field_name='user_id', field_value=person_rec.get('user_id', None)):
|
|
person_rec['user'] = user_rec
|
|
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(user_rec)
|
|
|
|
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(person_rec)
|
|
else:
|
|
return False
|
|
|
|
try:
|
|
person_obj = Person_Base(**person_rec)
|
|
log.debug(person_obj)
|
|
except ValidationError as e:
|
|
log.error(e.json())
|
|
return False
|
|
|
|
return person_obj
|
|
# ### END ### API Person Methods ### load_person_obj() ###
|