54 lines
2.1 KiB
Python
54 lines
2.1 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 .contact_model import Contact_Base
|
|
from ..models.address_methods import create_address_obj
|
|
|
|
|
|
# ### 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() ###
|