Pre code cleanup and documentation.
This commit is contained in:
@@ -69,6 +69,6 @@ def update_log(mapper, connection, target):
|
|||||||
else:
|
else:
|
||||||
logger.info(f"No changes detected, not updating logs.")
|
logger.info(f"No changes detected, not updating logs.")
|
||||||
|
|
||||||
|
if ctx.database_schema == "sqlite":
|
||||||
# event.listen(LogMixin, 'after_update', update_log, propagate=True)
|
event.listen(LogMixin, 'after_update', update_log, propagate=True)
|
||||||
# event.listen(LogMixin, 'after_insert', update_log, propagate=True)
|
event.listen(LogMixin, 'after_insert', update_log, propagate=True)
|
||||||
|
|||||||
@@ -4,8 +4,8 @@ Contains all models for sqlalchemy
|
|||||||
from __future__ import annotations
|
from __future__ import annotations
|
||||||
import sys, logging
|
import sys, logging
|
||||||
|
|
||||||
import pandas as pd
|
from pandas import DataFrame
|
||||||
from sqlalchemy import Column, INTEGER, String, JSON, inspect
|
from sqlalchemy import Column, INTEGER, String, JSON
|
||||||
from sqlalchemy.orm import DeclarativeMeta, declarative_base, Query, Session
|
from sqlalchemy.orm import DeclarativeMeta, declarative_base, Query, Session
|
||||||
from sqlalchemy.ext.declarative import declared_attr
|
from sqlalchemy.ext.declarative import declared_attr
|
||||||
from sqlalchemy.exc import ArgumentError
|
from sqlalchemy.exc import ArgumentError
|
||||||
@@ -100,11 +100,21 @@ class BaseClass(Base):
|
|||||||
Returns:
|
Returns:
|
||||||
dict | list | str: Output of key:value dict or single (list, str) desired variable
|
dict | list | str: Output of key:value dict or single (list, str) desired variable
|
||||||
"""
|
"""
|
||||||
|
# NOTE: singles is a list of fields that need to be limited to 1 result.
|
||||||
singles = list(set(cls.singles + BaseClass.singles))
|
singles = list(set(cls.singles + BaseClass.singles))
|
||||||
return dict(singles=singles)
|
return dict(singles=singles)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def find_regular_subclass(cls, name: str | None = None):
|
def find_regular_subclass(cls, name: str | None = None) -> Any:
|
||||||
|
"""
|
||||||
|
|
||||||
|
Args:
|
||||||
|
name (str): name of subclass of interest.
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
Any: Subclass of this object
|
||||||
|
|
||||||
|
"""
|
||||||
if not name:
|
if not name:
|
||||||
return cls
|
return cls
|
||||||
if " " in name:
|
if " " in name:
|
||||||
@@ -140,7 +150,7 @@ class BaseClass(Base):
|
|||||||
return query.limit(50).all()
|
return query.limit(50).all()
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def results_to_df(cls, objects: list, **kwargs) -> pd.DataFrame:
|
def results_to_df(cls, objects: list, **kwargs) -> DataFrame:
|
||||||
"""
|
"""
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
@@ -148,15 +158,15 @@ class BaseClass(Base):
|
|||||||
**kwargs (): Arguments necessary for the to_sub_dict method. eg extraction_kit=X
|
**kwargs (): Arguments necessary for the to_sub_dict method. eg extraction_kit=X
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
pd.Dataframe
|
Dataframe
|
||||||
"""
|
"""
|
||||||
records = [obj.to_sub_dict(**kwargs) for obj in objects]
|
records = [obj.to_sub_dict(**kwargs) for obj in objects]
|
||||||
return pd.DataFrame.from_records(records)
|
return DataFrame.from_records(records)
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def query(cls, **kwargs) -> Any | List[Any]:
|
def query(cls, **kwargs) -> Any | List[Any]:
|
||||||
"""
|
"""
|
||||||
Default query function for models. Overridden in most models.
|
Default query function for models. Overridden in most models with additional filters.
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
Any | List[Any]: Result of query execution.
|
Any | List[Any]: Result of query execution.
|
||||||
@@ -209,11 +219,9 @@ class BaseClass(Base):
|
|||||||
"""
|
"""
|
||||||
# logger.debug(f"Saving object: {pformat(self.__dict__)}")
|
# logger.debug(f"Saving object: {pformat(self.__dict__)}")
|
||||||
report = Report()
|
report = Report()
|
||||||
state = inspect(self)
|
|
||||||
try:
|
try:
|
||||||
self.__database_session__.add(self)
|
self.__database_session__.add(self)
|
||||||
self.__database_session__.commit()
|
self.__database_session__.commit()
|
||||||
return state
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
logger.critical(f"Problem saving object: {e}")
|
logger.critical(f"Problem saving object: {e}")
|
||||||
logger.error(f"Error message: {type(e)}")
|
logger.error(f"Error message: {type(e)}")
|
||||||
|
|||||||
@@ -1,3 +1,8 @@
|
|||||||
|
"""
|
||||||
|
Contains the audit log class and functions.
|
||||||
|
"""
|
||||||
|
from typing import List
|
||||||
|
|
||||||
from dateutil.parser import parse
|
from dateutil.parser import parse
|
||||||
from sqlalchemy.orm import declarative_base, DeclarativeMeta, Query
|
from sqlalchemy.orm import declarative_base, DeclarativeMeta, Query
|
||||||
from . import BaseClass
|
from . import BaseClass
|
||||||
@@ -13,7 +18,7 @@ class AuditLog(Base):
|
|||||||
|
|
||||||
__tablename__ = "_auditlog"
|
__tablename__ = "_auditlog"
|
||||||
|
|
||||||
id = Column(INTEGER, primary_key=True) #: primary key
|
id = Column(INTEGER, primary_key=True, autoincrement=True) #: primary key
|
||||||
user = Column(String(64))
|
user = Column(String(64))
|
||||||
time = Column(TIMESTAMP)
|
time = Column(TIMESTAMP)
|
||||||
object = Column(String(64))
|
object = Column(String(64))
|
||||||
@@ -23,7 +28,17 @@ class AuditLog(Base):
|
|||||||
return f"<{self.user} @ {self.time}>"
|
return f"<{self.user} @ {self.time}>"
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def query(cls, start_date: date | str | int | None = None, end_date: date | str | int | None = None, ):
|
def query(cls, start_date: date | str | int | None = None, end_date: date | str | int | None = None) -> List["AuditLog"]:
|
||||||
|
"""
|
||||||
|
Searches for database transactions by date.
|
||||||
|
|
||||||
|
Args:
|
||||||
|
start_date (date | str | int | None, Optional): Earliest date sought. Defaults to None
|
||||||
|
end_date (date | str | int | None, Optional): Latest date sought. Defaults to None
|
||||||
|
|
||||||
|
Returns:
|
||||||
|
List[AuditLog]: List of transactions made to the database.
|
||||||
|
"""
|
||||||
session = BaseClass.__database_session__
|
session = BaseClass.__database_session__
|
||||||
query: Query = session.query(cls)
|
query: Query = session.query(cls)
|
||||||
if start_date is not None and end_date is None:
|
if start_date is not None and end_date is None:
|
||||||
|
|||||||
@@ -401,7 +401,7 @@ class ReagentRole(BaseClass):
|
|||||||
super().save()
|
super().save()
|
||||||
|
|
||||||
|
|
||||||
class Reagent(BaseClass):
|
class Reagent(BaseClass, LogMixin):
|
||||||
"""
|
"""
|
||||||
Concrete reagent instance
|
Concrete reagent instance
|
||||||
"""
|
"""
|
||||||
|
|||||||
@@ -955,10 +955,6 @@ def report_result(func):
|
|||||||
match output:
|
match output:
|
||||||
case Report():
|
case Report():
|
||||||
report = output
|
report = output
|
||||||
# case InstanceState():
|
|
||||||
# for attr in output.attrs:
|
|
||||||
# print(f"{attr}: {attr.load_history()}")
|
|
||||||
# return
|
|
||||||
case tuple():
|
case tuple():
|
||||||
try:
|
try:
|
||||||
report = [item for item in output if isinstance(item, Report)][0]
|
report = [item for item in output if isinstance(item, Report)][0]
|
||||||
|
|||||||
Reference in New Issue
Block a user