Adding activity_log routes, methods, and models.
This commit is contained in:
97
app/methods/activity_log_methods.py
Normal file
97
app/methods/activity_log_methods.py
Normal file
@@ -0,0 +1,97 @@
|
||||
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_insert_or_update, sql_select, sql_update
|
||||
from app.lib_general import log, logging
|
||||
|
||||
|
||||
from app.models.common_field_schema import default_num_bytes
|
||||
from app.models.activity_log_models import Activity_Log_Base
|
||||
|
||||
|
||||
# ### BEGIN ### API Activity Log Methods ### load_activity_log_obj() ###
|
||||
def load_activity_log_obj(
|
||||
activity_log_id: int|str,
|
||||
limit: int = 10000,
|
||||
by_alias: bool = True,
|
||||
exclude_unset: bool = True,
|
||||
model_as_dict: bool = False,
|
||||
) -> Activity_Log_Base|bool:
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(locals())
|
||||
|
||||
if activity_log_id := redis_lookup_id_random(record_id_random=activity_log_id, table_name='activity_log'): pass
|
||||
else: return False
|
||||
|
||||
if activity_log_rec := sql_select(table_name='v_activity_log', record_id=activity_log_id): pass
|
||||
else: return False
|
||||
|
||||
try:
|
||||
activity_log_obj = Activity_Log_Base(**activity_log_rec)
|
||||
log.debug(activity_log_obj)
|
||||
except ValidationError as e:
|
||||
log.error(e.json())
|
||||
|
||||
if model_as_dict:
|
||||
return activity_log_obj.dict(by_alias=by_alias, exclude_unset=exclude_unset) # pylint: disable=no-member
|
||||
else:
|
||||
return activity_log_obj
|
||||
# ### END ### API Activity Log Methods ### load_activity_log_obj() ###
|
||||
|
||||
|
||||
# ### BEGIN ### API Activity Log Methods ### get_activity_log_rec_list() ###
|
||||
def get_activity_log_rec_list(
|
||||
account_id: str,
|
||||
from_datetime: datetime.datetime = None,
|
||||
to_datetime: datetime.datetime = 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
|
||||
else: return False
|
||||
data = {}
|
||||
data['account_id'] = account_id
|
||||
sql_account_id = f'`activity_log`.account_id = :account_id'
|
||||
|
||||
# if enabled in ['enabled', 'disabled', 'all']:
|
||||
# if enabled == 'enabled':
|
||||
# data['enable'] = True
|
||||
# sql_enabled = f'AND `activity_log`.enable = :enable'
|
||||
# elif enabled == 'disabled':
|
||||
# data['enable'] = False
|
||||
# sql_enabled = f'AND `activity_log`.enable = :enable'
|
||||
# elif enabled == 'all':
|
||||
# sql_enabled = ''
|
||||
sql_enabled = ''
|
||||
|
||||
if limit:
|
||||
data['limit'] = limit
|
||||
sql_limit = f'LIMIT :limit'
|
||||
else:
|
||||
sql_limit = ''
|
||||
|
||||
sql = f"""
|
||||
SELECT `activity_log`.id AS 'activity_log_id', `activity_log`.id_random AS 'activity_log_id_random'
|
||||
FROM `activity_log` AS `activity_log`
|
||||
WHERE
|
||||
{sql_account_id}
|
||||
{sql_enabled}
|
||||
ORDER BY `activity_log`.created_on DESC, `activity_log`.updated_on DESC
|
||||
{sql_limit};
|
||||
"""
|
||||
|
||||
if activity_log_rec_li_result := sql_select(data=data, sql=sql, as_list=True):
|
||||
activity_log_rec_li = activity_log_rec_li_result
|
||||
else:
|
||||
activity_log_rec_li = []
|
||||
log.setLevel(logging.DEBUG) # DEBUG, INFO, WARNING, ERROR, EXCEPTION, CRITICAL
|
||||
log.debug(activity_log_rec_li_result)
|
||||
|
||||
return activity_log_rec_li
|
||||
# ### END ### API Activity Log Methods ### get_activity_log_rec_list() ###
|
||||
Reference in New Issue
Block a user