103 lines
4.6 KiB
Python
103 lines
4.6 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 app.db_sql import redis_lookup_id_random, sql_insert, sql_select
|
|
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.models.organization_models import Organization_Base
|
|
|
|
|
|
# ### BEGIN ### API Organization Methods ### load_organization_obj() ###
|
|
# NOTE: This needs to be updated to the newer method template. Like address, contact, or person -STI 2021-06-10
|
|
def load_organization_obj(organization_id:int|str, inc_contact:bool=False, inc_address:bool=False) -> Organization_Base|bool:
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if organization_id := redis_lookup_id_random(record_id_random=organization_id, table_name='organization'): pass
|
|
else: return False
|
|
|
|
if organization_rec := sql_select(table_name='v_organization', record_id=organization_id):
|
|
#log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(organization_rec)
|
|
|
|
if inc_contact:
|
|
contact_id = organization_rec.get('contact_id', None)
|
|
if contact_obj_result := load_contact_obj(contact_id=contact_id):
|
|
contact_obj = contact_obj_result
|
|
organization_rec['contact'] = contact_obj
|
|
log.debug(organization_rec)
|
|
#else: organization_rec['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
|
|
organization_rec['contact'].address = address_obj
|
|
log.debug(organization_rec)
|
|
#else: organization_rec['contact'].address = None
|
|
else:
|
|
return False
|
|
|
|
try:
|
|
organization_obj = Organization_Base(**organization_rec)
|
|
log.debug(organization_obj)
|
|
except ValidationError as e:
|
|
log.error(e.json())
|
|
return False
|
|
|
|
return organization_obj
|
|
# ### END ### API Organization Methods ### load_organization_obj() ###
|
|
|
|
|
|
# ### BEGIN ### API Organization Methods ### update_organization_obj() ###
|
|
def update_organization_obj(
|
|
organization_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value.
|
|
organization_obj_up: Organization_Base,
|
|
create_missing_obj: bool = False,
|
|
) -> bool:
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if organization_obj_up.contact_id and organization_obj_up.contact:
|
|
contact_id = organization_obj_up.contact_id
|
|
contact_obj_up = organization_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 organization_obj_up.contact and not organization_obj_up.contact.id:
|
|
# NOTE: This will blindly create a new contact even if there was one associated but the organization.contact_id was not found.
|
|
contact_obj_in = organization_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)
|
|
organization_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
|
|
|
|
organization_dict_up = organization_obj_up.dict(by_alias=False, exclude_unset=True, exclude={'contact', 'person', 'user'})
|
|
log.debug(organization_dict_up)
|
|
|
|
if organization_obj_up_result := sql_update(data=organization_dict_up, table_name='organization', rm_id_random=True):
|
|
log.debug(organization_obj_up_result)
|
|
return True
|
|
else:
|
|
log.debug(organization_obj_up_result)
|
|
return False
|
|
# ### END ### API Organization Methods ### update_organization_obj() ### |