Working on membership, fundraising, and products
This commit is contained in:
@@ -211,7 +211,7 @@ app.include_router(
|
|||||||
)
|
)
|
||||||
app.include_router(
|
app.include_router(
|
||||||
product.router,
|
product.router,
|
||||||
prefix='/product',
|
# prefix='/product',
|
||||||
tags=['Product'],
|
tags=['Product'],
|
||||||
)
|
)
|
||||||
app.include_router(
|
app.include_router(
|
||||||
|
|||||||
@@ -13,7 +13,7 @@ from app.models.product_models import Product_Base
|
|||||||
# ### BEGIN ### API Product Methods ### load_product_obj() ###
|
# ### BEGIN ### API Product Methods ### load_product_obj() ###
|
||||||
def load_product_obj(
|
def load_product_obj(
|
||||||
product_id: int|str,
|
product_id: int|str,
|
||||||
limit: int = 1000,
|
# limit: int = 1000,
|
||||||
by_alias: bool = True,
|
by_alias: bool = True,
|
||||||
exclude_unset: bool = True,
|
exclude_unset: bool = True,
|
||||||
model_as_dict: bool = False,
|
model_as_dict: bool = False,
|
||||||
@@ -41,79 +41,73 @@ def load_product_obj(
|
|||||||
|
|
||||||
|
|
||||||
# ### BEGIN ### API Product Methods ### get_product_rec_list() ###
|
# ### BEGIN ### API Product Methods ### get_product_rec_list() ###
|
||||||
# Updated 2021-06-23
|
# Updated 2021-07-01
|
||||||
def get_product_rec_list(
|
def get_product_rec_list(
|
||||||
for_obj_type: str,
|
account_id: str = None,
|
||||||
for_obj_id: str,
|
for_obj_type: str = None,
|
||||||
|
for_obj_id: str = None,
|
||||||
|
prod_type: str = None,
|
||||||
limit: int = 1000,
|
limit: int = 1000,
|
||||||
enabled: str = 'enabled', # enabled, disabled, all
|
enabled: str = 'enabled', # enabled, disabled, all
|
||||||
) -> list|bool:
|
) -> 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())
|
log.debug(locals())
|
||||||
|
|
||||||
if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name='for_obj_type'): pass
|
if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
|
||||||
else: return False
|
if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type): pass
|
||||||
|
|
||||||
data = {}
|
data = {}
|
||||||
if for_obj_type == 'account':
|
data['account_id'] = account_id
|
||||||
sql_obj_type_id = f'`tbl`.{for_obj_type}_id = :{for_obj_type}_id'
|
data['for_obj_type'] = for_obj_type
|
||||||
|
data['for_obj_id'] = for_obj_id
|
||||||
|
|
||||||
if enabled in ['enabled', 'disabled', 'all']:
|
if account_id:
|
||||||
if enabled == 'enabled':
|
sql_account_id = f'`product`.account_id = :account_id'
|
||||||
data['enable'] = True
|
else: sql_account_id = ''
|
||||||
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 = ''
|
|
||||||
|
|
||||||
if limit:
|
if for_obj_type and for_obj_id: # event_exhibit, event_registration, fundraising, membership_group, membership_type
|
||||||
data['limit'] = limit
|
if for_obj_type == 'account':
|
||||||
sql_limit = f'LIMIT :limit'
|
sql_account_id = f'`product`.account_id = :for_obj_id'
|
||||||
|
sql_for_obj_type_id = ''
|
||||||
else:
|
else:
|
||||||
sql_limit = ''
|
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 = ''
|
||||||
|
|
||||||
sql = f"""
|
if enabled in ['enabled', 'disabled', 'all']:
|
||||||
SELECT `tbl`.id AS 'product_id', `tbl`.id_random AS 'product_id_random'
|
if enabled == 'enabled':
|
||||||
FROM `product` AS `tbl`
|
data['enable'] = True
|
||||||
WHERE
|
sql_enabled = f'AND `product`.enable = :enable'
|
||||||
{sql_obj_type_id}
|
elif enabled == 'disabled':
|
||||||
{sql_enabled}
|
data['enable'] = False
|
||||||
ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC
|
sql_enabled = f'AND `product`.enable = :enable'
|
||||||
{sql_limit};
|
elif enabled == 'all':
|
||||||
"""
|
sql_enabled = ''
|
||||||
else: # event_exhibit, event_registration, fundraising, membership_group, membership_type
|
else: sql_enabled = ''
|
||||||
data['for_obj_type'] = for_obj_type
|
|
||||||
data['for_obj_id'] = for_obj_id
|
|
||||||
|
|
||||||
sql_obj_type_id = f'`tbl`.for_type = :for_obj_type AND `tbl`.for_id = :for_obj_id'
|
if prod_type in ['event', 'event_option', 'event_registration', 'fundraising', 'membership_group', 'membership_type']:
|
||||||
|
data['type_name'] = prod_type
|
||||||
|
|
||||||
if enabled in ['enabled', 'disabled', 'all']:
|
sql_product_type = f"""AND product.type_name = :type_name"""
|
||||||
if enabled == 'enabled':
|
else: sql_product_type = ''
|
||||||
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 = ''
|
|
||||||
|
|
||||||
if limit:
|
if limit:
|
||||||
data['limit'] = limit
|
data['limit'] = limit
|
||||||
sql_limit = f'LIMIT :limit'
|
sql_limit = f'LIMIT :limit'
|
||||||
else:
|
else:
|
||||||
sql_limit = ''
|
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};
|
||||||
|
"""
|
||||||
|
|
||||||
sql = f"""
|
|
||||||
SELECT `tbl`.id AS 'product_id', `tbl`.id_random AS 'product_id_random'
|
|
||||||
FROM `product` AS `tbl`
|
|
||||||
WHERE
|
|
||||||
{sql_obj_type_id}
|
|
||||||
{sql_enabled}
|
|
||||||
ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC
|
|
||||||
{sql_limit};
|
|
||||||
"""
|
|
||||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.debug(sql)
|
log.debug(sql)
|
||||||
|
|
||||||
|
|||||||
@@ -20,15 +20,16 @@ class Product_Base(BaseModel):
|
|||||||
default_factory = lambda:secrets.token_urlsafe(default_num_bytes),
|
default_factory = lambda:secrets.token_urlsafe(default_num_bytes),
|
||||||
)
|
)
|
||||||
id: Optional[int] = Field(
|
id: Optional[int] = Field(
|
||||||
#alias = 'product_id'
|
alias = 'product_id'
|
||||||
)
|
)
|
||||||
account_id_random: Optional[str]
|
account_id_random: Optional[str]
|
||||||
account_id: Optional[int]
|
account_id: Optional[int]
|
||||||
|
|
||||||
type_id: Optional[int]
|
type_id: Optional[int]
|
||||||
type_name: Optional[str] = Field(
|
type_name: Optional[str] = Field(
|
||||||
alias = 'type'
|
# alias = 'type'
|
||||||
)
|
)
|
||||||
|
|
||||||
name: Optional[str]
|
name: Optional[str]
|
||||||
description: Optional[str]
|
description: Optional[str]
|
||||||
|
|
||||||
|
|||||||
@@ -648,7 +648,7 @@ async def get_account_obj_post_list(
|
|||||||
for_obj_id = account_id,
|
for_obj_id = account_id,
|
||||||
limit = limit,
|
limit = limit,
|
||||||
enabled = enabled,
|
enabled = enabled,
|
||||||
archive_on = archive_on
|
archive_on = archive_on,
|
||||||
):
|
):
|
||||||
post_result_list = []
|
post_result_list = []
|
||||||
for post_rec in post_rec_list_result:
|
for post_rec in post_rec_list_result:
|
||||||
|
|||||||
@@ -11,6 +11,8 @@ from app.db_sql import *
|
|||||||
|
|
||||||
from .api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
|
from .api_crud import delete_obj_template, get_obj_template, get_obj_li_template, patch_obj_template, post_obj_template
|
||||||
|
|
||||||
|
from app.methods.product_methods import get_product_rec_list, load_product_obj
|
||||||
|
|
||||||
from app.models.product_models import Product_Base
|
from app.models.product_models import Product_Base
|
||||||
from app.models.response_models import *
|
from app.models.response_models import *
|
||||||
|
|
||||||
@@ -18,7 +20,7 @@ from app.models.response_models import *
|
|||||||
router = APIRouter()
|
router = APIRouter()
|
||||||
|
|
||||||
|
|
||||||
@router.post('', response_model=Resp_Body_Base)
|
@router.post('/product', response_model=Resp_Body_Base)
|
||||||
async def post_product_obj(
|
async def post_product_obj(
|
||||||
obj: Product_Base,
|
obj: Product_Base,
|
||||||
x_account_id: str = Header(...),
|
x_account_id: str = Header(...),
|
||||||
@@ -41,7 +43,7 @@ async def post_product_obj(
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.patch('/{obj_id}', response_model=Resp_Body_Base)
|
@router.patch('/product/{obj_id}', response_model=Resp_Body_Base)
|
||||||
async def patch_product_obj(
|
async def patch_product_obj(
|
||||||
obj_id: str = Query(..., min_length=1, max_length=22),
|
obj_id: str = Query(..., min_length=1, max_length=22),
|
||||||
obj: Product_Base = None,
|
obj: Product_Base = None,
|
||||||
@@ -68,11 +70,13 @@ async def patch_product_obj(
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.get('/list', response_model=Resp_Body_Base)
|
@router.get('/product/list', response_model=Resp_Body_Base)
|
||||||
async def get_product_obj_li(
|
async def get_product_obj_li(
|
||||||
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
||||||
for_obj_id: Optional[str] = Query(None, min_length=1, max_length=22),
|
for_obj_id: Optional[str] = Query(None, min_length=1, max_length=22),
|
||||||
prod_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
prod_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
||||||
|
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
|
||||||
x_account_id: str = Header(...),
|
x_account_id: str = Header(...),
|
||||||
by_alias: Optional[bool] = True,
|
by_alias: Optional[bool] = True,
|
||||||
exclude_unset: Optional[bool] = True,
|
exclude_unset: Optional[bool] = True,
|
||||||
@@ -80,60 +84,87 @@ async def get_product_obj_li(
|
|||||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||||
log.debug(locals())
|
log.debug(locals())
|
||||||
|
|
||||||
obj_type = 'product'
|
if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type): pass
|
||||||
base_name = Product_Base
|
|
||||||
data = {}
|
|
||||||
|
|
||||||
if for_obj_type == 'account' and for_obj_id:
|
|
||||||
for_obj_id_random = for_obj_id
|
|
||||||
for_obj_id = redis_lookup_id_random(record_id_random=for_obj_id_random, table_name=for_obj_type)
|
|
||||||
|
|
||||||
data['for_obj_type'] = for_obj_type
|
|
||||||
data['for_obj_id'] = for_obj_id
|
|
||||||
data['for_obj_id_random'] = for_obj_id_random
|
|
||||||
|
|
||||||
sql_for_obj_type = f"""`product`.account_id = :for_obj_id"""
|
|
||||||
elif for_obj_type == 'event' and for_obj_id:
|
|
||||||
for_obj_id_random = for_obj_id
|
|
||||||
for_obj_id = redis_lookup_id_random(record_id_random=for_obj_id_random, table_name=for_obj_type)
|
|
||||||
|
|
||||||
data['for_obj_type'] = for_obj_type
|
|
||||||
data['for_obj_id'] = for_obj_id
|
|
||||||
data['for_obj_id_random'] = for_obj_id_random
|
|
||||||
|
|
||||||
sql_for_obj_type = f"""`product`.for_type = :for_obj_type AND `product`.for_id = :for_obj_id"""
|
|
||||||
else: sql_for_obj_type = ''
|
|
||||||
|
|
||||||
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 = ''
|
|
||||||
|
|
||||||
sql = f"""
|
|
||||||
SELECT *
|
|
||||||
FROM `v_product` AS product
|
|
||||||
WHERE {sql_for_obj_type}
|
|
||||||
{sql_product_type}
|
|
||||||
"""
|
|
||||||
|
|
||||||
#log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
|
||||||
log.debug(data)
|
|
||||||
log.debug(sql)
|
|
||||||
|
|
||||||
if sql_result := sql_select(data=data, sql=sql, as_list=True):
|
|
||||||
resp_data_li = []
|
|
||||||
for record in sql_result:
|
|
||||||
resp_data = base_name(**record).dict(by_alias=by_alias, exclude_unset=exclude_unset)
|
|
||||||
resp_data_li.append(resp_data)
|
|
||||||
|
|
||||||
return mk_resp(data=resp_data_li)
|
|
||||||
else:
|
else:
|
||||||
log.debug(sql_result)
|
return mk_resp(data=None, status_code=404)
|
||||||
return mk_resp(data=False, status_code=404)
|
|
||||||
|
response_data = None
|
||||||
|
|
||||||
|
# Updated 2021-07-01
|
||||||
|
if product_rec_list_result := get_product_rec_list(
|
||||||
|
for_obj_type = for_obj_type,
|
||||||
|
for_obj_id = for_obj_id,
|
||||||
|
prod_type = prod_type,
|
||||||
|
limit = limit,
|
||||||
|
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),
|
||||||
|
limit = limit,
|
||||||
|
by_alias = by_alias,
|
||||||
|
exclude_unset = exclude_unset,
|
||||||
|
# model_as_dict = model_as_dict,
|
||||||
|
):
|
||||||
|
product_result_list.append(load_product_result)
|
||||||
|
else:
|
||||||
|
product_result_list.append(None)
|
||||||
|
response_data = product_result_list
|
||||||
|
else:
|
||||||
|
return mk_resp(data=False, status_code=400) # Bad Request
|
||||||
|
|
||||||
|
return mk_resp(data=response_data)
|
||||||
|
|
||||||
|
|
||||||
@router.get('/{obj_id}', response_model=Resp_Body_Base)
|
@router.get('/account/{account_id}/product/list', response_model=Resp_Body_Base)
|
||||||
|
async def get_account_product_obj_li(
|
||||||
|
account_id: str = Query(..., min_length=1, max_length=22),
|
||||||
|
for_obj_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
||||||
|
for_obj_id: Optional[str] = Query(None, min_length=1, max_length=22),
|
||||||
|
prod_type: Optional[str] = Query(None, min_length=2, max_length=50),
|
||||||
|
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
|
||||||
|
x_account_id: str = Header(...),
|
||||||
|
by_alias: Optional[bool] = True,
|
||||||
|
exclude_unset: Optional[bool] = True,
|
||||||
|
):
|
||||||
|
log.setLevel(logging.DEBUG) # 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)
|
||||||
|
if for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name=for_obj_type): pass
|
||||||
|
else: pass
|
||||||
|
|
||||||
|
# Updated 2021-07-01
|
||||||
|
if product_rec_list_result := get_product_rec_list(
|
||||||
|
account_id = account_id,
|
||||||
|
for_obj_type = for_obj_type,
|
||||||
|
for_obj_id = for_obj_id,
|
||||||
|
prod_type = prod_type,
|
||||||
|
limit = limit,
|
||||||
|
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),
|
||||||
|
by_alias = by_alias,
|
||||||
|
exclude_unset = exclude_unset,
|
||||||
|
# model_as_dict = model_as_dict,
|
||||||
|
):
|
||||||
|
product_result_list.append(load_product_result)
|
||||||
|
else:
|
||||||
|
product_result_list.append(None)
|
||||||
|
response_data = product_result_list
|
||||||
|
else:
|
||||||
|
return mk_resp(data=False, status_code=400) # Bad Request
|
||||||
|
|
||||||
|
return mk_resp(data=response_data)
|
||||||
|
|
||||||
|
|
||||||
|
@router.get('/product/{obj_id}', response_model=Resp_Body_Base)
|
||||||
async def get_product_obj(
|
async def get_product_obj(
|
||||||
obj_id: str = Query(..., min_length=1, max_length=22),
|
obj_id: str = Query(..., min_length=1, max_length=22),
|
||||||
x_account_id: str = Header(...),
|
x_account_id: str = Header(...),
|
||||||
@@ -153,7 +184,7 @@ async def get_product_obj(
|
|||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
@router.delete('/{obj_id}', response_model=Resp_Body_Base)
|
@router.delete('/product/{obj_id}', response_model=Resp_Body_Base)
|
||||||
async def delete_product_obj(
|
async def delete_product_obj(
|
||||||
obj_id: str = Query(..., min_length=1, max_length=22),
|
obj_id: str = Query(..., min_length=1, max_length=22),
|
||||||
x_account_id: str = Header(...),
|
x_account_id: str = Header(...),
|
||||||
|
|||||||
Reference in New Issue
Block a user