Working on contact create update v4
This commit is contained in:
@@ -5,11 +5,11 @@ from typing import Dict, List, Optional, Set, Union
|
||||
|
||||
from app.lib_general import log, logging
|
||||
from app.config import settings
|
||||
from app.db_sql import sql_insert, sql_update, sql_insert_or_update, sql_select, sql_delete, redis_lookup_id_random
|
||||
from app.db_sql import get_id_random, sql_insert, sql_update, sql_insert_or_update, sql_select, sql_delete, redis_lookup_id_random
|
||||
|
||||
from app.routers.api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
|
||||
|
||||
from app.methods.contact_methods import load_contact_obj, update_contact_obj
|
||||
from app.methods.contact_methods import create_update_contact_obj_v4, load_contact_obj, update_contact_obj
|
||||
|
||||
from app.models.contact_models import Contact_Base
|
||||
from app.models.response_models import *
|
||||
@@ -70,6 +70,110 @@ async def patch_contact_obj(
|
||||
return result
|
||||
|
||||
|
||||
# ### BEGIN ### API Contact ### post_contact_obj_new_v4() ###
|
||||
# Updated 2021-08-25
|
||||
@router.post('/new_v4', response_model=Resp_Body_Base)
|
||||
async def post_contact_obj_new_v4(
|
||||
contact_obj: Contact_Base,
|
||||
for_type: str = None,
|
||||
for_id: str = None,
|
||||
create_sub_obj: bool = False,
|
||||
fail_any: bool = True, # Fail if any thing goes wrong for sub objects
|
||||
x_account_id: str = Header(...),
|
||||
return_obj: bool = True,
|
||||
inc_address: bool = False,
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
response: Response = Response,
|
||||
):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
# There should probably be a check for the account ID before calling the create update function?
|
||||
if create_update_contact_obj_result := create_update_contact_obj_v4(
|
||||
contact_dict_obj = contact_obj,
|
||||
contact_id = None,
|
||||
account_id = x_account_id,
|
||||
for_type = for_type,
|
||||
for_id = for_id,
|
||||
create_sub_obj = create_sub_obj,
|
||||
fail_any = fail_any,
|
||||
return_outline = False,
|
||||
): pass
|
||||
else: return mk_resp(data=False, status_code=400, response=response, status_message='The contact was not created. Check the field names and data types.')
|
||||
|
||||
if isinstance(create_update_contact_obj_result, int):
|
||||
contact_id = create_update_contact_obj_result
|
||||
if return_obj:
|
||||
if load_contact_obj_result := load_contact_obj(contact_id=contact_id, inc_address=inc_address):
|
||||
data = load_contact_obj_result
|
||||
else:
|
||||
data = False
|
||||
else:
|
||||
contact_id = create_update_contact_obj_result
|
||||
contact_id_random = get_id_random(record_id=contact_id, table_name='contact')
|
||||
data = {}
|
||||
data['contact_id'] = contact_id
|
||||
data['contact_id_random'] = contact_id_random
|
||||
return mk_resp(data=data, response=response, status_message='The contact was created.')
|
||||
else:
|
||||
return mk_resp(data=False, status_code=400, response=response, status_message='The result from trying to create an contact was unexpected.')
|
||||
# ### BEGIN ### API Contact ### post_contact_obj_new_v4() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Contact ### patch_contact_obj_exist_v4() ###
|
||||
# Updated 2021-08-25
|
||||
@router.patch('/{contact_id}/exist_v4', response_model=Resp_Body_Base)
|
||||
async def patch_contact_obj_exist_v4(
|
||||
contact_obj: Contact_Base,
|
||||
contact_id: str = Query(..., min_length=11, max_length=22),
|
||||
for_type: str = None,
|
||||
for_id: str = None,
|
||||
create_sub_obj: bool = False,
|
||||
fail_any: bool = True, # Fail if any thing goes wrong for sub objects
|
||||
x_account_id: Optional[str] = Header(..., ),
|
||||
return_obj: Optional[bool] = True,
|
||||
inc_address: bool = False,
|
||||
by_alias: Optional[bool] = True,
|
||||
exclude_unset: Optional[bool] = True,
|
||||
exclude_none: Optional[bool] = True,
|
||||
response: Response = Response,
|
||||
):
|
||||
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 mk_resp(data=None, status_code=404)
|
||||
|
||||
if update_contact_obj_result := create_update_contact_obj_v4(
|
||||
contact_dict_obj = contact_obj,
|
||||
contact_id = contact_obj.contact_id,
|
||||
account_id = x_account_id,
|
||||
for_type = for_type,
|
||||
for_id = for_id,
|
||||
create_sub_obj = create_sub_obj,
|
||||
fail_any = fail_any,
|
||||
return_outline = False,
|
||||
): pass
|
||||
else: return mk_resp(data=False, status_code=400, response=response, status_message='The contact was not created. Check the field names and data types.')
|
||||
|
||||
if update_contact_obj_result:
|
||||
if return_obj:
|
||||
if load_contact_obj_result := load_contact_obj(contact_id=contact_id, inc_address=inc_address):
|
||||
data = load_contact_obj_result
|
||||
else:
|
||||
data = False
|
||||
else:
|
||||
contact_id_random = get_id_random(record_id=contact_id, table_name='contact')
|
||||
data = {}
|
||||
data['contact_id'] = contact_id
|
||||
data['contact_id_random'] = contact_id_random
|
||||
return mk_resp(data=data, response=response, status_message='The contact was created.')
|
||||
else:
|
||||
return mk_resp(data=False, status_code=400, response=response, status_message='The result from trying to create an contact was unexpected.')
|
||||
# ### END ### API Contact ### patch_contact_obj_exist_v4() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Contact ### patch_contact_json() ###
|
||||
@router.patch('/{contact_id}/json', response_model=Resp_Body_Base)
|
||||
async def patch_contact_json(
|
||||
|
||||
Reference in New Issue
Block a user