182 lines
6.4 KiB
Python
182 lines
6.4 KiB
Python
import datetime, pytz, secrets, time
|
|
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 log, logging, common_route_params, Common_Route_Params
|
|
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.routers.api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
|
|
|
|
from app.methods.site_methods import get_site_rec_list, load_site_obj
|
|
|
|
from app.models.common_field_schema import default_num_bytes
|
|
from app.models.response_models import Resp_Body_Base, mk_resp
|
|
from app.models.site_models import Site_Base
|
|
from app.models.site_domain_models import Site_Domain_Base
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post('/site', response_model=Resp_Body_Base)
|
|
async def post_site_obj(
|
|
obj: Site_Base,
|
|
x_account_id: str = Header(...),
|
|
return_obj: Optional[bool] = True,
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'site'
|
|
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('/site/{obj_id}', response_model=Resp_Body_Base)
|
|
async def patch_site_obj(
|
|
obj_id: str = Path(min_length=11, max_length=22),
|
|
obj: Site_Base = None,
|
|
x_account_id: Optional[str] = Header(..., ),
|
|
return_obj: Optional[bool] = True,
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'site'
|
|
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('/site/list', response_model=Resp_Body_Base)
|
|
async def get_site_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),
|
|
x_account_id: str = Header(...),
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'site'
|
|
result = get_obj_li_template(
|
|
obj_type=obj_type,
|
|
for_obj_type=for_obj_type,
|
|
for_obj_id=for_obj_id,
|
|
by_alias=True,
|
|
exclude_unset=True,
|
|
)
|
|
return result
|
|
|
|
|
|
@router.get('/site/{obj_id}', response_model=Resp_Body_Base)
|
|
async def get_site_obj(
|
|
obj_id: str = Path(min_length=11, max_length=22),
|
|
x_account_id: str = Header(...),
|
|
by_alias: Optional[bool] = True,
|
|
exclude_unset: Optional[bool] = True,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'site'
|
|
result = get_obj_template(
|
|
obj_type=obj_type,
|
|
obj_id=obj_id,
|
|
by_alias=True,
|
|
exclude_unset=True,
|
|
)
|
|
return result
|
|
|
|
|
|
@router.delete('/site/{obj_id}', response_model=Resp_Body_Base)
|
|
async def delete_site_obj(
|
|
obj_id: str = Path(min_length=11, max_length=22),
|
|
x_account_id: str = Header(...),
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'site'
|
|
result = delete_obj_template(
|
|
obj_type=obj_type,
|
|
obj_id=obj_id,
|
|
)
|
|
return result
|
|
|
|
|
|
# ### BEGIN ### API Site ### get_account_site_obj_li() ###
|
|
# Updated 2021-12-13
|
|
@router.get('/account/{account_id}/site/list', response_model=Resp_Body_Base)
|
|
async def get_account_site_obj_li(
|
|
account_id: str = Path(min_length=11, max_length=22),
|
|
|
|
inc_site_domain_list: bool = False,
|
|
|
|
commons: Common_Route_Params = Depends(common_route_params),
|
|
):
|
|
log.setLevel(logging.INFO) # 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, response=commons.response)
|
|
|
|
# Updated 2021-12-13
|
|
if site_rec_list_result := get_site_rec_list(
|
|
account_id = account_id,
|
|
# hidden = 'all', # hidden, not_hidden, all
|
|
enabled = commons.enabled,
|
|
limit = commons.limit,
|
|
offset = commons.offset,
|
|
):
|
|
site_result_list = []
|
|
for site_rec in site_rec_list_result:
|
|
if load_site_result := load_site_obj(
|
|
site_id = site_rec.get('site_id', None),
|
|
enabled = commons.enabled,
|
|
# hidden = hidden,
|
|
limit = commons.limit,
|
|
offset = commons.offset,
|
|
inc_site_domain_list = inc_site_domain_list,
|
|
):
|
|
site_result_list.append(load_site_result)
|
|
else:
|
|
site_result_list.append(None)
|
|
response_data = site_result_list
|
|
elif isinstance(site_rec_list_result, list) or site_rec_list_result is None: # Empty list or None
|
|
log.info('No results')
|
|
return mk_resp(data=False, status_code=404, response=commons.response) # Not Found
|
|
else:
|
|
log.warning('Likely bad request')
|
|
return mk_resp(data=False, status_code=400, response=commons.response) # Bad Request
|
|
|
|
return mk_resp(data=response_data, response=commons.response)
|
|
# ### END ### API Site ### get_account_site_obj_li() ### |