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.user_methods import load_user_obj
|
||||
@@ -14,8 +14,9 @@ from app.models.post_comment_models import Post_Comment_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Post Comment Methods ### create_post_comment_obj() ###
|
||||
@logger_reset
|
||||
def create_post_comment_obj(post_comment_obj_new:Post_Comment_Base):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if not post_comment_obj_new:
|
||||
@@ -37,6 +38,7 @@ def create_post_comment_obj(post_comment_obj_new:Post_Comment_Base):
|
||||
|
||||
|
||||
# ### BEGIN ### API Post Comment Methods ### load_post_comment_obj() ###
|
||||
@logger_reset
|
||||
def load_post_comment_obj(
|
||||
post_comment_id: int|str,
|
||||
limit: int = 1000,
|
||||
@@ -47,13 +49,13 @@ def load_post_comment_obj(
|
||||
inc_person: bool = False,
|
||||
inc_user: bool = False,
|
||||
) -> Post_Comment_Base|dict|bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.INFO) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if post_comment_id := redis_lookup_id_random(record_id_random=post_comment_id, table_name='post_comment'): pass
|
||||
else: return False
|
||||
|
||||
if post_comment_rec := sql_select(table_name='v_post_comment_detail', record_id=post_comment_id):
|
||||
if post_comment_rec := sql_select(table_name='v_post_comment', record_id=post_comment_id):
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(post_comment_rec)
|
||||
else:
|
||||
@@ -105,12 +107,13 @@ def load_post_comment_obj(
|
||||
|
||||
|
||||
# ### BEGIN ### API Post Comment Methods ### update_post_comment_obj() ###
|
||||
@logger_reset
|
||||
def update_post_comment_obj(
|
||||
post_comment_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value.
|
||||
post_comment_obj_up: Post_Comment_Base,
|
||||
create_sub_obj: bool = False,
|
||||
) -> 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 post_comment_id := redis_lookup_id_random(record_id_random=post_comment_id, table_name='post_comment'): pass
|
||||
@@ -168,13 +171,15 @@ def update_post_comment_obj(
|
||||
|
||||
|
||||
# ### BEGIN ### API Post Comment Methods ### get_post_comment_rec_list() ###
|
||||
# Updated 2021-12-13
|
||||
@logger_reset
|
||||
def get_post_comment_rec_list(
|
||||
for_obj_type: str,
|
||||
for_obj_id: str,
|
||||
limit: int = 1000,
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
) -> list|bool:
|
||||
log.setLevel(logging.WARNING) # 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
|
||||
@@ -182,15 +187,15 @@ def get_post_comment_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_comment`.{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_comment`.enable = :enable'
|
||||
# elif enabled == 'disabled':
|
||||
# data['enable'] = False
|
||||
# sql_enabled = f'AND `tbl`.enable = :enable'
|
||||
# sql_enabled = f'AND `post_comment`.enable = :enable'
|
||||
# elif enabled == 'all':
|
||||
# sql_enabled = ''
|
||||
sql_enabled = ''
|
||||
@@ -202,20 +207,23 @@ def get_post_comment_rec_list(
|
||||
sql_limit = ''
|
||||
|
||||
sql = f"""
|
||||
SELECT `tbl`.id AS 'post_comment_id', `tbl`.id_random AS 'post_comment_id_random'
|
||||
FROM `post_comment` AS `tbl`
|
||||
SELECT `post_comment`.id AS 'post_comment_id', `post_comment`.id_random AS 'post_comment_id_random'
|
||||
FROM `post_comment` AS `post_comment`
|
||||
WHERE
|
||||
{sql_obj_type_id}
|
||||
{sql_enabled}
|
||||
ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC
|
||||
ORDER BY `post_comment`.created_on DESC, `post_comment`.updated_on DESC
|
||||
{sql_limit};
|
||||
"""
|
||||
log.debug(sql)
|
||||
|
||||
if post_comment_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
|
||||
log.info('Got a list result')
|
||||
post_comment_rec_li = post_comment_rec_li_result
|
||||
else:
|
||||
post_comment_rec_li = []
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
else: # [] or False
|
||||
log.info('No results or something went wrong')
|
||||
post_comment_rec_li = post_comment_rec_li_result
|
||||
|
||||
log.debug(post_comment_rec_li_result)
|
||||
|
||||
return post_comment_rec_li
|
||||
|
||||
Reference in New Issue
Block a user