- Moved 29 unreferenced legacy router files to app/routers/archive/ - Restored and registered 'websockets' router in registry.py (exempted from deprecation) - Cleaned up registry imports and verified application compilation.
480 lines
22 KiB
Python
480 lines
22 KiB
Python
import datetime
|
|
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Path, Query, Response, status
|
|
from pydantic import BaseModel, EmailStr, Field
|
|
from typing import Dict, List, Optional, Set, Union
|
|
|
|
from app.lib_general import *
|
|
from app.config import settings
|
|
from app.db_sql import *
|
|
|
|
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.order_methods import create_order_obj, update_order_obj, get_order_rec_list, load_order_obj, save_order_obj
|
|
from app.methods.order_line_methods import create_order_obj_line, update_order_obj_line, load_order_obj_line
|
|
|
|
from app.models.response_models import Resp_Body_Base, mk_resp
|
|
from app.models.order_models_v3 import Order_Base
|
|
from app.models.order_line_models_v3 import Order_Line_Base
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
# ### BEGIN ### API Order Routers ### post_order_obj() ###
|
|
# Updated 2022-01-18
|
|
@router.post('/v3/order', response_model=Resp_Body_Base)
|
|
@router.post('/v3/person/{person_id}/order', response_model=Resp_Body_Base)
|
|
async def post_order_obj(
|
|
order_obj: Order_Base,
|
|
person_id: str = Path(min_length=11, max_length=22),
|
|
|
|
inc_address: bool = False,
|
|
inc_contact: bool = False,
|
|
inc_order_line_list: bool = True,
|
|
inc_person: bool = False,
|
|
return_obj: bool = True,
|
|
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# ### SECTION ### Secondary data validation
|
|
if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
|
|
# elif person_id is None: pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The person ID was invalid or not found.')
|
|
|
|
# ### SECTION ### Process data
|
|
if order_id := create_order_obj(
|
|
account_id = commons.x_account_id,
|
|
person_id = person_id,
|
|
order_dict_obj = order_obj,
|
|
): pass
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something failed while processing the data.') # Bad Request
|
|
|
|
# ### SECTION ### Return successful results
|
|
if return_obj:
|
|
if load_order_obj_result := load_order_obj(
|
|
order_id = order_id,
|
|
inc_address = inc_address,
|
|
inc_contact = inc_contact,
|
|
inc_order_line_list = inc_order_line_list,
|
|
inc_person = inc_person,
|
|
).dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset):
|
|
log.info('Loading successful. Returning result')
|
|
log.debug(load_order_obj_result)
|
|
return mk_resp(data=load_order_obj_result, response=commons.response)
|
|
elif isinstance(load_order_obj_result, list) or load_order_obj_result is None: # Empty list or None
|
|
return mk_resp(data=None, status_code=404, response=commons.response) # Not Found
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
|
|
else:
|
|
order_id_random = get_id_random(record_id=order_id, table_name='order')
|
|
data = {}
|
|
data['order_id'] = order_id
|
|
data['order_id_random'] = order_id_random
|
|
return mk_resp(data=data, response=commons.response)
|
|
# ### END ### API Order Routers ### post_order_obj() ###
|
|
|
|
|
|
# ### BEGIN ### API Order Routers ### patch_order_obj() ###
|
|
# Updated 2022-01-18
|
|
@router.patch('/v3/order/{order_id}', response_model=Resp_Body_Base)
|
|
# @router.patch('/v3/person/{person_id}/order/{order_id}', response_model=Resp_Body_Base)
|
|
async def patch_order_obj(
|
|
order_obj: Order_Base,
|
|
order_id: str = Path(min_length=11, max_length=22),
|
|
# person_id: str = Query(None, min_length=11, max_length=22),
|
|
|
|
inc_address: bool = False,
|
|
inc_contact: bool = False,
|
|
inc_order_line_list: bool = True,
|
|
inc_person: bool = False,
|
|
return_obj: Optional[bool] = True,
|
|
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# ### SECTION ### Secondary data validation
|
|
order_id_random = order_id # This is used later for the response data
|
|
# person_id_random = person_id # This is used later for the response data
|
|
|
|
if order_id := redis_lookup_id_random(record_id_random=order_id, table_name='order'): pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The order ID was invalid or not found.')
|
|
|
|
# if person_id := redis_lookup_id_random(record_id_random=person_id, table_name='person'): pass
|
|
# elif person_id is None: pass
|
|
# else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The person ID was invalid or not found.')
|
|
|
|
# ### SECTION ### Process data
|
|
if update_order_obj_result := update_order_obj(
|
|
order_id = order_id,
|
|
order_dict_obj = order_obj,
|
|
# person_id = person_id,
|
|
): pass
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something failed while processing the data.') # Bad Request
|
|
|
|
# ### SECTION ### Return successful results
|
|
if return_obj:
|
|
if load_order_obj_result := load_order_obj(
|
|
order_id = order_id,
|
|
inc_address = inc_address,
|
|
inc_contact = inc_contact,
|
|
inc_order_line_list = inc_order_line_list,
|
|
inc_person = inc_person,
|
|
).dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset):
|
|
log.info('Loading successful. Returning result')
|
|
log.debug(load_order_obj_result)
|
|
return mk_resp(data=load_order_obj_result, response=commons.response)
|
|
elif isinstance(load_order_obj_result, list) or load_order_obj_result is None: # Empty list or None
|
|
return mk_resp(data=None, status_code=404, response=commons.response) # Not Found
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
|
|
else:
|
|
data = {}
|
|
data['order_id'] = order_id
|
|
data['order_id_random'] = order_id_random
|
|
return mk_resp(data=data, response=commons.response)
|
|
# ### END ### API Order Routers ### patch_order_obj() ###
|
|
|
|
|
|
# ### BEGIN ### API Order Routers ### patch_order_obj_add_line() ###
|
|
# Updated 2022-01-18
|
|
@router.patch('/v3/order/{order_id}/line/add', response_model=Resp_Body_Base)
|
|
async def patch_order_obj_add_line(
|
|
order_line_obj: Order_Line_Base,
|
|
order_id: str = Path(min_length=11, max_length=22),
|
|
|
|
# inc_order: bool = False,
|
|
inc_order_line_list: bool = True,
|
|
return_obj: Optional[bool] = True,
|
|
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# ### SECTION ### Secondary data validation
|
|
order_id_random = order_id # This is used later for the response data
|
|
|
|
if order_id := redis_lookup_id_random(record_id_random=order_id, table_name='order'): pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The order ID was invalid or not found.')
|
|
|
|
# ### SECTION ### Process data
|
|
if order_line_id := add_order_obj_line(
|
|
order_id = order_id,
|
|
order_line_dict_obj = order_line_obj,
|
|
): pass
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something failed while processing the data.') # Bad Request
|
|
|
|
# ### SECTION ### Return successful results
|
|
if return_obj:
|
|
if load_order_obj_result := load_order_obj(
|
|
order_id = order_id,
|
|
# inc_address = inc_address,
|
|
# inc_contact = inc_contact,
|
|
inc_order_line_list = inc_order_line_list,
|
|
# inc_person = inc_person,
|
|
).dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset):
|
|
log.info('Loading successful. Returning result')
|
|
log.debug(load_order_obj_result)
|
|
return mk_resp(data=load_order_obj_result, response=commons.response)
|
|
elif isinstance(load_order_obj_result, list) or load_order_obj_result is None: # Empty list or None
|
|
return mk_resp(data=None, status_code=404, response=commons.response) # Not Found
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
|
|
else:
|
|
order_line_id = order_line_add_result
|
|
order_line_id_random = get_id_random(record_id=order_line_id, table_name='order_line')
|
|
data = {}
|
|
data['order_id'] = order_id
|
|
data['order_id_random'] = order_id_random
|
|
data['order_line_id'] = order_line_id
|
|
data['order_line_id_random'] = order_line_id_random
|
|
return mk_resp(data=data, response=commons.response)
|
|
# ### END ### API Order Routers ### patch_order_obj_add_line() ###
|
|
|
|
|
|
# ### BEGIN ### API Order Routers ### patch_order_obj_update_line() ###
|
|
# Updated 2022-01-18
|
|
@router.patch('/v3/order/{order_id}/line/{order_line_id}/update', response_model=Resp_Body_Base)
|
|
async def patch_order_obj_update_line(
|
|
order_obj: Order_Line_Base,
|
|
order_id: str = Path(min_length=11, max_length=22),
|
|
order_line_id: str = Path(min_length=11, max_length=22),
|
|
|
|
# inc_order: bool = False,
|
|
inc_order_line_list: bool = True,
|
|
return_obj: Optional[bool] = True,
|
|
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# ### SECTION ### Secondary data validation
|
|
order_id_random = order_id # This is used later for the response data
|
|
order_line_id_random = order_line_id # This is used later for the response data
|
|
|
|
if order_id := redis_lookup_id_random(record_id_random=order_id, table_name='order'): pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The order ID was invalid or not found.')
|
|
|
|
if order_line_id := redis_lookup_id_random(record_id_random=order_line_id, table_name='order_line'): pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The order line ID was invalid or not found.')
|
|
|
|
# ### SECTION ### Process data
|
|
if update_order_obj_line_result := update_order_obj_line(
|
|
order_line_id = order_line_id,
|
|
order_line_dict_obj = order_line_obj,
|
|
): pass
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something failed while processing the data.') # Bad Request
|
|
|
|
# ### SECTION ### Return successful results
|
|
if return_obj:
|
|
if load_order_obj_result := load_order_obj(
|
|
order_id = order_id,
|
|
# inc_address = inc_address,
|
|
# inc_contact = inc_contact,
|
|
inc_order_line_list = inc_order_line_list,
|
|
# inc_person = inc_person,
|
|
).dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset):
|
|
log.info('Loading successful. Returning result')
|
|
log.debug(order_dict)
|
|
return mk_resp(data=order_dict, response=commons.response)
|
|
elif isinstance(load_order_obj_result, list) or load_order_obj_result is None: # Empty list or None
|
|
return mk_resp(data=None, status_code=404, response=commons.response) # Not Found
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
|
|
else:
|
|
data = {}
|
|
data['order_id'] = order_id
|
|
data['order_id_random'] = order_id_random
|
|
data['order_line_id'] = order_line_id
|
|
data['order_line_id_random'] = order_line_id_random
|
|
return mk_resp(data=data, response=commons.response)
|
|
# ### END ### API Order Routers ### patch_order_obj_update_line() ###
|
|
|
|
|
|
# ### BEGIN ### API Order Routers ### patch_order_obj_remove_line() ###
|
|
# Updated 2022-01-18
|
|
@router.patch('/v3/order/{order_id}/line/{order_line_id}/remove', response_model=Resp_Body_Base)
|
|
async def patch_order_obj_remove_line(
|
|
order_obj: Order_Line_Base,
|
|
order_id: str = Path(min_length=11, max_length=22),
|
|
order_line_id: str = Path(min_length=11, max_length=22),
|
|
|
|
# inc_order: bool = False,
|
|
inc_order_line_list: bool = True,
|
|
return_obj: Optional[bool] = True,
|
|
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
# ### SECTION ### Secondary data validation
|
|
order_id_random = order_id # This is used later for the response data
|
|
order_line_id_random = order_line_id # This is used later for the response data
|
|
|
|
if order_id := redis_lookup_id_random(record_id_random=order_id, table_name='order'): pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The order ID was invalid or not found.')
|
|
|
|
if order_line_id := redis_lookup_id_random(record_id_random=order_line_id, table_name='order_line'): pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The order line ID was invalid or not found.')
|
|
|
|
# ### SECTION ### Process data
|
|
if remove_order_obj_line_result := remove_order_obj_line(
|
|
order_line_id = order_line_id,
|
|
): pass
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=commons.response, status_message='Something failed while processing the data.') # Bad Request
|
|
|
|
# ### SECTION ### Return successful results
|
|
if return_obj:
|
|
if load_order_obj_result := load_order_obj(
|
|
order_id = order_id,
|
|
# inc_address = inc_address,
|
|
# inc_contact = inc_contact,
|
|
inc_order_line_list = inc_order_line_list,
|
|
# inc_person = inc_person,
|
|
).dict(by_alias=commons.by_alias, exclude_unset=commons.exclude_unset):
|
|
log.info('Loading successful. Returning result')
|
|
log.debug(order_dict)
|
|
return mk_resp(data=order_dict, response=commons.response)
|
|
elif isinstance(load_order_obj_result, list) or load_order_obj_result is None: # Empty list or None
|
|
return mk_resp(data=None, status_code=404, response=commons.response) # Not Found
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
|
|
else:
|
|
data = {}
|
|
data['order_id'] = order_id
|
|
data['order_id_random'] = order_id_random
|
|
data['order_line_id'] = order_line_id
|
|
data['order_line_id_random'] = order_line_id_random
|
|
return mk_resp(data=data, response=commons.response)
|
|
# ### END ### API Order Routers ### patch_order_obj_remove_line() ###
|
|
|
|
|
|
# ### BEGIN ### API Order Routers ### get_order_obj_li() ###
|
|
# Updated 2022-01-18
|
|
@router.get('/v3/{for_obj_type}/{for_obj_id}/order/list', response_model=Resp_Body_Base)
|
|
async def get_order_obj_li(
|
|
for_obj_type: str = Path(min_length=2, max_length=50),
|
|
for_obj_id: str = Path(min_length=11, max_length=22),
|
|
order_status: str = 'complete',
|
|
order_checkout_status: str = 'complete',
|
|
from_datetime: datetime.datetime = None,
|
|
to_datetime: datetime.datetime = None,
|
|
|
|
inc_address: bool = False,
|
|
inc_contact: bool = False,
|
|
inc_order_cfg: bool = False,
|
|
inc_order_line_list: bool = False,
|
|
inc_person: bool = False,
|
|
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if obj_type in ['account', 'person']: pass
|
|
else: return mk_resp(data=False, status_code=400, response=response, status_message='The object type passed was invalid or not found. Expecting "account" or "person".') # Bad Request
|
|
|
|
if obj_type_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type): pass
|
|
else: return mk_resp(data=False, status_code=404, response=commons.response) # Not Found
|
|
|
|
if get_order_rec_list_result := get_order_rec_list(
|
|
for_obj_type = for_obj_type,
|
|
for_obj_id = for_obj_id,
|
|
from_datetime = from_datetime,
|
|
to_datetime = to_datetime,
|
|
status = order_status,
|
|
# checkout_status = order_checkout_status,
|
|
enabled = commons.enabled,
|
|
limit = commons.limit,
|
|
offset = commons.offset,
|
|
):
|
|
order_obj_list = []
|
|
for order_rec in get_order_rec_list_result:
|
|
if load_order_obj_result := load_order_obj(
|
|
order_id = order_rec.get('order_id'),
|
|
inc_address = inc_address,
|
|
inc_contact = inc_contact,
|
|
inc_order_cfg = inc_order_cfg,
|
|
inc_order_line_list = inc_order_line_list,
|
|
inc_person = inc_person,
|
|
enabled = commons.enabled,
|
|
limit = commons.limit,
|
|
by_alias = commons.by_alias,
|
|
exclude_unset = commons.exclude_unset,
|
|
# model_as_dict = model_as_dict,
|
|
):
|
|
log.debug(load_order_obj_result)
|
|
order_obj_list.append(load_order_obj_result)
|
|
else:
|
|
order_obj_list.append(None)
|
|
log.info('Loading successful. Returning result')
|
|
log.debug(order_obj_list)
|
|
return mk_resp(data=order_obj_list, response=commons.response)
|
|
elif isinstance(get_order_rec_list_result, list) or get_order_rec_list_result is None: # Empty list or None
|
|
return mk_resp(data=None, status_code=404, response=commons.response) # Not Found
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
|
|
# ### END ### API Order Routers ### get_order_obj_li() ###
|
|
|
|
|
|
# ### BEGIN ### API Order Routes ### get_order_obj() ###
|
|
# NOTE 2021-08-09: Use with rework of order_cart
|
|
# Updated 2022-12-18
|
|
@router.get('/v3/order/{order_id}', response_model=Resp_Body_Base)
|
|
async def get_order_obj(
|
|
order_id: str = Path(min_length=11, max_length=22),
|
|
|
|
inc_address: bool = False,
|
|
inc_contact: bool = False,
|
|
inc_order_cfg: bool = False,
|
|
inc_order_line_list: bool = False,
|
|
inc_person: bool = False,
|
|
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if order_id := redis_lookup_id_random(record_id_random=order_id, table_name='order'): pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The order ID was invalid or not found.')
|
|
|
|
if load_order_obj_result := load_order_obj(
|
|
order_id = order_id,
|
|
inc_address = inc_address,
|
|
inc_contact = inc_contact,
|
|
inc_order_cfg = inc_order_cfg,
|
|
inc_order_line_list = inc_order_line_list,
|
|
inc_person = inc_person,
|
|
limit = commons.limit,
|
|
enabled = commons.enabled,
|
|
by_alias = commons.by_alias,
|
|
exclude_unset = commons.exclude_unset,
|
|
# model_as_dict = model_as_dict,
|
|
):
|
|
log.debug(load_order_obj_result)
|
|
order_dict = load_order_obj_result.dict(by_alias=commons.by_alias, exclude_unset=False) # NOTE NOTE NOTE NOTE exclude_unset is forced to False for now. Will return more fields than is ideal. Need to create another Order_Line_Base. Probably Order_Line_OUT_Base
|
|
log.info('Loading successful. Returning result')
|
|
return mk_resp(data=order_dict, response=commons.response)
|
|
elif isinstance(load_order_obj_result, list) or load_order_obj_result is None: # Empty list or None
|
|
return mk_resp(data=None, status_code=404, response=commons.response) # Not Found
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
|
|
# ### END ### API Order Routes ### get_order_obj() ###
|
|
|
|
|
|
# ### BEGIN ### API Order ### get_person_id_order_cart() ###
|
|
# NOTE 2021-08-09: Use with rework of order_cart. The most recent (hopefully only one) "open" order for a person.
|
|
# Updated 2022-12-18
|
|
@router.get('/v3/person/{person_id}/order/cart', response_model=Resp_Body_Base)
|
|
async def get_person_id_order_cart(
|
|
person_id: str = Path(min_length=11, max_length=22),
|
|
enabled: str = 'enabled',
|
|
inc_order_line_list: bool = False,
|
|
inc_order_cfg: bool = False,
|
|
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
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 mk_resp(data=None, status_code=404, response=commons.response, status_message='The person ID was invalid or not found.')
|
|
|
|
# Query to get the one "open" order status for a person ID
|
|
|
|
return False
|
|
# ### END ### API Order ### get_person_id_order_cart() ###
|
|
|
|
|
|
# ### BEGIN ### API Order Routers ### delete_order_obj() ###
|
|
# Updated 2022-01-18
|
|
@router.delete('/v3/order/{order_id}', response_model=Resp_Body_Base)
|
|
async def delete_order_obj(
|
|
order_id: str = Path(min_length=11, max_length=22),
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if order_id := redis_lookup_id_random(record_id_random=order_id, table_name='order'): pass
|
|
else: return mk_resp(data=None, status_code=404, response=commons.response, status_message='The order ID was invalid or not found.')
|
|
|
|
obj_type = 'order'
|
|
result = delete_obj_template(
|
|
obj_type = obj_type,
|
|
obj_id = obj_id,
|
|
)
|
|
return result
|
|
# ### END ### API Order Routers ### delete_order_obj() ### |