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 ..lib_general 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 ..models.response_model import * from ..models.site_model import Site_Base from ..models.site_domain_model import Site_Domain_Base router = APIRouter() @router.post('', response_model=Resp_Body_Base) async def post_site_domain_obj( obj: Site_Domain_Base, x_account_id: str = Header(...), return_obj: Optional[bool] = True, by_alias: Optional[bool] = True, exclude_unset: Optional[bool] = True, exclude_none: Optional[bool] = True, ): log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) obj_type = 'site_domain' 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('/{obj_id}', response_model=Resp_Body_Base) async def patch_site_domain_obj( obj_id: str = Query(..., min_length=1, max_length=22), obj: Site_Domain_Base = None, x_account_id: Optional[str] = Header(..., ), return_obj: Optional[bool] = True, by_alias: Optional[bool] = True, exclude_unset: Optional[bool] = True, exclude_none: Optional[bool] = True, ): log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) obj_type = 'site_domain' 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('/lookup_fqdn', response_model=Resp_Body_Base) async def lookup_site_domain_obj( fqdn: str, access_key: Optional[str] = None, referrer: Optional[str] = None, x_account_id: str = Header(...), by_alias: Optional[bool] = True, include: Optional[list] = [], exclude: Optional[list] = [], exclude_unset: Optional[bool] = True, exclude_none: Optional[bool] = True, ): log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) data = {} data['fqdn'] = fqdn data['domain_access_key'] = access_key data['required_referrer'] = referrer if access_key: #sql_domain_access_key = 'AND site_domain.domain_access_key = :domain_access_key' sql_domain_access_key = """ AND (site_domain.domain_access_key = :domain_access_key AND (site_domain.required_referrer IS NULL OR site_domain.required_referrer = '')) OR (site_domain.domain_access_key = :domain_access_key AND site_domain.required_referrer = :required_referrer) """ else: sql_domain_access_key = 'AND site_domain.domain_access_key IS NULL' #if referrer: # sql_referrer = 'AND site_domain.required_referrer = :required_referrer' sql = f""" SELECT *, site_enable_from AS 'enable_from', site_enable_to AS 'enable_to' FROM `v_site_domain` AS site_domain WHERE site_domain.fqdn = :fqdn {sql_domain_access_key} ; """ log.debug(sql) if site_domain_obj_result := sql_select(data=data, sql=sql): try: site_obj = Site_Base(**site_domain_obj_result).dict(by_alias=by_alias, exclude_unset=exclude_unset, exclude_none=exclude_none) log.debug(site_obj) except ValidationError as e: log.error(e.json()) try: site_domain_obj = Site_Domain_Base(**site_domain_obj_result).dict(by_alias=by_alias, exclude_unset=exclude_unset, exclude_none=exclude_none) log.debug(site_domain_obj) except ValidationError as e: log.error(e.json()) site_domain_obj['site'] = site_obj log.debug(site_domain_obj) return mk_resp(data=site_domain_obj) else: log.debug(site_domain_obj_result) return mk_resp(data=False, status_code=404) @router.get('/list', response_model=Resp_Body_Base) async def get_site_domain_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, ): log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) obj_type = 'site_domain' 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('/{obj_id}', response_model=Resp_Body_Base) async def get_site_domain_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, exclude_none: Optional[bool] = True, ): log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.debug(locals()) obj_type = 'site_domain' result = get_obj_template( obj_type=obj_type, obj_id=obj_id, by_alias=True, exclude_unset=True, ) return result @router.delete('/{obj_id}', response_model=Resp_Body_Base) async def delete_site_domain_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 = 'site_domain' result = delete_obj_template( obj_type=obj_type, obj_id=obj_id, ) return result