Working on various... order, post, cart, etc
This commit is contained in:
92
app/methods/lu_post_topic_methods.py
Normal file
92
app/methods/lu_post_topic_methods.py
Normal file
@@ -0,0 +1,92 @@
|
||||
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
|
||||
|
||||
|
||||
# ### BEGIN ### API Lookup Post Topic Methods ### load_post_topic() ###
|
||||
# def load_lu_post_topic_obj(
|
||||
# lu_post_topic_id: int|str,
|
||||
# limit: int = 1000,
|
||||
# by_alias: bool = True,
|
||||
# exclude_unset: bool = True,
|
||||
# model_as_dict: bool = False,
|
||||
# enabled: str = 'enabled', # enabled, disabled, all
|
||||
# ) -> Post_Base|bool:
|
||||
# log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
# log.debug(locals())
|
||||
|
||||
# if post_id := redis_lookup_id_random(record_id_random=post_id, table_name='post'): pass
|
||||
# else: return False
|
||||
|
||||
# if model_as_dict:
|
||||
# return post_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member
|
||||
# else:
|
||||
# return post_obj
|
||||
# ### END ### API Post Methods ### load_post_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Lookup Post Topic Methods ### get_lu_post_topic_rec_list() ###
|
||||
def get_lu_post_topic_rec_list(
|
||||
account_id: str,
|
||||
for_type: str,
|
||||
inc_admin_options: bool = False,
|
||||
limit: int = 1000,
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
) -> 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 = {}
|
||||
|
||||
if account_id:
|
||||
data['account_id'] = account_id
|
||||
sql_account_id = f'`tbl`.account_id = :account_id'
|
||||
else:
|
||||
sql_account_id = ''
|
||||
|
||||
if for_type:
|
||||
data['for_type'] = for_type
|
||||
sql_for_type = f'AND `tbl`.for_type = :for_type'
|
||||
else:
|
||||
sql_for_type = ''
|
||||
|
||||
if inc_admin_options:
|
||||
data['admin_option'] = [0, 1]
|
||||
sql_admin_option = f'AND `tbl`.admin_option IN :admin_option'
|
||||
else:
|
||||
data['admin_option'] = [0]
|
||||
sql_admin_option = f'AND `tbl`.admin_option IN :admin_option'
|
||||
|
||||
if limit:
|
||||
data['limit'] = limit
|
||||
sql_limit = f'LIMIT :limit'
|
||||
else:
|
||||
sql_limit = ''
|
||||
|
||||
sql = f"""
|
||||
SELECT * /*`tbl`.id AS 'lu_post_topic_id'*/
|
||||
FROM `lu_post_topic` AS `tbl`
|
||||
WHERE
|
||||
{sql_account_id}
|
||||
{sql_for_type}
|
||||
{sql_admin_option}
|
||||
ORDER BY `tbl`.sort DESC
|
||||
{sql_limit};
|
||||
"""
|
||||
|
||||
if lu_post_topic_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
|
||||
lu_post_topic_rec_li = lu_post_topic_rec_li_result
|
||||
else:
|
||||
lu_post_topic_rec_li = []
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(lu_post_topic_rec_li_result)
|
||||
|
||||
return lu_post_topic_rec_li
|
||||
# ### END ### API Lookup Post Topic Methods ### get_lu_post_topic_rec_list() ###
|
||||
@@ -199,8 +199,9 @@ def get_post_rec_list(
|
||||
for_obj_id: str,
|
||||
limit: int = 1000,
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
archive_on: datetime.datetime = None,
|
||||
) -> list|bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.DEBUG) # 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
|
||||
@@ -220,6 +221,10 @@ def get_post_rec_list(
|
||||
elif enabled == 'all':
|
||||
sql_enabled = ''
|
||||
|
||||
if archive_on:
|
||||
data['archive_on'] = archive_on
|
||||
sql_archive_on = 'AND tbl.archive_on >= :archive_on'
|
||||
|
||||
if limit:
|
||||
data['limit'] = limit
|
||||
sql_limit = f'LIMIT :limit'
|
||||
@@ -232,6 +237,7 @@ def get_post_rec_list(
|
||||
WHERE
|
||||
{sql_obj_type_id}
|
||||
{sql_enabled}
|
||||
{sql_archive_on}
|
||||
ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC
|
||||
{sql_limit};
|
||||
"""
|
||||
|
||||
@@ -26,13 +26,18 @@ class Post_Base(BaseModel):
|
||||
)
|
||||
account_id_random: Optional[str]
|
||||
account_id: Optional[int]
|
||||
|
||||
user_id_random: Optional[str]
|
||||
user_id: Optional[int]
|
||||
|
||||
type_id_random: Optional[str]
|
||||
type_id: Optional[int]
|
||||
|
||||
topic_id_random: Optional[str]
|
||||
topic_id: Optional[int]
|
||||
|
||||
type: Optional[str]
|
||||
|
||||
title: Optional[str]
|
||||
content: Optional[str]
|
||||
|
||||
@@ -65,7 +70,9 @@ class Post_Base(BaseModel):
|
||||
updated_on: Optional[datetime.datetime] = None
|
||||
|
||||
# Including other related objects
|
||||
post_comment_list: Optional[list] # Post_Comment_Base()
|
||||
post_comment_list: Optional[list] = Field(
|
||||
alias = 'comment_list'
|
||||
) # Post_Comment_Base()
|
||||
person: Optional[Person_Base]
|
||||
user: Optional[User_Base]
|
||||
|
||||
|
||||
@@ -14,6 +14,7 @@ from app.methods.account_cfg_methods import load_account_cfg_obj
|
||||
from app.methods.membership_group_methods import get_membership_group_rec_list, load_membership_group_obj
|
||||
from app.methods.membership_member_methods import get_membership_member_rec_list, load_membership_member_obj
|
||||
from app.methods.membership_type_methods import get_membership_type_rec_list, load_membership_type_obj
|
||||
from app.methods.post_methods import get_post_rec_list, load_post_obj
|
||||
|
||||
from app.models.account_models import Account_Base
|
||||
from app.models.response_models import Resp_Body_Base, mk_resp
|
||||
@@ -508,6 +509,67 @@ async def get_account_obj_membership_type_list(
|
||||
# ### END ### API Account ### get_account_obj_membership_type_list() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Account ### get_account_obj_post_list() ###
|
||||
# Working well as of 2021-06-28. Using as a template for other routes.
|
||||
@router.get('/{account_id}/post_list', response_model=Resp_Body_Base)
|
||||
async def get_account_obj_post_list(
|
||||
account_id: str = Query(..., min_length=1, max_length=22),
|
||||
limit: int = 500, # For now this covers any included objects or object lists
|
||||
enabled: str = 'enabled', # For now this covers any included objects or object lists
|
||||
archive_on: datetime.datetime = None,
|
||||
inc_account_cfg: bool = False,
|
||||
inc_address: bool = False, # Under contact
|
||||
inc_contact: bool = False,
|
||||
inc_person: bool = False,
|
||||
inc_post_comment_list: bool = False,
|
||||
inc_user: bool = False,
|
||||
x_account_id: str = Header(...),
|
||||
by_alias: Optional[bool] = True,
|
||||
exclude_unset: Optional[bool] = True,
|
||||
):
|
||||
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 mk_resp(data=None, status_code=404)
|
||||
|
||||
response_data = None
|
||||
|
||||
# Updated 2021-06-28
|
||||
if post_rec_list_result := get_post_rec_list(
|
||||
for_obj_type = 'account',
|
||||
for_obj_id = account_id,
|
||||
limit = limit,
|
||||
enabled = enabled,
|
||||
archive_on = archive_on
|
||||
):
|
||||
post_result_list = []
|
||||
for post_rec in post_rec_list_result:
|
||||
if load_post_result := load_post_obj(
|
||||
post_id = post_rec.get('post_id', None),
|
||||
limit = limit,
|
||||
by_alias = by_alias,
|
||||
exclude_unset = exclude_unset,
|
||||
# model_as_dict = model_as_dict,
|
||||
enabled = enabled,
|
||||
# inc_address = inc_address,
|
||||
# inc_contact = inc_contact,
|
||||
inc_person = inc_person,
|
||||
inc_post_comment_list = inc_post_comment_list,
|
||||
inc_user = inc_user,
|
||||
):
|
||||
post_result_list.append(load_post_result)
|
||||
else:
|
||||
post_result_list.append(None)
|
||||
response_data = post_result_list
|
||||
else:
|
||||
return mk_resp(data=False, status_code=400) # Bad Request
|
||||
|
||||
return mk_resp(data=response_data)
|
||||
# ### END ### API Account ### get_account_obj_post_list() ###
|
||||
|
||||
|
||||
@router.delete('/{obj_id}', response_model=Resp_Body_Base)
|
||||
async def delete_account_obj(
|
||||
obj_id: str = Query(..., min_length=1, max_length=22),
|
||||
|
||||
@@ -10,6 +10,8 @@ from app.db_sql import sql_select, redis_lookup_id_random
|
||||
|
||||
from app.routers.api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
|
||||
|
||||
from app.methods.lu_post_topic_methods import get_lu_post_topic_rec_list
|
||||
|
||||
from app.models.response_models import *
|
||||
#from app.models.lookup_models import Lookup_Base
|
||||
|
||||
@@ -20,27 +22,38 @@ router = APIRouter()
|
||||
@router.get('/{for_lookup_name}/list', response_model=Resp_Body_Base)
|
||||
async def get_lookup_li(
|
||||
for_lookup_name: Optional[str] = Query(None, min_length=2, max_length=50),
|
||||
#for_lookup_id: Optional[str] = Query(None, min_length=1, max_length=22),
|
||||
#x_account_id: str = Header(...),
|
||||
#by_alias: Optional[bool] = True,
|
||||
#include: Optional[list] = [],
|
||||
#exclude: Optional[list] = [],
|
||||
#exclude_unset: Optional[bool] = True,
|
||||
#exclude_none: Optional[bool] = True,
|
||||
account_id: Optional[str] = Query(None, min_length=2, max_length=50),
|
||||
for_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
||||
inc_admin_options: bool = False,
|
||||
limit: int = 1000,
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
):
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
allowed_lookup_tables = ['country', 'country_subdivision', 'event_session_type', 'file_purpose', 'membership_member_status', 'order_status', 'user_status']
|
||||
allowed_lookup_tables = ['country', 'country_subdivision', 'event_session_type', 'file_purpose', 'membership_member_status', 'order_status', 'post_topic', 'user_status']
|
||||
|
||||
if for_lookup_name in allowed_lookup_tables:
|
||||
table_name = f'lu_{for_lookup_name}'
|
||||
if for_lookup_name in allowed_lookup_tables: pass
|
||||
else:
|
||||
return mk_resp(data=False, status_code=400)
|
||||
|
||||
sql_result = sql_select(table_name=table_name)
|
||||
if for_lookup_name == 'post_topic':
|
||||
if lu_post_topic_rec_list_result := get_lu_post_topic_rec_list(
|
||||
account_id = account_id,
|
||||
for_type = for_type,
|
||||
inc_admin_options = inc_admin_options,
|
||||
limit = limit,
|
||||
enabled = enabled,
|
||||
):
|
||||
response_data = lu_post_topic_rec_list_result
|
||||
else:
|
||||
return mk_resp(data=False, status_code=400) # Bad Request
|
||||
else:
|
||||
table_name = f'lu_{for_lookup_name}'
|
||||
lu_list_result = sql_select(table_name=table_name)
|
||||
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(sql_result)
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(lu_list_result)
|
||||
response_data = lu_list_result
|
||||
|
||||
return mk_resp(data=sql_result)
|
||||
return mk_resp(data=response_data)
|
||||
|
||||
Reference in New Issue
Block a user