Code clean up. Working on returning proper 404 vs 400 responses if the results are empty and nothing went wrong.
This commit is contained in:
@@ -5,7 +5,7 @@ from typing import Dict, List, Optional, Set, Union
|
||||
from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, validator
|
||||
|
||||
from app.db_sql import redis_lookup_id_random, sql_insert, sql_select, sql_update
|
||||
from app.lib_general import log, logging
|
||||
from app.lib_general import log, logging, logger_reset
|
||||
|
||||
from app.methods.person_methods import load_person_obj
|
||||
# from app.methods.post_comment_methods import create_post_comment_obj, update_post_comment_obj
|
||||
@@ -16,6 +16,7 @@ from app.models.post_models import Post_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Post Methods ### create_post_obj() ###
|
||||
@logger_reset
|
||||
def create_post_obj(post_obj_new:Post_Base):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
@@ -39,6 +40,7 @@ def create_post_obj(post_obj_new:Post_Base):
|
||||
|
||||
|
||||
# ### BEGIN ### API Post Methods ### load_post_obj() ###
|
||||
@logger_reset
|
||||
def load_post_obj(
|
||||
post_id: int|str,
|
||||
limit: int = 1000,
|
||||
@@ -56,7 +58,7 @@ def load_post_obj(
|
||||
if post_id := redis_lookup_id_random(record_id_random=post_id, table_name='post'): pass
|
||||
else: return False
|
||||
|
||||
if post_rec := sql_select(table_name='v_post_detail', record_id=post_id):
|
||||
if post_rec := sql_select(table_name='v_post', record_id=post_id):
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(post_rec)
|
||||
else: return False
|
||||
@@ -132,6 +134,7 @@ def load_post_obj(
|
||||
|
||||
|
||||
# ### BEGIN ### API Post Methods ### update_post_obj() ###
|
||||
@logger_reset
|
||||
def update_post_obj(
|
||||
post_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value.
|
||||
post_obj_up: Post_Base,
|
||||
@@ -195,6 +198,8 @@ def update_post_obj(
|
||||
|
||||
|
||||
# ### BEGIN ### API Post Methods ### get_post_rec_list() ###
|
||||
# Updated 2021-12-13
|
||||
@logger_reset
|
||||
def get_post_rec_list(
|
||||
for_obj_type: str,
|
||||
for_obj_id: str,
|
||||
@@ -202,7 +207,7 @@ def get_post_rec_list(
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
archive_on: datetime.datetime = None,
|
||||
) -> list|bool:
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.INFO) # 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
|
||||
@@ -210,21 +215,21 @@ def get_post_rec_list(
|
||||
data = {}
|
||||
data[f'{for_obj_type}_id'] = for_obj_id
|
||||
# data['for_obj_type'] = for_obj_type
|
||||
sql_obj_type_id = f'`tbl`.{for_obj_type}_id = :{for_obj_type}_id'
|
||||
sql_obj_type_id = f'`post`.{for_obj_type}_id = :{for_obj_type}_id'
|
||||
|
||||
if enabled in ['enabled', 'disabled', 'all']:
|
||||
if enabled == 'enabled':
|
||||
data['enable'] = True
|
||||
sql_enabled = f'AND `tbl`.enable = :enable'
|
||||
sql_enabled = f'AND `post`.enable = :enable'
|
||||
elif enabled == 'disabled':
|
||||
data['enable'] = False
|
||||
sql_enabled = f'AND `tbl`.enable = :enable'
|
||||
sql_enabled = f'AND `post`.enable = :enable'
|
||||
elif enabled == 'all':
|
||||
sql_enabled = ''
|
||||
|
||||
if archive_on:
|
||||
data['archive_on'] = archive_on
|
||||
sql_archive_on = 'AND tbl.archive_on >= :archive_on'
|
||||
sql_archive_on = 'AND post.archive_on >= :archive_on'
|
||||
else:
|
||||
sql_archive_on = ''
|
||||
|
||||
@@ -235,21 +240,24 @@ def get_post_rec_list(
|
||||
sql_limit = ''
|
||||
|
||||
sql = f"""
|
||||
SELECT `tbl`.id AS 'post_id', `tbl`.id_random AS 'post_id_random'
|
||||
FROM `post` AS `tbl`
|
||||
SELECT `post`.id AS 'post_id', `post`.id_random AS 'post_id_random'
|
||||
FROM `post` AS `post`
|
||||
WHERE
|
||||
{sql_obj_type_id}
|
||||
{sql_enabled}
|
||||
{sql_archive_on}
|
||||
ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC
|
||||
ORDER BY `post`.created_on DESC, `post`.updated_on DESC
|
||||
{sql_limit};
|
||||
"""
|
||||
log.debug(sql)
|
||||
|
||||
if post_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
|
||||
log.info('Got a list result')
|
||||
post_rec_li = post_rec_li_result
|
||||
else:
|
||||
post_rec_li = []
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
else: # [] or False
|
||||
log.info('No results or something went wrong')
|
||||
post_rec_li = post_rec_li_result
|
||||
|
||||
log.debug(post_rec_li_result)
|
||||
|
||||
return post_rec_li
|
||||
|
||||
Reference in New Issue
Block a user