Work on archives

This commit is contained in:
Scott Idem
2021-11-01 17:17:02 -04:00
parent f31626f51c
commit 82a6cb4525
5 changed files with 80 additions and 18 deletions

View File

@@ -86,7 +86,7 @@ app.include_router(
) )
app.include_router( app.include_router(
archive.router, archive.router,
prefix='/archive', # prefix='/archive',
tags=['Archive'], tags=['Archive'],
) )
app.include_router( app.include_router(

View File

@@ -51,6 +51,9 @@ def get_archive_content_rec_list(
for_obj_id: str, for_obj_id: str,
limit: int = 1000, limit: int = 1000,
enabled: str = 'enabled', # enabled, disabled, all enabled: str = 'enabled', # enabled, disabled, all
sort_by_str: str = None,
sort_by: str = None,
sort_by_desc: bool = False,
) -> list|bool: ) -> list|bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals()) log.debug(locals())
@@ -78,13 +81,20 @@ def get_archive_content_rec_list(
else: else:
sql_limit = '' sql_limit = ''
if sort_by:
sql_order_by = f'ORDER BY `tbl`.{sort_by}'
if sort_by_desc:
sql_order_by = f'{sql_order_by} {DESC}'
else:
sql_order_by = 'ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC'
sql = f""" sql = f"""
SELECT `tbl`.id AS 'archive_content_id', `tbl`.id_random AS 'archive_content_id_random' SELECT `tbl`.id AS 'archive_content_id', `tbl`.id_random AS 'archive_content_id_random'
FROM `archive_content` AS `tbl` FROM `archive_content` AS `tbl`
WHERE WHERE
{sql_obj_type_id} {sql_obj_type_id}
{sql_enabled} {sql_enabled}
ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC {sql_order_by}
{sql_limit}; {sql_limit};
""" """

View File

@@ -20,6 +20,7 @@ def load_archive_obj(
exclude_unset: bool = True, exclude_unset: bool = True,
model_as_dict: bool = False, model_as_dict: bool = False,
enabled: str = 'enabled', # enabled, disabled, all enabled: str = 'enabled', # enabled, disabled, all
hidden: str = 'not_hidden', # hidden, not_hidden, all
inc_archive_content_list: bool = False, inc_archive_content_list: bool = False,
) -> Archive_Base|dict|bool: ) -> Archive_Base|dict|bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
@@ -42,11 +43,15 @@ def load_archive_obj(
# Updated 2021-06-18 # Updated 2021-06-18
if inc_archive_content_list: if inc_archive_content_list:
sort_by = archive_rec.get('sort_by')
sort_by_desc = archive_rec.get('sort_by_desc')
if archive_content_rec_list_result := get_archive_content_rec_list( if archive_content_rec_list_result := get_archive_content_rec_list(
for_obj_type = 'archive', for_obj_type = 'archive',
for_obj_id = archive_id, for_obj_id = archive_id,
limit = limit, limit = limit,
enabled = enabled, enabled = enabled,
sort_by = sort_by,
sort_by_desc = sort_by_desc,
): ):
archive_content_result_list = [] archive_content_result_list = []
for archive_content_rec in archive_content_rec_list_result: for archive_content_rec in archive_content_rec_list_result:

View File

@@ -46,6 +46,9 @@ class Archive_Base(BaseModel):
meta_data: Optional[str] meta_data: Optional[str]
access_key: Optional[str] access_key: Optional[str]
sort_by: Optional[str]
sort_by_desc: Optional[bool]
enable: Optional[bool] enable: Optional[bool]
enable_from: Optional[datetime.datetime] = None enable_from: Optional[datetime.datetime] = None
enable_to: Optional[datetime.datetime] = None enable_to: Optional[datetime.datetime] = None

View File

@@ -11,6 +11,8 @@ 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 .api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
from app.methods.archive_methods import load_archive_obj
from app.models.archive_models import Archive_Base from app.models.archive_models import Archive_Base
from app.models.response_models import * from app.models.response_models import *
@@ -18,7 +20,7 @@ from app.models.response_models import *
router = APIRouter() router = APIRouter()
@router.post('', response_model=Resp_Body_Base) @router.post('/archive', response_model=Resp_Body_Base)
async def post_archive_obj( async def post_archive_obj(
obj: Archive_Base, obj: Archive_Base,
x_account_id: str = Header(...), x_account_id: str = Header(...),
@@ -42,7 +44,7 @@ async def post_archive_obj(
return result return result
@router.patch('/{obj_id}', response_model=Resp_Body_Base) @router.patch('/archive/{obj_id}', response_model=Resp_Body_Base)
async def patch_archive_obj( async def patch_archive_obj(
obj_id: str = Query(..., min_length=1, max_length=22), obj_id: str = Query(..., min_length=1, max_length=22),
obj: Archive_Base = None, obj: Archive_Base = None,
@@ -70,7 +72,7 @@ async def patch_archive_obj(
return result return result
@router.get('/list', response_model=Resp_Body_Base) @router.get('/archive/list', response_model=Resp_Body_Base)
async def get_archive_obj_li( async def get_archive_obj_li(
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50), 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), for_obj_id: Optional[str] = Query(None, min_length=1, max_length=22),
@@ -93,28 +95,70 @@ async def get_archive_obj_li(
return result return result
@router.get('/{obj_id}', response_model=Resp_Body_Base) # ### BEGIN ### API Archive ### get_archive_obj() ###
# Working well as of 2021-11-01. Using as a template for other routes.
@router.get('/archive/{archive_id}', response_model=Resp_Body_Base)
async def get_archive_obj( async def get_archive_obj(
obj_id: str = Query(..., min_length=1, max_length=22), archive_id: str = Query(..., min_length=11, max_length=22),
enabled: str = 'enabled', # enabled, disabled, all
hidden: str = 'not_hidden', # hidden, not_hidden, all
# inc_address: bool = False, # Under archive and under contact
# inc_contact: bool = False,
inc_archive_content_list: bool = False,
x_account_id: str = Header(...), x_account_id: str = Header(...),
limit: int = 500, # For now this covers any included objects or object lists
by_alias: Optional[bool] = True, by_alias: Optional[bool] = True,
exclude_unset: Optional[bool] = True, exclude_unset: Optional[bool] = True,
response: Response = Response, response: Response = Response,
): ):
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals()) log.debug(locals())
obj_type = 'archive' if archive_id := redis_lookup_id_random(record_id_random=archive_id, table_name='archive'): pass
result = get_obj_template( else: return mk_resp(data=None, status_code=404, response=response)
obj_type=obj_type,
obj_id=obj_id, if archive_obj := load_archive_obj(
by_alias=True, archive_id = archive_id,
exclude_unset=True, enabled = enabled,
) hidden = hidden,
return result # inc_address = inc_address,
# inc_contact = inc_contact,
inc_archive_content_list = inc_archive_content_list,
limit = limit,
by_alias = by_alias,
exclude_unset = exclude_unset,
# model_as_dict = model_as_dict,
):
pass
else:
return mk_resp(data=False, status_code=400, response=response) # Bad Request
return mk_resp(data=archive_obj, response=response)
# ### END ### API Archive ### get_archive_obj() ###
@router.delete('/{obj_id}', response_model=Resp_Body_Base) # @router.get('/archive/{obj_id}', response_model=Resp_Body_Base)
# async def get_archive_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 = 'archive'
# result = get_obj_template(
# obj_type=obj_type,
# obj_id=obj_id,
# by_alias=True,
# exclude_unset=True,
# )
# return result
@router.delete('/archive/{obj_id}', response_model=Resp_Body_Base)
async def delete_archive_obj( async def delete_archive_obj(
obj_id: str = Query(..., min_length=1, max_length=22), obj_id: str = Query(..., min_length=1, max_length=22),
x_account_id: str = Header(...), x_account_id: str = Header(...),
@@ -128,4 +172,4 @@ async def delete_archive_obj(
obj_type=obj_type, obj_type=obj_type,
obj_id=obj_id, obj_id=obj_id,
) )
return result return result