General clean up
This commit is contained in:
@@ -11,7 +11,7 @@ from .order_cart_model import Order_Cart_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Order Cart Methods ### load_order_cart_obj() ###
|
||||
def load_order_cart_obj(order_cart_id:int|str, inc_order_cart_line_li:bool=False, inc_order_cart_cfg:bool=False):
|
||||
def load_order_cart_obj(order_cart_id:int|str, inc_order_cart_line_li:bool=False, inc_order_cart_cfg:bool=False) -> Order_Cart_Base:
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
@@ -41,6 +41,7 @@ def load_order_cart_obj(order_cart_id:int|str, inc_order_cart_line_li:bool=False
|
||||
log.debug(order_cart_obj)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
|
||||
return order_cart_obj
|
||||
# ### END ### API Order Cart Methods ### load_order_cart_obj() ###
|
||||
|
||||
@@ -86,7 +86,7 @@ class Order_Cart_Line_Base(BaseModel):
|
||||
log.setLevel(logging.WARNING)
|
||||
log.debug(locals())
|
||||
|
||||
if values['product_id_random']:
|
||||
if values.get('product_id_random', None):
|
||||
return redis_lookup_id_random(record_id_random=values['product_id_random'], table_name='product')
|
||||
return None
|
||||
|
||||
|
||||
@@ -24,7 +24,18 @@ class Resp_Body_Base(BaseModel):
|
||||
|
||||
# ### BEGIN ### API Response Model ### mk_resp() ###
|
||||
# The make response function for REST - STI 2021-03-17
|
||||
def mk_resp(data:None|bool|dict|list, dict_to_json:bool=False, status_code:int=200, status_message:str='', status_name:str='', success:bool=True, details:str='', by_alias:bool=True, exclude_unset:bool=True):
|
||||
def mk_resp(
|
||||
data:None|bool|dict|list,
|
||||
dict_to_json:bool=False,
|
||||
status_code:int=200,
|
||||
status_message:str='',
|
||||
status_name:str='',
|
||||
success:bool=True,
|
||||
details:str='',
|
||||
by_alias:bool=True,
|
||||
exclude_unset:bool=True,
|
||||
response=None
|
||||
):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
@@ -53,6 +64,18 @@ def mk_resp(data:None|bool|dict|list, dict_to_json:bool=False, status_code:int=2
|
||||
resp_body['meta']['data_type'] = 'list'
|
||||
resp_body['meta']['data_list_count'] = len(data)
|
||||
|
||||
if response:
|
||||
if status_code == 400: response.status_code = status.HTTP_400_BAD_REQUEST
|
||||
elif status_code == 401: response.status_code = status.HTTP_401_UNAUTHORIZED
|
||||
elif status_code == 403: response.status_code = status.HTTP_403_FORBIDDEN
|
||||
elif status_code == 404: response.status_code = status.HTTP_404_NOT_FOUND
|
||||
elif status_code == 408: response.status_code = status.HTTP_408_REQUEST_TIMEOUT
|
||||
elif status_code == 429: response.status_code = status.HTTP_429_TOO_MANY_REQUESTS
|
||||
elif status_code == 500: response.status_code = status.HTTP_500_INTERNAL_SERVER_ERROR
|
||||
elif status_code == 502: response.status_code = status.HTTP_502_BAD_GATEWAY
|
||||
elif status_code == 503: response.status_code = status.HTTP_503_SERVICE_UNAVAILABLE
|
||||
elif status_code == 504: response.status_code = status.HTTP_504_GATEWAY_TIMEOUT
|
||||
|
||||
log.debug(type(resp_body['data']))
|
||||
|
||||
resp_body = Resp_Body_Base(**resp_body).dict(by_alias=by_alias, exclude_unset=exclude_unset)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import datetime
|
||||
#from datetime import datetime, time, timedelta
|
||||
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, status
|
||||
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, Response, status
|
||||
from pydantic import BaseModel, EmailStr, Field
|
||||
from typing import Dict, List, Optional, Set, Union
|
||||
|
||||
@@ -94,6 +94,7 @@ async def get_order_cart_obj_li(
|
||||
# Look up is only for account, order, person, or user records
|
||||
@router.get('/lookup', response_model=Resp_Body_Base)
|
||||
async def lookup_order_cart_obj(
|
||||
response: Response,
|
||||
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
||||
for_obj_id: Optional[Union[int,str]] = None,
|
||||
inc_order_cart_line_li: Optional[bool] = True,
|
||||
@@ -166,27 +167,26 @@ async def lookup_order_cart_obj(
|
||||
return mk_resp(data=data)
|
||||
else:
|
||||
log.debug(order_cart_obj_result)
|
||||
return mk_resp(data=False, status_code=404) # Not Found
|
||||
return mk_resp(data=False, status_code=404, response=response) # Not Found
|
||||
|
||||
|
||||
@router.get('/{obj_id}', response_model=Resp_Body_Base)
|
||||
@router.get('/{order_cart_id}', response_model=Resp_Body_Base)
|
||||
async def get_order_cart_obj(
|
||||
obj_id: str = Query(..., min_length=1, max_length=22),
|
||||
order_cart_id: str = Query(..., min_length=1, max_length=22),
|
||||
inc_order_cart_line_li: bool = False,
|
||||
inc_order_cart_cfg: bool = False,
|
||||
x_account_id: str = Header(...),
|
||||
by_alias: Optional[bool] = True,
|
||||
exclude_unset: Optional[bool] = True,
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
obj_type = 'order_cart'
|
||||
result = get_obj_template(
|
||||
obj_type=obj_type,
|
||||
obj_id=obj_id,
|
||||
by_alias=True,
|
||||
exclude_unset=True,
|
||||
)
|
||||
return result
|
||||
if order_cart_obj := load_order_cart_obj(order_cart_id=order_cart_id, inc_order_cart_line_li=inc_order_cart_line_li, inc_order_cart_cfg=inc_order_cart_cfg):
|
||||
data = order_cart_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset)
|
||||
return mk_resp(data=data)
|
||||
else:
|
||||
return mk_resp(data=False, status_code=404) # Not Found
|
||||
|
||||
|
||||
@router.delete('/{obj_id}', response_model=Resp_Body_Base)
|
||||
|
||||
Reference in New Issue
Block a user