Work on post and post comment list.
This commit is contained in:
150
app/methods/post_comment_methods.py
Normal file
150
app/methods/post_comment_methods.py
Normal file
@@ -0,0 +1,150 @@
|
||||
from __future__ import annotations
|
||||
import datetime
|
||||
|
||||
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.methods.person_methods import create_person_obj, load_person_obj, update_person_obj
|
||||
from app.methods.user_methods import create_user_obj, load_user_obj, update_user_obj
|
||||
|
||||
from app.models.post_comment_models import Post_Comment_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Post Comment Methods ### create_post_comment_obj() ###
|
||||
def create_post_comment_obj(post_comment_obj_new:Post_Comment_Base):
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if not post_comment_obj_new:
|
||||
return False
|
||||
|
||||
post_comment_obj_data = post_comment_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'user', 'created_on', 'updated_on'})
|
||||
|
||||
if post_comment_obj_in_result := sql_insert(data=post_comment_obj_data, table_name='post_comment', rm_id_random=True, id_random_length=8): pass
|
||||
else:
|
||||
return False
|
||||
|
||||
log.debug(post_comment_obj_in_result)
|
||||
|
||||
post_comment_id = post_comment_obj_in_result
|
||||
|
||||
log.debug(f'Returning the new post_comment_id: {post_comment_id}')
|
||||
return post_comment_id
|
||||
# ### END ### API Post Comment Methods ### create_post_comment_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Post Comment Methods ### load_post_comment_obj() ###
|
||||
def load_post_comment_obj(
|
||||
post_comment_id: int|str,
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
limit: int = 1000,
|
||||
inc_person: bool = False,
|
||||
inc_user: bool = False,
|
||||
) -> Post_Comment_Base|bool:
|
||||
log.setLevel(logging.WARNING) # 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', record_id=post_comment_id):
|
||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(post_comment_rec)
|
||||
else:
|
||||
return False
|
||||
|
||||
try:
|
||||
post_comment_obj = Post_Comment_Base(**post_comment_rec)
|
||||
log.debug(post_comment_obj)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
|
||||
if inc_person:
|
||||
person_id = post_comment_rec.get('person_id', None)
|
||||
if person_obj_result := load_person_obj(person_id=person_id):
|
||||
person_obj = person_obj_result
|
||||
# post_comment_rec['person'] = person_obj
|
||||
# log.debug(post_comment_rec)
|
||||
#post_comment_obj.person = person_obj.dict(by_alias=True, exclude_unset=True)
|
||||
post_comment_obj.person = person_obj
|
||||
else: post_comment_obj.person = None
|
||||
|
||||
if inc_user:
|
||||
user_id = post_comment_rec.get('user_id', None)
|
||||
if user_obj_result := load_user_obj(user_id=user_id):
|
||||
user_obj = user_obj_result
|
||||
# post_comment_rec['user'] = user_obj
|
||||
# log.debug(post_comment_rec)
|
||||
#post_comment_obj.user = user_obj.dict(by_alias=True, exclude_unset=True)
|
||||
post_comment_obj.user = user_obj
|
||||
else: post_comment_obj.user = None
|
||||
|
||||
return post_comment_obj
|
||||
# ### END ### API Post Comment Methods ### load_post_comment_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Post Comment Methods ### update_post_comment_obj() ###
|
||||
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_missing_obj: bool = False,
|
||||
) -> bool:
|
||||
log.setLevel(logging.DEBUG) # 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
|
||||
|
||||
post_comment_obj_up.id = post_comment_id
|
||||
|
||||
log.debug(post_comment_obj_up)
|
||||
# log.debug(post_comment_obj_up.dict(by_alias=True, exclude_unset=True))
|
||||
log.debug(post_comment_obj_up.dict(by_alias=False, exclude_unset=True))
|
||||
# log.debug(post_comment_obj_up.dict(by_alias=False, exclude_unset=False))
|
||||
|
||||
#post_comment_dict_up = post_comment_obj_up.dict(by_alias=False, exclude_unset=True)
|
||||
|
||||
# if post_comment_obj_up.person_id and post_comment_obj_up.person:
|
||||
# person_id = post_comment_obj_up.person_id
|
||||
# person_obj_up = post_comment_obj_up.person
|
||||
# log.debug(person_id)
|
||||
# log.debug(person_obj_up)
|
||||
# if person_obj_up_result := update_person_obj(
|
||||
# person_id=person_id,
|
||||
# person_obj_up=person_obj_up,
|
||||
# create_missing_obj=create_missing_obj,
|
||||
# ):
|
||||
# log.debug(person_obj_up_result)
|
||||
# else:
|
||||
# log.debug(person_obj_up_result)
|
||||
# return False
|
||||
|
||||
# if post_comment_obj_up.user_id and post_comment_obj_up.user:
|
||||
# user_id = post_comment_obj_up.user_id
|
||||
# user_obj_up = post_comment_obj_up.user
|
||||
# log.debug(user_id)
|
||||
# log.debug(user_obj_up)
|
||||
# if user_obj_up_result := update_user_obj(
|
||||
# user_id=user_id,
|
||||
# user_obj_up=user_obj_up,
|
||||
# create_missing_obj=create_missing_obj,
|
||||
# ):
|
||||
# log.debug(user_obj_up_result)
|
||||
# else:
|
||||
# log.debug(user_obj_up_result)
|
||||
# return False
|
||||
|
||||
post_comment_dict_up = post_comment_obj_up.dict(by_alias=False, exclude_unset=True, exclude={'user'})
|
||||
log.debug(post_comment_dict_up)
|
||||
|
||||
if post_comment_obj_up_result := sql_update(data=post_comment_dict_up, table_name='post_comment', rm_id_random=True):
|
||||
log.debug(post_comment_obj_up_result)
|
||||
return True
|
||||
else:
|
||||
log.debug(post_comment_obj_up_result)
|
||||
return False
|
||||
# ### END ### API Post Comment Methods ### update_post_comment_obj() ###
|
||||
Reference in New Issue
Block a user