Added better fundraising routes, methods, and models! I thought I already did this...
This commit is contained in:
@@ -41,7 +41,7 @@ def load_account_cfg_obj(
|
||||
log.error(e.json())
|
||||
|
||||
if inc_fundraising_cfg:
|
||||
if fundraising_cfg_dict := load_fundraising_cfg_obj(
|
||||
if fundraising_cfg_dict := load_fundraising_cfg_obj_old(
|
||||
account_id = account_id,
|
||||
model_as_dict = model_as_dict,
|
||||
):
|
||||
|
||||
@@ -1,17 +1,49 @@
|
||||
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_select
|
||||
from app.lib_general import log, logging
|
||||
from app.db_sql import redis_lookup_id_random, sql_enable_part, sql_insert, sql_limit_offset_part, sql_select, sql_update
|
||||
from app.lib_general import log, logging, logger_reset
|
||||
|
||||
from app.models.fundraising_cfg_models import Fundraising_Cfg_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Fundraising Cfg Methods ### load_fundraising_cfg_obj() ###
|
||||
# Updated 2022-11-18
|
||||
def load_fundraising_cfg_obj(
|
||||
fundraising_id: int|str,
|
||||
model_as_dict: bool = False,
|
||||
) -> Fundraising_Cfg_Base|dict|bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if fundraising_id := redis_lookup_id_random(record_id_random=fundraising_id, table_name='fundraising'): pass
|
||||
else: return False
|
||||
|
||||
if fundraising_cfg_rec := sql_select(
|
||||
table_name = 'v_fundraising_cfg',
|
||||
field_name = 'fundraising_id',
|
||||
field_value = fundraising_id
|
||||
): pass
|
||||
else: return False
|
||||
log.debug(fundraising_cfg_rec)
|
||||
try:
|
||||
fundraising_cfg_obj = Fundraising_Cfg_Base(**fundraising_cfg_rec)
|
||||
log.debug(fundraising_cfg_obj)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
|
||||
if model_as_dict:
|
||||
return fundraising_cfg_obj.dict(by_alias=True, exclude_unset=True) # pylint: disable=no-member
|
||||
else:
|
||||
return fundraising_cfg_obj
|
||||
# ### END ### API Fundraising Cfg Methods ### load_fundraising_cfg_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Fundraising Cfg Methods ### load_fundraising_cfg_obj_old() ###
|
||||
# Updated 2022-11-18
|
||||
def load_fundraising_cfg_obj_old(
|
||||
account_id: int|str,
|
||||
model_as_dict: bool = False,
|
||||
) -> Fundraising_Cfg_Base|dict|bool:
|
||||
@@ -38,4 +70,4 @@ def load_fundraising_cfg_obj(
|
||||
return fundraising_cfg_obj.dict(by_alias=True, exclude_unset=True) # pylint: disable=no-member
|
||||
else:
|
||||
return fundraising_cfg_obj
|
||||
# ### END ### API Fundraising Cfg Methods ### load_fundraising_cfg_obj() ###
|
||||
# ### END ### API Fundraising Cfg Methods ### load_fundraising_cfg_obj_old() ###
|
||||
|
||||
165
app/methods/fundraising_methods.py
Normal file
165
app/methods/fundraising_methods.py
Normal file
@@ -0,0 +1,165 @@
|
||||
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_enable_part, sql_insert, sql_limit_offset_part, sql_select, sql_update
|
||||
from app.lib_general import log, logging, logger_reset
|
||||
|
||||
from app.methods.fundraising_cfg_methods import load_fundraising_cfg_obj
|
||||
from app.methods.product_methods import get_product_rec_list, load_product_obj
|
||||
|
||||
from app.models.fundraising_models import Fundraising_Base
|
||||
from app.models.fundraising_cfg_models import Fundraising_Cfg_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Fundraising Cfg Methods ### load_fundraising_obj() ###
|
||||
# Updated 2022-11-18
|
||||
def load_fundraising_obj(
|
||||
fundraising_id: int|str,
|
||||
|
||||
inc_fundraising_cfg: bool = False,
|
||||
inc_product_list: bool = False,
|
||||
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
limit: int = 100,
|
||||
offset: int = 0,
|
||||
|
||||
model_as_dict: bool = False,
|
||||
) -> Fundraising_Base|dict|bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if fundraising_id := redis_lookup_id_random(record_id_random=fundraising_id, table_name='fundraising'): pass
|
||||
else: return False
|
||||
|
||||
if fundraising_rec := sql_select(table_name='v_fundraising', record_id=fundraising_id): pass
|
||||
else: return False
|
||||
|
||||
log.debug(fundraising_rec)
|
||||
|
||||
try:
|
||||
fundraising_obj = Fundraising_Base(**fundraising_rec)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
return False
|
||||
log.debug(fundraising_obj)
|
||||
|
||||
# Updated 2022-11-18
|
||||
if inc_fundraising_cfg:
|
||||
log.info('Need to include fundraising configuration...')
|
||||
if fundraising_cfg_result := load_fundraising_cfg_obj(
|
||||
fundraising_id = fundraising_id,
|
||||
|
||||
# by_alias = by_alias,
|
||||
# exclude_unset = exclude_unset,
|
||||
# model_as_dict = model_as_dict,
|
||||
):
|
||||
fundraising_obj.fundraising_cfg = fundraising_cfg_result
|
||||
else: fundraising_obj.fundraising_cfg = {} # None
|
||||
log.debug(fundraising_obj.fundraising_cfg)
|
||||
|
||||
# Updated 2022-11-18
|
||||
if inc_product_list:
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.info('Need to include product list...')
|
||||
|
||||
if product_rec_list_result := get_product_rec_list(
|
||||
for_obj_type = 'fundraising',
|
||||
for_obj_id = fundraising_id,
|
||||
enabled = enabled,
|
||||
):
|
||||
product_result_list = []
|
||||
for product_rec in product_rec_list_result:
|
||||
if load_product_result := load_product_obj(
|
||||
product_id = product_rec.get('product_id', None),
|
||||
):
|
||||
product_result_list.append(load_product_result)
|
||||
else:
|
||||
product_result_list.append(None)
|
||||
log.debug(product_result_list)
|
||||
fundraising_obj.product_list = product_result_list
|
||||
elif isinstance(product_rec_list_result, list):
|
||||
fundraising_obj.product_list = []
|
||||
else:
|
||||
fundraising_obj.product_list = None
|
||||
|
||||
if model_as_dict:
|
||||
return fundraising_obj.dict(by_alias=True, exclude_unset=True) # pylint: disable=no-member
|
||||
else:
|
||||
return fundraising_obj
|
||||
# ### END ### API Fundraising Cfg Methods ### load_fundraising_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Fundraising Cfg Methods ### get_fundraising_rec_list() ###
|
||||
@logger_reset
|
||||
def get_fundraising_rec_list(
|
||||
account_id: str = None,
|
||||
|
||||
enabled: str = 'enabled', # enabled, disabled, all
|
||||
hidden: str = 'not_hidden', # hidden, not_hidden, all
|
||||
priority: str = 'all', # priority, not_priority, all
|
||||
|
||||
limit: int = 100,
|
||||
offset: int = 0,
|
||||
) -> list|bool:
|
||||
log.setLevel(logging.INFO) # 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: pass
|
||||
|
||||
data = {}
|
||||
data['account_id'] = account_id
|
||||
|
||||
sql_where_account_id = f'`fundraising`.account_id = :account_id'
|
||||
|
||||
sql_hidden = ''
|
||||
if hidden in ['hidden', 'not_hidden', 'all']:
|
||||
if hidden == 'hidden':
|
||||
data['hide'] = True
|
||||
sql_hidden = f'AND `fundraising`.hide = :hide'
|
||||
elif hidden == 'not_hidden':
|
||||
data['hide'] = False
|
||||
sql_hidden = f'AND `fundraising`.hide = :hide'
|
||||
elif hidden == 'all':
|
||||
sql_hidden = ''
|
||||
|
||||
sql_priority = ''
|
||||
if priority in ['priority', 'not_priority', 'all']:
|
||||
if priority == 'priority':
|
||||
data['priority'] = True
|
||||
sql_priority = f'AND `fundraising`.priority = :priority'
|
||||
elif priority == 'not_priority':
|
||||
data['priority'] = False
|
||||
sql_priority = f'AND `fundraising`.priority = :priority'
|
||||
elif priority == 'all':
|
||||
sql_priority = ''
|
||||
|
||||
sql_enabled, data['enable'] = sql_enable_part(table_name='fundraising', enabled=enabled) # Reasonably safe return str and bool
|
||||
sql_limit = sql_limit_offset_part(limit=limit, offset=offset) # Reasonably safe return str
|
||||
|
||||
sql = f"""
|
||||
SELECT `fundraising`.id AS 'fundraising_id', `fundraising`.id_random AS 'fundraising_id_random'
|
||||
FROM `fundraising` AS `fundraising`
|
||||
WHERE
|
||||
{sql_where_account_id}
|
||||
{sql_hidden}
|
||||
{sql_priority}
|
||||
{sql_enabled}
|
||||
ORDER BY `fundraising`.priority DESC, -`fundraising`.sort DESC, `fundraising`.title ASC, `fundraising`.created_on DESC, `fundraising`.updated_on DESC
|
||||
{sql_limit};
|
||||
"""
|
||||
|
||||
if fundraising_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
|
||||
|
||||
fundraising_rec_li = fundraising_rec_li_result
|
||||
else:
|
||||
fundraising_rec_li = []
|
||||
# log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(fundraising_rec_li_result)
|
||||
log.debug(type(fundraising_rec_li))
|
||||
log.debug(len(fundraising_rec_li))
|
||||
|
||||
return fundraising_rec_li
|
||||
# ### END ### API Fundraising Cfg Methods ### get_fundraising_rec_list() ###
|
||||
@@ -13,7 +13,7 @@ from app.models.product_models import Product_Base
|
||||
# ### BEGIN ### API Product Methods ### load_product_obj() ###
|
||||
def load_product_obj(
|
||||
product_id: int|str,
|
||||
# limit: int = 1000,
|
||||
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
model_as_dict: bool = False,
|
||||
|
||||
Reference in New Issue
Block a user