Moving stuff around to make it more easy to manage. Adding models, methods, and routes.

This commit is contained in:
Scott Idem
2021-06-11 14:07:10 -04:00
parent 9c679765e5
commit 03bb55e62a
12 changed files with 473 additions and 116 deletions

View File

@@ -7,65 +7,56 @@ from pydantic import BaseModel, EmailStr, Field, PrivateAttr, ValidationError, v
from app.db_sql import redis_lookup_id_random, sql_select
from app.lib_general import log, logging
from app.methods.event_methods import load_event_obj
from app.methods.membership_cfg_methods import load_membership_cfg_obj
from app.models.account_cfg_models import Account_Cfg_Base
# from app.models.membership_cfg_models import Membership_Cfg_Base
# ### BEGIN ### API Account Methods ### load_account_cfg_obj() ###
def load_account_cfg_obj(account_id:int|str) -> Account_Cfg_Base|bool:
# ### BEGIN ### API Account Cfg Methods ### load_account_cfg_obj() ###
def load_account_cfg_obj(
account_id: int|str,
model_as_dict: bool = False,
# inc_event_cfg: bool = False,
inc_fundraising_cfg: bool = False,
inc_membership_cfg: bool = False,
) -> Account_Cfg_Base|dict|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
if account_cfg_rec := sql_select(table_name='v_account_cfg', field_name='account_id', field_value=account_id):
log.debug(account_cfg_rec)
try:
account_cfg_obj = Account_Cfg_Base(**account_cfg_rec)
log.debug(account_cfg_obj)
except ValidationError as e:
log.error(e.json())
return account_cfg_obj
if account_cfg_rec := sql_select(
table_name = 'v_account_cfg_detail', # This view should probably be cleaned up
field_name = 'account_id',
field_value = account_id
): pass
else: return False
# ### END ### API Account Methods ### load_account_cfg_obj() ###
log.debug(account_cfg_rec)
try:
account_cfg_obj = Account_Cfg_Base(**account_cfg_rec)
log.debug(account_cfg_obj)
except ValidationError as e:
log.error(e.json())
if inc_fundraising_cfg:
if fundraising_cfg_dict := load_fundraising_cfg_obj(
account_id = account_id,
model_as_dict = model_as_dict,
):
account_cfg_obj.fundraising_cfg = fundraising_cfg_dict
else: account_cfg_obj.fundraising_cfg = None
# ### BEGIN ### API Account Methods ### load_account_cfg_obj() ###
# def load_account_cfg_obj(account_id:int|str, inc_event_cfg:bool=False, inc_fundraising_cfg:bool=False, inc_membership_cfg:bool=False):
# log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# log.debug(locals())
if inc_membership_cfg:
if membership_cfg_dict := load_membership_cfg_obj(
account_id = account_id,
model_as_dict = model_as_dict,
):
account_cfg_obj.membership_cfg = membership_cfg_dict
else: account_cfg_obj.membership_cfg = None
# if account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
# else: return False
# if account_cfg_rec := sql_select(table_name='v_account_cfg', field_name='account_id', field_value=account_id):
# log.debug(account_cfg_rec)
# try:
# account_cfg_obj = Account_Cfg_Base(**account_cfg_rec)
# log.debug(account_cfg_obj)
# except ValidationError as e:
# log.error(e.json())
# return account_cfg_obj
# else: return False
# if inc_membership_cfg:
# if membership_cfg_rec := sql_select(table_name='v_membership_cfg', field_name='account_id', field_value=account_id):
# #log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
# log.debug(membership_cfg_rec)
# #account_rec['membership_cfg'] = membership_cfg_rec
# try:
# membership_cfg_obj = Membership_Cfg_Base(**membership_cfg_rec)
# log.debug(membership_cfg_obj)
# except ValidationError as e:
# log.error(e.json())
# return membership_cfg_obj
# ### END ### API Account Methods ### load_account_cfg_obj() ###
if model_as_dict:
return account_cfg_obj.dict(by_alias=True, exclude_unset=True) # pylint: disable=no-member
else:
return account_cfg_obj
# ### END ### API Account Cfg Methods ### load_account_cfg_obj() ###

View File

