123 lines
4.2 KiB
Python
123 lines
4.2 KiB
Python
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.lib_general import log, logging
|
|
from app.db_sql import redis_lookup_id_random, sql_insert_or_update, sql_select
|
|
|
|
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,
|
|
) -> Product_Base:
|
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(locals())
|
|
|
|
if product_id := redis_lookup_id_random(record_id_random=product_id, table_name='product'): pass
|
|
else: return False
|
|
|
|
if product_rec := sql_select(table_name='v_product', record_id=product_id): pass
|
|
else: return False
|
|
|
|
try:
|
|
product_obj = Product_Base(**product_rec)
|
|
log.debug(product_obj)
|
|
except ValidationError as e:
|
|
log.error(e.json())
|
|
|
|
if model_as_dict:
|
|
return product_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member
|
|
else:
|
|
return product_obj
|
|
# ### END ### API Product Methods ### load_product_obj() ###
|
|
|
|
|
|
# ### BEGIN ### API Product Methods ### get_product_rec_list() ###
|
|
# Updated 2021-07-01
|
|
def get_product_rec_list(
|
|
account_id: str = None,
|
|
for_obj_type: str = None,
|
|
for_obj_id: str = None,
|
|
prod_type: str = None,
|
|
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
|
|
if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type): pass
|
|
|
|
data = {}
|
|
data['account_id'] = account_id
|
|
data['for_obj_type'] = for_obj_type
|
|
data['for_obj_id'] = for_obj_id
|
|
|
|
if account_id:
|
|
sql_account_id = f'`product`.account_id = :account_id'
|
|
else: sql_account_id = ''
|
|
|
|
if for_obj_type and for_obj_id: # event_exhibit, event_registration, fundraising, membership_group, membership_type
|
|
if for_obj_type == 'account':
|
|
sql_account_id = f'`product`.account_id = :for_obj_id'
|
|
sql_for_obj_type_id = ''
|
|
else:
|
|
sql_for_obj_type_id = f'`product`.for_type = :for_obj_type AND `product`.for_id = :for_obj_id'
|
|
else: sql_for_obj_type_id = ''
|
|
|
|
if enabled in ['enabled', 'disabled', 'all']:
|
|
if enabled == 'enabled':
|
|
data['enable'] = True
|
|
sql_enabled = f'AND `product`.enable = :enable'
|
|
elif enabled == 'disabled':
|
|
data['enable'] = False
|
|
sql_enabled = f'AND `product`.enable = :enable'
|
|
elif enabled == 'all':
|
|
sql_enabled = ''
|
|
else: sql_enabled = ''
|
|
|
|
if prod_type in ['event', 'event_option', 'event_registration', 'fundraising', 'membership_group', 'membership_type']:
|
|
data['type_name'] = prod_type
|
|
|
|
sql_product_type = f"""AND product.type_name = :type_name"""
|
|
else: sql_product_type = ''
|
|
|
|
if limit:
|
|
data['limit'] = limit
|
|
sql_limit = f'LIMIT :limit'
|
|
else:
|
|
sql_limit = ''
|
|
|
|
sql = f"""
|
|
SELECT `product`.id AS 'product_id', `product`.id_random AS 'product_id_random'
|
|
FROM `v_product` AS `product`
|
|
WHERE
|
|
{sql_account_id}
|
|
{sql_for_obj_type_id}
|
|
{sql_product_type}
|
|
{sql_enabled}
|
|
ORDER BY `product`.created_on DESC, `product`.updated_on DESC
|
|
{sql_limit};
|
|
"""
|
|
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(sql)
|
|
|
|
if product_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
|
|
product_rec_li = product_rec_li_result
|
|
else:
|
|
product_rec_li = []
|
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
log.debug(product_rec_li_result)
|
|
|
|
return product_rec_li
|
|
# ### END ### API Product Methods ### get_product_rec_list() ###
|