196 lines
7.1 KiB
Python
196 lines
7.1 KiB
Python
import datetime
|
|
#from datetime import datetime, time, timedelta
|
|
from fastapi import APIRouter, Body, Depends, Header, HTTPException, Query, status
|
|
from pydantic import BaseModel, EmailStr, Field
|
|
from typing import Dict, List, Optional, Set, Union
|
|
|
|
from app.lib_general import *
|
|
from ..log import *
|
|
from app.config import settings
|
|
from app.db_sql import *
|
|
|
|
from .api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
|
|
|
|
from app.methods.product_methods import get_product_rec_list, load_product_obj
|
|
|
|
from app.models.product_models import Product_Base
|
|
from app.models.response_models import *
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post('/product', response_model=Resp_Body_Base)
|
|
async def post_product_obj(
|
|
obj: Product_Base,
|
|
x_account_id: str = Header(...),
|
|
return_obj: Optional[bool] = True,
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'product'
|
|
obj_data_dict = obj.dict(by_alias=False, exclude_unset=True)
|
|
result = post_obj_template(
|
|
obj_type=obj_type,
|
|
data=obj_data_dict,
|
|
return_obj=True,
|
|
by_alias=True,
|
|
exclude_unset=True,
|
|
)
|
|
return result
|
|
|
|
|
|
@router.patch('/product/{obj_id}', response_model=Resp_Body_Base)
|
|
async def patch_product_obj(
|
|
obj_id: str = Query(..., min_length=1, max_length=22),
|
|
obj: Product_Base = None,
|
|
x_account_id: Optional[str] = Header(..., ),
|
|
return_obj: Optional[bool] = True,
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'product'
|
|
obj_data_dict = obj.dict(by_alias=False, exclude_unset=True)
|
|
obj_data_dict['id'] = redis_lookup_id_random(record_id_random=obj_id, table_name=obj_type)
|
|
obj_data_dict['id_random'] = obj_id
|
|
result = patch_obj_template(
|
|
obj_type=obj_type,
|
|
data=obj_data_dict,
|
|
obj_id=obj_id,
|
|
return_obj=True,
|
|
by_alias=True,
|
|
exclude_unset=True,
|
|
)
|
|
return result
|
|
|
|
|
|
@router.get('/product/list', response_model=Resp_Body_Base)
|
|
async def get_product_obj_li(
|
|
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
|
for_obj_id: Optional[str] = Query(None, min_length=1, max_length=22),
|
|
prod_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
|
limit: int = 500, # For now this covers any included objects or object lists
|
|
enabled: str = 'enabled', # For now this covers any included objects or object lists
|
|
x_account_id: str = Header(...),
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type): pass
|
|
else: return mk_resp(data=None, status_code=404)
|
|
|
|
# Updated 2021-07-01
|
|
if product_rec_list_result := get_product_rec_list(
|
|
for_obj_type = for_obj_type,
|
|
for_obj_id = for_obj_id,
|
|
prod_type = prod_type,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
product_result_list = []
|
|
for product_rec in product_rec_list_result:
|
|
if load_product_result := load_product_obj(
|
|
product_id = product_rec.get('product_id', None),
|
|
by_alias = by_alias,
|
|
exclude_unset = exclude_unset,
|
|
# model_as_dict = model_as_dict,
|
|
):
|
|
product_result_list.append(load_product_result)
|
|
else:
|
|
product_result_list.append(None)
|
|
response_data = product_result_list
|
|
else:
|
|
return mk_resp(data=False, status_code=400) # Bad Request
|
|
|
|
return mk_resp(data=response_data)
|
|
|
|
|
|
@router.get('/account/{account_id}/product/list', response_model=Resp_Body_Base)
|
|
async def get_account_product_obj_li(
|
|
account_id: str = Query(..., min_length=1, max_length=22),
|
|
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
|
for_obj_id: Optional[str] = Query(None, min_length=1, max_length=22),
|
|
prod_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
|
limit: int = 500, # For now this covers any included objects or object lists
|
|
enabled: str = 'enabled', # For now this covers any included objects or object lists
|
|
x_account_id: str = Header(...),
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
):
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
|
|
else: return mk_resp(data=None, status_code=404)
|
|
if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type): pass
|
|
else: pass
|
|
|
|
# Updated 2021-07-01
|
|
if product_rec_list_result := get_product_rec_list(
|
|
account_id = account_id,
|
|
for_obj_type = for_obj_type,
|
|
for_obj_id = for_obj_id,
|
|
prod_type = prod_type,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
):
|
|
product_result_list = []
|
|
for product_rec in product_rec_list_result:
|
|
if load_product_result := load_product_obj(
|
|
product_id = product_rec.get('product_id', None),
|
|
by_alias = by_alias,
|
|
exclude_unset = exclude_unset,
|
|
# model_as_dict = model_as_dict,
|
|
):
|
|
product_result_list.append(load_product_result)
|
|
else:
|
|
product_result_list.append(None)
|
|
response_data = product_result_list
|
|
else:
|
|
return mk_resp(data=False, status_code=400) # Bad Request
|
|
|
|
return mk_resp(data=response_data)
|
|
|
|
|
|
@router.get('/product/{obj_id}', response_model=Resp_Body_Base)
|
|
async def get_product_obj(
|
|
obj_id: str = Query(..., min_length=1, max_length=22),
|
|
x_account_id: str = Header(...),
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'product'
|
|
result = get_obj_template(
|
|
obj_type=obj_type,
|
|
obj_id=obj_id,
|
|
by_alias=True,
|
|
exclude_unset=True,
|
|
)
|
|
return result
|
|
|
|
|
|
@router.delete('/product/{obj_id}', response_model=Resp_Body_Base)
|
|
async def delete_product_obj(
|
|
obj_id: str = Query(..., min_length=1, max_length=22),
|
|
x_account_id: str = Header(...),
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'product'
|
|
result = delete_obj_template(
|
|
obj_type=obj_type,
|
|
obj_id=obj_id,
|
|
)
|
|
return result |