@@ -8,6 +8,7 @@ from app.db_sql import redis_lookup_id_random, sql_select
from app.lib_general import log, logging
from app.methods.account_cfg_methods import load_account_cfg_obj
from app.methods.archive_methods import load_archive_obj, load_archive_obj_list
from app.methods.event_methods import load_event_obj
from app.models.account_models import Account_Base
@@ -16,15 +17,19 @@ from app.models.account_cfg_models import Account_Cfg_Base
# ### BEGIN ### API Account Methods ### load_account_obj() ###
# Working well as of 2021-06-11. Using as a template for other load objects.
def load_account_obj(
account_id: int|str,
enabled: str = 'enabled', # enabled, disabled, all
limit: int = 1000,
model_as_dict: bool = False,
enabled: str = 'enabled', # enabled, disabled, all
inc_account_cfg: bool = False, # Priority l1
inc_address: bool = False, # Under contact
inc_address_list: bool = False, # Priority l3
inc_archive: bool = False,
inc_archive_list: bool = False, # Priority l1
inc_archive_content: bool = False,
inc_archive_content_list: bool = False, # Priority l2
inc_contact: bool = False,
inc_contact_list: bool = False, # Priority l3
inc_event: bool = False,
@@ -80,7 +85,7 @@ def load_account_obj(
inc_site_list: bool = False, # Priority l3
inc_user: bool = False,
inc_user_list: bool = False, # Priority l2
) -> Account_Base|bool:
) -> Account_Base|dict|bool:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
@@ -100,13 +105,28 @@ def load_account_obj(
log.error(e.json())
if inc_account_cfg:
organization_id = account_rec.get('organization_id', None)
if account_cfg_obj_result := load_account_cfg_obj(account_id=account_id):
account_cfg_obj = account_cfg_obj_result
account_cfg_dict = account_cfg_obj.dict(by_alias=True, exclude_unset=True)
if account_cfg_dict := load_account_cfg_obj(
account_id = account_id,
model_as_dict = True,
# inc_event_cfg = inc_event_cfg,
inc_fundraising_cfg = inc_fundraising_cfg,
inc_membership_cfg = inc_membership_cfg,
):
account_obj.account_cfg = account_cfg_dict
else: account_obj.account_cfg = None
if inc_archive_list:
if archive_dict_list := load_archive_obj_list(
account_id = account_id,
limit = limit,
model_as_dict = True,
enabled = enabled,
inc_archive_content_list = inc_archive_content_list
):
account_obj.archive_list = archive_dict_list
else: account_obj.archive_list = None
# if inc_contact:
# contact_id = account_rec.get('contact_id', None)
# if contact_obj_result := load_contact_obj(contact_id=contact_id):
@@ -206,5 +226,8 @@ def load_account_obj(
account_obj.event_list = []
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
return account_obj
if model_as_dict:
return account_obj.dict(by_alias=True, exclude_unset=True) # pylint: disable=no-member
else:
return account_obj
# ### END ### API Account Methods ### load_account_obj() ###

View File

@@ -0,0 +1,114 @@
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.models.archive_content_models import Archive_Content_Base
# ### BEGIN ### API Archive Content Methods ### load_archive_content_obj() ###
def load_archive_content_obj(
archive_content_id: int|str,
# limit: int = 1000,
model_as_dict: bool = False,
# enabled: str = 'enabled', # enabled, disabled, all
# inc_archive_content_content_list: bool = False,
) -> Archive_Content_Base|dict|bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if archive_content_id := redis_lookup_id_random(record_id_random=archive_content_id, table_name='archive_content'): pass
else: return False
if archive_content_rec := sql_select(
table_name='v_archive_content',
record_id=archive_content_id,
): pass
else: return False
log.debug(archive_content_rec)
try:
archive_content_obj = Archive_Content_Base(**archive_content_rec)
log.debug(archive_content_obj)
except ValidationError as e:
log.error(e.json())
if model_as_dict:
return archive_content_obj.dict(by_alias=True, exclude_unset=True) # pylint: disable=no-member
else:
return archive_content_obj
# ### END ### API Archive Content Methods ### load_archive_content_obj() ###
# ### BEGIN ### API Archive Content Methods ### load_archive_content_obj_list() ###
def load_archive_content_obj_list(
archive_id: int|str,
limit: int = 1000,
model_as_dict: bool = False,
enabled: str = 'enabled', # enabled, disabled, all
# inc_archive_content_content_list: bool = False,
) -> list|bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if archive_id := redis_lookup_id_random(record_id_random=archive_id, table_name='archive'): pass
else: return False
data = {}
data['archive_id'] = archive_id
if enabled in ['enabled', 'disabled', 'all']:
if enabled == 'enabled':
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 = ''
# else: tbl_obj['archive'] = None
if limit:
data['limit'] = limit
sql_limit = f'LIMIT :limit'
else:
sql_limit = ''
sql = f"""
SELECT `tbl`.id AS 'archive_content_id', `tbl`.id_random AS 'archive_content_id_random'
FROM `archive_content` AS `tbl`
WHERE `tbl`.archive_id = :archive_id
{sql_enabled}
ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC
{sql_limit};
"""
if archive_content_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(archive_content_rec_li_result)
archive_content_result_li = []
for archive_content_rec in archive_content_rec_li_result:
archive_content_id = archive_content_rec.get('archive_content_id', None)
if archive_content_result := load_archive_content_obj(
archive_content_id=archive_content_id,
model_as_dict=model_as_dict,
# enabled=enabled,
# inc_archive_content_content_list=inc_archive_content_content_list,
):
log.debug(archive_content_result)
archive_content_result_li.append(archive_content_result)
else:
log.debug(archive_content_result)
archive_content_result_li.append(None)
log.debug(archive_content_result_li)
else:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(archive_content_rec_li_result)
archive_content_result_li = []
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
return archive_content_result_li
# ### END ### API Archive Content Methods ### load_archive_content_obj_list() ###

View File

@@ -0,0 +1,127 @@
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.methods.archive_content_methods import load_archive_content_obj, load_archive_content_obj_list
from app.models.archive_models import Archive_Base
# ### BEGIN ### API Archive Methods ### load_archive_obj() ###
def load_archive_obj(
archive_id: int|str,
limit: int = 1000,
model_as_dict: bool = False,
enabled: str = 'enabled', # enabled, disabled, all
inc_archive_content_list: bool = False,
) -> Archive_Base|dict|bool:
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(locals())
if archive_id := redis_lookup_id_random(record_id_random=archive_id, table_name='archive'): pass
else: return False
if archive_rec := sql_select(
table_name='v_archive',
record_id=archive_id,
): pass
else: return False
log.debug(archive_rec)
try:
archive_obj = Archive_Base(**archive_rec)
log.debug(archive_obj)
except ValidationError as e:
log.error(e.json())
if inc_archive_content_list:
if archive_content_dict_list := load_archive_content_obj_list(
archive_id = archive_id,
limit=limit,
model_as_dict = True,
enabled=enabled,
# inc_archive_content_content_list=inc_archive_content_content_list
):
archive_obj.archive_content_list = archive_content_dict_list
else: archive_obj.archive_content_list = None
if model_as_dict:
return archive_obj.dict(by_alias=True, exclude_unset=True) # pylint: disable=no-member
else:
return archive_obj
# ### END ### API Archive Methods ### load_archive_obj() ###
# ### BEGIN ### API Archive Methods ### load_archive_obj_list() ###
def load_archive_obj_list(
account_id: int|str,
limit: int = 1000,
model_as_dict: bool = False,
enabled: str = 'enabled', # enabled, disabled, all
inc_archive_content_list: bool = False,
) -> 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 = {}
data['account_id'] = account_id
if enabled in ['enabled', 'disabled', 'all']:
if enabled == 'enabled':
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 = ''
# else: tbl_obj['account'] = None
if limit:
data['limit'] = limit
sql_limit = f'LIMIT :limit'
else:
sql_limit = ''
sql = f"""
SELECT `tbl`.id AS 'archive_id', `tbl`.id_random AS 'archive_id_random'
FROM `archive` AS `tbl`
WHERE `tbl`.account_id = :account_id
{sql_enabled}
ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC
{sql_limit};
"""
if archive_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(archive_rec_li_result)
archive_result_li = []
for archive_rec in archive_rec_li_result:
archive_id = archive_rec.get('archive_id', None)
if archive_result := load_archive_obj(
archive_id=archive_id,
model_as_dict=model_as_dict,
enabled=enabled,
inc_archive_content_list=inc_archive_content_list,
):
log.debug(archive_result)
archive_result_li.append(archive_result)
else:
log.debug(archive_result)
archive_result_li.append(None)
log.debug(archive_result_li)
else:
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
log.debug(archive_rec_li_result)
archive_result_li = []
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
return archive_result_li
# ### END ### API Archive Methods ### load_archive_obj_list() ###

View File

@@ -0,0 +1,41 @@
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.models.fundraising_cfg_models import Fundraising_Cfg_Base
# ### BEGIN ### API Fundraising Cfg Methods ### load_fundraising_cfg_obj() ###
def load_fundraising_cfg_obj(
account_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 account_id := redis_lookup_id_random(record_id_random=account_id, table_name='account'): pass
else: return False
if fundraising_cfg_rec := sql_select(
table_name = 'v_fundraising_cfg',
field_name = 'account_id',
field_value = account_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() ###

View File

@@ -0,0 +1,41 @@
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.models.membership_cfg_models import Membership_Cfg_Base
# ### BEGIN ### API Membership Cfg Methods ### load_membership_cfg_obj() ###
def load_membership_cfg_obj(
account_id: int|str,
model_as_dict: bool = False,
) -> Membership_Cfg_Base|dict|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
if membership_cfg_rec := sql_select(
table_name = 'v_membership_cfg',
field_name = 'account_id',
field_value = account_id
): pass
else: return False
log.debug(membership_cfg_rec)
try:
membership_cfg_obj = Membership_Cfg_Base(**membership_cfg_rec)
log.debug(membership_cfg_obj)
except ValidationError as e:
log.error(e.json())
if model_as_dict:
return membership_cfg_obj.dict(by_alias=True, exclude_unset=True) # pylint: disable=no-member
else:
return membership_cfg_obj
# ### END ### API Membership Cfg Methods ### load_membership_cfg_obj() ###