Clean up and working on user related models, methods, and routes.

This commit is contained in:
Scott Idem
2021-06-11 15:00:39 -04:00
parent 03bb55e62a
commit 29dfcc5440
7 changed files with 476 additions and 139 deletions

View File

@@ -40,8 +40,9 @@ def create_post_obj(post_obj_new:Post_Base):
# ### BEGIN ### API Post Methods ### load_post_obj() ###
def load_post_obj(
post_id: int|str,
enabled: str = 'enabled', # enabled, disabled, all
limit: int = 1000,
model_as_dict: bool = False,
enabled: str = 'enabled', # enabled, disabled, all
inc_post_comment_list: bool = False,
inc_person: bool = False,
inc_user: bool = False,
@@ -77,59 +78,16 @@ def load_post_obj(
else: post_obj.person = None
if inc_post_comment_list:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
data = {}
data['post_id'] = post_id
if enabled in ['enabled', 'disabled', 'all']:
if enabled == 'enabled':
data['enable'] = True
sql_enabled = f'AND `post_comment`.enable = :enable'
elif enabled == 'disabled':
data['enable'] = False
sql_enabled = f'AND `post_comment`.enable = :enable'
elif enabled == 'all':
sql_enabled = ''
# else: event_obj['post'] = None
if limit:
data['limit'] = limit
sql_limit = f'LIMIT :limit'
else:
sql_limit = ''
sql = f"""
SELECT `post_comment`.id AS 'post_comment_id', `post_comment`.id_random AS 'post_comment_id_random'
FROM `post_comment` AS `post_comment`
WHERE `post_comment`.post_id = :post_id
{sql_enabled}
ORDER BY `post_comment`.created_on DESC, `post_comment`.updated_on DESC
{sql_limit};
"""
#log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
if post_comment_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(post_comment_rec_li_result)
post_comment_obj_li = []
for post_comment_rec in post_comment_rec_li_result:
post_comment_id = post_comment_rec.get('post_comment_id', None)
if post_comment_obj := load_post_comment_obj(
post_comment_id=post_comment_id,
enabled=enabled,
inc_person=inc_person,
inc_user=inc_user
):
data = post_comment_obj.dict(by_alias=True, exclude_unset=True)
post_comment_obj_li.append(data)
log.debug(post_comment_obj_li)
post_obj.post_comment_list = post_comment_obj_li
else:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(post_comment_rec_li_result)
post_obj.post_comment_list = []
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
if post_comment_dict_list := load_post_comment_obj_list(
post_id = post_id,
limit = limit,
model_as_dict = True,
enabled = enabled,
inc_person = inc_person,
inc_user = inc_user,
):
post_obj.post_comment_list = post_comment_dict_list
else: post_obj.post_comment_list = None
if inc_user:
user_id = post_rec.get('user_id', None)
@@ -141,10 +99,88 @@ def load_post_obj(
post_obj.user = user_obj
else: post_obj.user = None
return post_obj
if model_as_dict:
return post_obj.dict(by_alias=True, exclude_unset=True) # pylint: disable=no-member
else:
return post_obj
# ### END ### API Post Methods ### load_post_obj() ###
# ### BEGIN ### API Post Methods ### load_post_obj_list() ###
def load_post_obj_list(
account_id: int|str,
limit: int = 1000,
model_as_dict: bool = False,
enabled: str = 'enabled', # enabled, disabled, all
inc_post_comment_list: bool = False,
inc_person: bool = False,
inc_user: bool = False,
) -> list|bool:
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 False
data = {}
data['account_id'] = account_id
if enabled in ['enabled', 'disabled', 'all']:
if enabled == 'enabled':
data['enable'] = True
sql_enabled = f'AND `tbl`.enable = :enable'
elif enabled == 'disabled':
data['enable'] = False
sql_enabled = f'AND `tbl`.enable = :enable'
elif enabled == 'all':
sql_enabled = ''
# else: tbl_obj['account'] = None
if limit:
data['limit'] = limit
sql_limit = f'LIMIT :limit'
else:
sql_limit = ''
sql = f"""
SELECT `tbl`.id AS 'post_id', `tbl`.id_random AS 'post_id_random'
FROM `post` AS `tbl`
WHERE `tbl`.account_id = :account_id
{sql_enabled}
ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC
{sql_limit};
"""
if post_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(post_rec_li_result)
post_result_li = []
for post_rec in post_rec_li_result:
post_id = post_rec.get('post_id', None)
if post_result := load_post_obj(
post_id = post_id,
model_as_dict = model_as_dict,
enabled = enabled,
inc_post_comment_list = inc_post_comment_list,
inc_person = inc_person,
inc_user = inc_user,
):
log.debug(post_result)
post_result_li.append(post_result)
else:
log.debug(post_result)
post_result_li.append(None)
log.debug(post_result_li)
else:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(post_rec_li_result)
post_result_li = []
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
return post_result_li
# ### END ### API Post Methods ### load_post_obj_list() ###
# ### BEGIN ### API Post Methods ### update_post_obj() ###
def update_post_obj(
post_id: int|str, # Ideally the int ID should be passed. This allows for updating of the id_random value.