Working on all the routes, methods, and models.
This commit is contained in:
157
app/methods/journal_entry_methods.py
Normal file
157
app/methods/journal_entry_methods.py
Normal file
@@ -0,0 +1,157 @@
|
||||
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
|
||||
|
||||
# from app.methods.journal_methods import load_journal_entry_obj
|
||||
|
||||
from app.models.journal_entry_models import Journal_Entry_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Journal Entry Methods ### create_journal_entry_obj() ###
|
||||
def create_journal_entry_obj(journal_entry_obj_new:Journal_Entry_Base) -> bool|int:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
journal_entry_obj_data = journal_entry_obj_new.dict(by_alias=False, exclude_defaults=False, exclude_unset=True, exclude={'created_on', 'updated_on'})
|
||||
|
||||
if journal_entry_obj_in_result := sql_insert(
|
||||
data=journal_entry_obj_data,
|
||||
table_name='journal_entry',
|
||||
rm_id_random=True,
|
||||
id_random_length=8
|
||||
): pass
|
||||
else: return False
|
||||
|
||||
log.debug(journal_entry_obj_in_result)
|
||||
|
||||
journal_entry_id = journal_entry_obj_in_result
|
||||
|
||||
log.debug(f'New journal_entry_id: {journal_entry_id}')
|
||||
return journal_entry_id
|
||||
# ### END ### API Journal Entry Methods ### create_journal_entry_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Journal Entry Methods ### load_journal_entry_obj() ###
|
||||
def load_journal_entry_obj(
|
||||
journal_entry_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
|
||||
inc_journal_entry_list: bool = False,
|
||||
inc_person: bool = False,
|
||||
inc_user: bool = False,
|
||||
) -> Journal_Entry_Base|bool:
|
||||
log.setLevel(logging.WARNING) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if journal_entry_id := redis_lookup_id_random(record_id_random=journal_entry_id, table_name='journal_entry'): pass
|
||||
else: return False
|
||||
log.debug(journal_entry_id)
|
||||
|
||||
if journal_entry_rec := sql_select(table_name='v_journal_entry', record_id=journal_entry_id):
|
||||
log.debug(journal_entry_rec)
|
||||
else: return False
|
||||
log.debug(journal_entry_rec)
|
||||
|
||||
try:
|
||||
journal_entry_obj = Journal_Entry_Base(**journal_entry_rec)
|
||||
log.debug(journal_entry_obj)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
|
||||
if model_as_dict:
|
||||
return journal_entry_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member
|
||||
else:
|
||||
return journal_entry_obj
|
||||
# ### END ### API Journal Entry Methods ### load_journal_entry_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Journal Entry Methods ### update_journal_entry_obj() ###
|
||||
def update_journal_entry_obj(
|
||||
journal_entry_id: int|str, # This allows for updating of the id_random value.
|
||||
journal_entry_obj_up: Journal_Entry_Base,
|
||||
create_missing_obj: bool = False,
|
||||
) -> bool:
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if journal_entry_id := redis_lookup_id_random(record_id_random=journal_entry_id, table_name='journal_entry'): pass
|
||||
else: return False
|
||||
|
||||
journal_entry_obj_up.id = journal_entry_id
|
||||
|
||||
log.debug(journal_entry_obj_up)
|
||||
log.debug(journal_entry_obj_up.dict(by_alias=False, exclude_unset=True))
|
||||
# log.debug(journal_entry_obj_up.dict(by_alias=False, exclude_unset=False))
|
||||
|
||||
journal_dict_up = journal_entry_obj_up.dict(by_alias=False, exclude_unset=True)
|
||||
log.debug(journal_dict_up)
|
||||
|
||||
if journal_entry_obj_up_result := sql_update(data=journal_dict_up, table_name='journal_entry', rm_id_random=True):
|
||||
log.debug(journal_entry_obj_up_result)
|
||||
return True
|
||||
else:
|
||||
log.debug(journal_entry_obj_up_result)
|
||||
return False
|
||||
# ### END ### API Journal Entry Methods ### update_journal_entry_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Journal Entry Methods ### get_journal_entry_rec_list() ###
|
||||
def get_journal_entry_rec_list(
|
||||
for_obj_type: str,
|
||||
for_obj_id: str,
|
||||
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 for_obj_id := redis_lookup_id_random(record_id_random=for_obj_id, table_name='for_obj_type'): pass
|
||||
else: return False
|
||||
data = {}
|
||||
data[f'{for_obj_type}_id'] = for_obj_id
|
||||
# data['for_obj_type'] = for_obj_type
|
||||
sql_obj_type_id = f'`tbl`.{for_obj_type}_id = :{for_obj_type}_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 = ''
|
||||
|
||||
if limit:
|
||||
data['limit'] = limit
|
||||
sql_limit = f'LIMIT :limit'
|
||||
else:
|
||||
sql_limit = ''
|
||||
|
||||
sql = f"""
|
||||
SELECT `tbl`.id AS 'journal_entry_id', `tbl`.id_random AS 'journal_entry_id_random'
|
||||
FROM `journal_entry` AS `tbl`
|
||||
WHERE
|
||||
{sql_obj_type_id}
|
||||
{sql_enabled}
|
||||
ORDER BY `tbl`.created_on DESC, `tbl`.updated_on DESC
|
||||
{sql_limit};
|
||||
"""
|
||||
|
||||
if journal_entry_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
|
||||
journal_entry_rec_li = journal_entry_rec_li_result
|
||||
else:
|
||||
journal_entry_rec_li = []
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(journal_entry_rec_li_result)
|
||||
|
||||
return journal_entry_rec_li
|
||||
# ### END ### API Journal Entry Methods ### get_journal_entry_rec_list() ###
|
||||
Reference in New Issue
Block a user