194 lines
6.6 KiB
Python
194 lines
6.6 KiB
Python
import datetime
|
|
#from datetime import datetime, time, timedelta
|
|
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
|
|
|
|
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.post_methods import get_post_rec_list, load_post_obj
|
|
|
|
from app.models.post_models import Post_Base
|
|
from app.models.response_models import *
|
|
|
|
|
|
router = APIRouter()
|
|
|
|
|
|
@router.post('/post', response_model=Resp_Body_Base)
|
|
async def post_post_obj(
|
|
obj: Post_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 = 'post'
|
|
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('/post/{obj_id}', response_model=Resp_Body_Base)
|
|
async def patch_post_obj(
|
|
obj_id: str = Query(..., min_length=1, max_length=22),
|
|
obj: Post_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 = 'post'
|
|
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('/post/list', response_model=Resp_Body_Base)
|
|
async def get_post_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 = 'post'
|
|
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
|
|
|
|
|
|
# ### BEGIN ### API Post ### get_account_obj_post_list() ###
|
|
# Working well as of 2021-06-28. Using as a template for other routes.
|
|
@router.get('/account/{account_id}/post/list', response_model=Resp_Body_Base)
|
|
async def get_account_obj_post_list(
|
|
account_id: str = Query(..., min_length=1, max_length=22),
|
|
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
|
|
archive_on: datetime.datetime = None,
|
|
inc_account_cfg: bool = False,
|
|
inc_address: bool = False, # Under contact
|
|
inc_contact: bool = False,
|
|
inc_person: bool = False,
|
|
inc_post_comment_list: bool = False,
|
|
inc_user: bool = False,
|
|
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())
|
|
|
|
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_data = None
|
|
|
|
# Updated 2021-06-28
|
|
if post_rec_list_result := get_post_rec_list(
|
|
for_obj_type = 'account',
|
|
for_obj_id = account_id,
|
|
limit = limit,
|
|
enabled = enabled,
|
|
archive_on = archive_on,
|
|
):
|
|
post_result_list = []
|
|
for post_rec in post_rec_list_result:
|
|
if load_post_result := load_post_obj(
|
|
post_id = post_rec.get('post_id', None),
|
|
limit = limit,
|
|
by_alias = by_alias,
|
|
exclude_unset = exclude_unset,
|
|
# model_as_dict = model_as_dict,
|
|
enabled = enabled,
|
|
# inc_address = inc_address,
|
|
# inc_contact = inc_contact,
|
|
inc_person = inc_person,
|
|
inc_post_comment_list = inc_post_comment_list,
|
|
inc_user = inc_user,
|
|
):
|
|
post_result_list.append(load_post_result)
|
|
else:
|
|
post_result_list.append(None)
|
|
response_data = post_result_list
|
|
else:
|
|
return mk_resp(data=False, status_code=400, response=response) # Bad Request
|
|
|
|
return mk_resp(data=response_data)
|
|
# ### END ### API Post ### get_account_obj_post_list() ###
|
|
|
|
|
|
@router.get('/post/{obj_id}', response_model=Resp_Body_Base)
|
|
async def get_post_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,
|
|
response: Response = Response,
|
|
):
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
obj_type = 'post'
|
|
result = get_obj_template(
|
|
obj_type=obj_type,
|
|
obj_id=obj_id,
|
|
by_alias=True,
|
|
exclude_unset=True,
|
|
)
|
|
return result
|
|
|
|
|
|
@router.delete('/post/{obj_id}', response_model=Resp_Body_Base)
|
|
async def delete_post_obj(
|
|
obj_id: str = Query(..., min_length=1, 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 = 'post'
|
|
result = delete_obj_template(
|
|
obj_type=obj_type,
|
|
obj_id=obj_id,
|
|
)
|
|
return result |