Middle of the road.
This commit is contained in:
@@ -8,7 +8,7 @@ from pydantic import BaseModel
|
||||
from sqlalchemy import Column, INTEGER, String, JSON
|
||||
from sqlalchemy.orm import DeclarativeMeta, declarative_base, Query, Session, InstrumentedAttribute
|
||||
from sqlalchemy.ext.declarative import declared_attr
|
||||
from sqlalchemy.exc import ArgumentError
|
||||
from sqlalchemy.exc import ArgumentError, InvalidRequestError
|
||||
from typing import Any, List
|
||||
from pathlib import Path
|
||||
from tools import report_result, list_sort_dict
|
||||
@@ -48,7 +48,7 @@ class BaseClass(Base):
|
||||
__table_args__ = {'extend_existing': True} #: Will only add new columns
|
||||
|
||||
singles = ['id']
|
||||
omni_removes = ['submissions']
|
||||
omni_removes = ['submissions', "omnigui_class_dict", "omnigui_instance_dict"]
|
||||
omni_sort = ["name"]
|
||||
|
||||
@classproperty
|
||||
@@ -60,7 +60,7 @@ class BaseClass(Base):
|
||||
|
||||
@classproperty
|
||||
def aliases(cls):
|
||||
return [cls.__name__.lower()]
|
||||
return [cls.query_alias]
|
||||
|
||||
@classproperty
|
||||
def level(cls):
|
||||
@@ -198,7 +198,7 @@ class BaseClass(Base):
|
||||
try:
|
||||
records = [obj.to_sub_dict(**kwargs) for obj in objects]
|
||||
except AttributeError:
|
||||
records = [{k:v['instance_attr'] for k, v in obj.to_omnigui_dict(**kwargs).items()} for obj in objects]
|
||||
records = [{k:v['instance_attr'] for k, v in obj.omnigui_instance_dict.items()} for obj in objects]
|
||||
return DataFrame.from_records(records)
|
||||
|
||||
@classmethod
|
||||
@@ -233,7 +233,11 @@ class BaseClass(Base):
|
||||
logger.info(f"Using key: {k} with value: {v}")
|
||||
try:
|
||||
attr = getattr(model, k)
|
||||
query = query.filter(attr == v)
|
||||
# NOTE: account for attrs that use list.
|
||||
if attr.property.uselist:
|
||||
query = query.filter(attr.contains(v))
|
||||
else:
|
||||
query = query.filter(attr == v)
|
||||
except (ArgumentError, AttributeError) as e:
|
||||
logger.error(f"Attribute {k} unavailable due to:\n\t{e}\nSkipping.")
|
||||
if k in singles:
|
||||
@@ -265,14 +269,13 @@ class BaseClass(Base):
|
||||
return report
|
||||
|
||||
@property
|
||||
def omnigui_dict(self) -> dict:
|
||||
def omnigui_instance_dict(self) -> dict:
|
||||
"""
|
||||
For getting any object in an omni-thing friendly output.
|
||||
|
||||
Returns:
|
||||
dict: Dictionary of object minus _sa_instance_state with id at the front.
|
||||
"""
|
||||
# dicto = {k: v for k, v in self.__dict__.items() if k not in ["_sa_instance_state"]}
|
||||
dicto = {key: dict(class_attr=getattr(self.__class__, key), instance_attr=getattr(self, key))
|
||||
for key in dir(self.__class__) if
|
||||
isinstance(getattr(self.__class__, key), InstrumentedAttribute) and key not in self.omni_removes
|
||||
@@ -306,7 +309,7 @@ class BaseClass(Base):
|
||||
model = getattr(pydant, f"Pyd{cls.__name__}")
|
||||
except AttributeError:
|
||||
logger.warning(f"Couldn't get {cls.__name__} pydantic model.")
|
||||
return None
|
||||
return pydant.PydElastic
|
||||
return model
|
||||
|
||||
@classproperty
|
||||
@@ -324,6 +327,32 @@ class BaseClass(Base):
|
||||
query_kwargs = {relationship_instance.query_alias:relationship_instance}
|
||||
return cls.query(**query_kwargs)
|
||||
|
||||
def __setattr__(self, key, value):
|
||||
try:
|
||||
field_type = getattr(self.__class__, key)
|
||||
except AttributeError:
|
||||
return super().__setattr__(key, value)
|
||||
try:
|
||||
check = field_type.property.uselist
|
||||
except AttributeError:
|
||||
check = False
|
||||
if check:
|
||||
logger.debug(f"Setting with uselist")
|
||||
if self.__getattribute__(key) is not None:
|
||||
if isinstance(value, list):
|
||||
value = self.__getattribute__(key) + value
|
||||
else:
|
||||
value = self.__getattribute__(key) + [value]
|
||||
else:
|
||||
value = [value]
|
||||
else:
|
||||
if isinstance(value, list):
|
||||
if len(value) == 1:
|
||||
value = value[0]
|
||||
else:
|
||||
raise ValueError("Object is too long to parse a single value.")
|
||||
return super().__setattr__(key, value)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -146,7 +146,7 @@ class Control(BaseClass):
|
||||
@classmethod
|
||||
@setup_lookup
|
||||
def query(cls,
|
||||
submission_type: str | None = None,
|
||||
submissiontype: str | None = None,
|
||||
subtype: str | None = None,
|
||||
start_date: date | str | int | None = None,
|
||||
end_date: date | str | int | None = None,
|
||||
@@ -169,13 +169,13 @@ class Control(BaseClass):
|
||||
"""
|
||||
from backend.db import SubmissionType
|
||||
query: Query = cls.__database_session__.query(cls)
|
||||
match submission_type:
|
||||
match submissiontype:
|
||||
case str():
|
||||
from backend import BasicSubmission, SubmissionType
|
||||
query = query.join(BasicSubmission).join(SubmissionType).filter(SubmissionType.name == submission_type)
|
||||
query = query.join(BasicSubmission).join(SubmissionType).filter(SubmissionType.name == submissiontype)
|
||||
case SubmissionType():
|
||||
from backend import BasicSubmission
|
||||
query = query.join(BasicSubmission).filter(BasicSubmission.submission_type_name == submission_type.name)
|
||||
query = query.join(BasicSubmission).filter(BasicSubmission.submission_type_name == submissiontype.name)
|
||||
case _:
|
||||
pass
|
||||
# NOTE: by control type
|
||||
@@ -347,7 +347,7 @@ class PCRControl(Control):
|
||||
parent.mode_typer.clear()
|
||||
parent.mode_typer.setEnabled(False)
|
||||
report = Report()
|
||||
controls = cls.query(submission_type=chart_settings['sub_type'], start_date=chart_settings['start_date'],
|
||||
controls = cls.query(submissiontype=chart_settings['sub_type'], start_date=chart_settings['start_date'],
|
||||
end_date=chart_settings['end_date'])
|
||||
data = [control.to_sub_dict() for control in controls]
|
||||
df = DataFrame.from_records(data)
|
||||
|
||||
@@ -96,7 +96,8 @@ class KitType(BaseClass):
|
||||
Base of kits used in submission processing
|
||||
"""
|
||||
|
||||
query_alias = "kit_type"
|
||||
# query_alias = "kit_type"
|
||||
omni_sort = BaseClass.omni_sort + ["kit_submissiontype_associations", "kit_reagentrole_associations", "processes"]
|
||||
|
||||
id = Column(INTEGER, primary_key=True) #: primary key
|
||||
name = Column(String(64), unique=True) #: name of kit
|
||||
@@ -134,7 +135,12 @@ class KitType(BaseClass):
|
||||
|
||||
@classproperty
|
||||
def aliases(cls):
|
||||
return super().aliases + [cls.query_alias, "kit_types"]
|
||||
return super().aliases + [cls.query_alias, "kit_types", "kit_type"]
|
||||
|
||||
@hybrid_property
|
||||
def submissiontype(self):
|
||||
"""Alias used_for field to allow query with SubmissionType query alias"""
|
||||
return self.used_for
|
||||
|
||||
def get_reagents(self,
|
||||
required: bool = False,
|
||||
@@ -227,7 +233,8 @@ class KitType(BaseClass):
|
||||
name: str = None,
|
||||
used_for: str | SubmissionType | None = None,
|
||||
id: int | None = None,
|
||||
limit: int = 0
|
||||
limit: int = 0,
|
||||
**kwargs
|
||||
) -> KitType | List[KitType]:
|
||||
"""
|
||||
Lookup a list of or single KitType.
|
||||
@@ -264,7 +271,7 @@ class KitType(BaseClass):
|
||||
limit = 1
|
||||
case _:
|
||||
pass
|
||||
return cls.execute_query(query=query, limit=limit)
|
||||
return cls.execute_query(query=query, limit=limit, **kwargs)
|
||||
|
||||
@check_authorization
|
||||
def save(self):
|
||||
@@ -407,10 +414,11 @@ class ReagentRole(BaseClass):
|
||||
@setup_lookup
|
||||
def query(cls,
|
||||
name: str | None = None,
|
||||
kit_type: KitType | str | None = None,
|
||||
kittype: KitType | str | None = None,
|
||||
reagent: Reagent | str | None = None,
|
||||
id: int | None = None,
|
||||
limit: int = 0,
|
||||
**kwargs
|
||||
) -> ReagentRole | List[ReagentRole]:
|
||||
"""
|
||||
Lookup reagent types in the database.
|
||||
@@ -429,14 +437,14 @@ class ReagentRole(BaseClass):
|
||||
ReagentRole|List[ReagentRole]: ReagentRole or list of ReagentRoles matching filter.
|
||||
"""
|
||||
query: Query = cls.__database_session__.query(cls)
|
||||
if (kit_type is not None and reagent is None) or (reagent is not None and kit_type is None):
|
||||
if (kittype is not None and reagent is None) or (reagent is not None and kittype is None):
|
||||
raise ValueError("Cannot filter without both reagent and kit type.")
|
||||
elif kit_type is None and reagent is None:
|
||||
elif kittype is None and reagent is None:
|
||||
pass
|
||||
else:
|
||||
match kit_type:
|
||||
match kittype:
|
||||
case str():
|
||||
kit_type = KitType.query(name=kit_type)
|
||||
kittype = KitType.query(name=kittype)
|
||||
case _:
|
||||
pass
|
||||
match reagent:
|
||||
@@ -446,7 +454,7 @@ class ReagentRole(BaseClass):
|
||||
pass
|
||||
assert reagent.role
|
||||
# NOTE: Get all roles common to the reagent and the kit.
|
||||
result = set(kit_type.reagent_roles).intersection(reagent.role)
|
||||
result = set(kittype.reagent_roles).intersection(reagent.role)
|
||||
return next((item for item in result), None)
|
||||
match name:
|
||||
case str():
|
||||
@@ -491,8 +499,6 @@ class Reagent(BaseClass, LogMixin):
|
||||
Concrete reagent instance
|
||||
"""
|
||||
|
||||
|
||||
|
||||
id = Column(INTEGER, primary_key=True) #: primary key
|
||||
role = relationship("ReagentRole", back_populates="instances",
|
||||
secondary=reagentroles_reagents) #: joined parent reagent type
|
||||
@@ -523,6 +529,11 @@ class Reagent(BaseClass, LogMixin):
|
||||
def searchables(cls):
|
||||
return [dict(label="Lot", field="lot")]
|
||||
|
||||
@hybrid_property
|
||||
def reagentrole(self):
|
||||
"""Alias role field to allow query with ReagentRole query alias"""
|
||||
return self.role
|
||||
|
||||
def to_sub_dict(self, extraction_kit: KitType = None, full_data: bool = False, **kwargs) -> dict:
|
||||
"""
|
||||
dictionary containing values necessary for gui
|
||||
@@ -583,9 +594,9 @@ class Reagent(BaseClass, LogMixin):
|
||||
Report: Result of operation
|
||||
"""
|
||||
report = Report()
|
||||
rt = ReagentRole.query(kit_type=kit, reagent=self, limit=1)
|
||||
rt = ReagentRole.query(kittype=kit, reagent=self, limit=1)
|
||||
if rt is not None:
|
||||
assoc = KitTypeReagentRoleAssociation.query(kit_type=kit, reagent_role=rt)
|
||||
assoc = KitTypeReagentRoleAssociation.query(kittype=kit, reagentrole=rt)
|
||||
if assoc is not None:
|
||||
if assoc.last_used != self.lot:
|
||||
assoc.last_used = self.lot
|
||||
@@ -621,7 +632,8 @@ class Reagent(BaseClass, LogMixin):
|
||||
role: str | ReagentRole | None = None,
|
||||
lot: str | None = None,
|
||||
name: str | None = None,
|
||||
limit: int = 0
|
||||
limit: int = 0,
|
||||
**kwargs
|
||||
) -> Reagent | List[Reagent]:
|
||||
"""
|
||||
Lookup a list of reagents from the database.
|
||||
@@ -643,6 +655,8 @@ class Reagent(BaseClass, LogMixin):
|
||||
limit = 1
|
||||
case _:
|
||||
pass
|
||||
# if not role and "reagentrole" in kwargs.keys():
|
||||
# role = kwargs['reagentrole']
|
||||
match role:
|
||||
case str():
|
||||
query = query.join(cls.role).filter(ReagentRole.name == role)
|
||||
@@ -663,7 +677,7 @@ class Reagent(BaseClass, LogMixin):
|
||||
limit = 1
|
||||
case _:
|
||||
pass
|
||||
return cls.execute_query(query=query, limit=limit)
|
||||
return cls.execute_query(query=query, limit=limit, **kwargs)
|
||||
|
||||
def set_attribute(self, key, value):
|
||||
match key:
|
||||
@@ -741,7 +755,7 @@ class Discount(BaseClass):
|
||||
@setup_lookup
|
||||
def query(cls,
|
||||
organization: Organization | str | int | None = None,
|
||||
kit_type: KitType | str | int | None = None,
|
||||
kittype: KitType | str | int | None = None,
|
||||
) -> Discount | List[Discount]:
|
||||
"""
|
||||
Lookup discount objects (union of kit and organization)
|
||||
@@ -763,13 +777,13 @@ class Discount(BaseClass):
|
||||
query = query.join(Organization).filter(Organization.id == organization)
|
||||
case _:
|
||||
pass
|
||||
match kit_type:
|
||||
match kittype:
|
||||
case KitType():
|
||||
query = query.filter(cls.kit == kit_type)
|
||||
query = query.filter(cls.kit == kittype)
|
||||
case str():
|
||||
query = query.join(KitType).filter(KitType.name == kit_type)
|
||||
query = query.join(KitType).filter(KitType.name == kittype)
|
||||
case int():
|
||||
query = query.join(KitType).filter(KitType.id == kit_type)
|
||||
query = query.join(KitType).filter(KitType.id == kittype)
|
||||
case _:
|
||||
pass
|
||||
return cls.execute_query(query=query)
|
||||
@@ -833,6 +847,14 @@ class SubmissionType(BaseClass):
|
||||
"""
|
||||
return f"<SubmissionType({self.name})>"
|
||||
|
||||
@hybrid_property
|
||||
def kittype(self):
|
||||
return self.kit_types
|
||||
|
||||
@hybrid_property
|
||||
def process(self):
|
||||
return self.processes
|
||||
|
||||
@classproperty
|
||||
def aliases(cls):
|
||||
return super().aliases + ["submission_types", "submission_type"]
|
||||
@@ -990,7 +1012,8 @@ class SubmissionType(BaseClass):
|
||||
def query(cls,
|
||||
name: str | None = None,
|
||||
key: str | None = None,
|
||||
limit: int = 0
|
||||
limit: int = 0,
|
||||
**kwargs
|
||||
) -> SubmissionType | List[SubmissionType]:
|
||||
"""
|
||||
Lookup submission type in the database by a number of parameters
|
||||
@@ -1124,7 +1147,23 @@ class SubmissionTypeKitTypeAssociation(BaseClass):
|
||||
Returns:
|
||||
str: Representation of this object
|
||||
"""
|
||||
return f"<SubmissionTypeKitTypeAssociation({self.submission_type.name}&{self.kit_type.name})>"
|
||||
try:
|
||||
submission_type_name = self.submission_type.name
|
||||
except AttributeError:
|
||||
submission_type_name = "None"
|
||||
try:
|
||||
kit_type_name = self.kit_type.name
|
||||
except AttributeError:
|
||||
kit_type_name = "None"
|
||||
return f"<SubmissionTypeKitTypeAssociation({submission_type_name}&{kit_type_name})>"
|
||||
|
||||
@hybrid_property
|
||||
def kittype(self):
|
||||
return self.kit_type
|
||||
|
||||
@hybrid_property
|
||||
def submissiontype(self):
|
||||
return self.submission_type
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
@@ -1133,8 +1172,8 @@ class SubmissionTypeKitTypeAssociation(BaseClass):
|
||||
@classmethod
|
||||
@setup_lookup
|
||||
def query(cls,
|
||||
submission_type: SubmissionType | str | int | None = None,
|
||||
kit_type: KitType | str | int | None = None,
|
||||
submissiontype: SubmissionType | str | int | None = None,
|
||||
kittype: KitType | str | int | None = None,
|
||||
limit: int = 0,
|
||||
**kwargs
|
||||
) -> SubmissionTypeKitTypeAssociation | List[SubmissionTypeKitTypeAssociation]:
|
||||
@@ -1150,21 +1189,21 @@ class SubmissionTypeKitTypeAssociation(BaseClass):
|
||||
SubmissionTypeKitTypeAssociation|List[SubmissionTypeKitTypeAssociation]: SubmissionTypeKitTypeAssociation(s) of interest
|
||||
"""
|
||||
query: Query = cls.__database_session__.query(cls)
|
||||
match submission_type:
|
||||
match submissiontype:
|
||||
case SubmissionType():
|
||||
query = query.filter(cls.submission_type == submission_type)
|
||||
query = query.filter(cls.submission_type == submissiontype)
|
||||
case str():
|
||||
query = query.join(SubmissionType).filter(SubmissionType.name == submission_type)
|
||||
query = query.join(SubmissionType).filter(SubmissionType.name == submissiontype)
|
||||
case int():
|
||||
query = query.join(SubmissionType).filter(SubmissionType.id == submission_type)
|
||||
match kit_type:
|
||||
query = query.join(SubmissionType).filter(SubmissionType.id == submissiontype)
|
||||
match kittype:
|
||||
case KitType():
|
||||
query = query.filter(cls.kit_type == kit_type)
|
||||
query = query.filter(cls.kit_type == kittype)
|
||||
case str():
|
||||
query = query.join(KitType).filter(KitType.name == kit_type)
|
||||
query = query.join(KitType).filter(KitType.name == kittype)
|
||||
case int():
|
||||
query = query.join(KitType).filter(KitType.id == kit_type)
|
||||
if kit_type is not None and submission_type is not None:
|
||||
query = query.join(KitType).filter(KitType.id == kittype)
|
||||
if kittype is not None and submissiontype is not None:
|
||||
limit = 1
|
||||
# limit = query.count()
|
||||
return cls.execute_query(query=query, limit=limit)
|
||||
@@ -1221,7 +1260,10 @@ class KitTypeReagentRoleAssociation(BaseClass):
|
||||
|
||||
@property
|
||||
def name(self):
|
||||
return f"{self.kit_type.name} -> {self.reagent_role.name}"
|
||||
try:
|
||||
return f"{self.kit_type.name} -> {self.reagent_role.name}"
|
||||
except AttributeError:
|
||||
return "Blank KitTypeReagentRole"
|
||||
|
||||
@validates('required')
|
||||
def validate_required(self, key, value):
|
||||
@@ -1238,6 +1280,8 @@ class KitTypeReagentRoleAssociation(BaseClass):
|
||||
Returns:
|
||||
_type_: value
|
||||
"""
|
||||
if isinstance(value, bool):
|
||||
value = int(value)
|
||||
if not 0 <= value < 2:
|
||||
raise ValueError(f'Invalid required value {value}. Must be 0 or 1.')
|
||||
return value
|
||||
@@ -1264,8 +1308,8 @@ class KitTypeReagentRoleAssociation(BaseClass):
|
||||
@classmethod
|
||||
@setup_lookup
|
||||
def query(cls,
|
||||
kit_type: KitType | str | None = None,
|
||||
reagent_role: ReagentRole | str | None = None,
|
||||
kittype: KitType | str | None = None,
|
||||
reagentrole: ReagentRole | str | None = None,
|
||||
limit: int = 0,
|
||||
**kwargs
|
||||
) -> KitTypeReagentRoleAssociation | List[KitTypeReagentRoleAssociation]:
|
||||
@@ -1281,21 +1325,21 @@ class KitTypeReagentRoleAssociation(BaseClass):
|
||||
models.KitTypeReagentTypeAssociation|List[models.KitTypeReagentTypeAssociation]: Junction of interest.
|
||||
"""
|
||||
query: Query = cls.__database_session__.query(cls)
|
||||
match kit_type:
|
||||
match kittype:
|
||||
case KitType():
|
||||
query = query.filter(cls.kit_type == kit_type)
|
||||
query = query.filter(cls.kit_type == kittype)
|
||||
case str():
|
||||
query = query.join(KitType).filter(KitType.name == kit_type)
|
||||
query = query.join(KitType).filter(KitType.name == kittype)
|
||||
case _:
|
||||
pass
|
||||
match reagent_role:
|
||||
match reagentrole:
|
||||
case ReagentRole():
|
||||
query = query.filter(cls.reagent_role == reagent_role)
|
||||
query = query.filter(cls.reagent_role == reagentrole)
|
||||
case str():
|
||||
query = query.join(ReagentRole).filter(ReagentRole.name == reagent_role)
|
||||
query = query.join(ReagentRole).filter(ReagentRole.name == reagentrole)
|
||||
case _:
|
||||
pass
|
||||
if kit_type is not None and reagent_role is not None:
|
||||
if kittype is not None and reagentrole is not None:
|
||||
limit = 1
|
||||
return cls.execute_query(query=query, limit=limit)
|
||||
|
||||
@@ -1329,8 +1373,8 @@ class KitTypeReagentRoleAssociation(BaseClass):
|
||||
yield rel_reagent
|
||||
|
||||
@property
|
||||
def omnigui_dict(self) -> dict:
|
||||
dicto = super().omnigui_dict
|
||||
def omnigui_instance_dict(self) -> dict:
|
||||
dicto = super().omnigui_instance_dict
|
||||
dicto['required']['instance_attr'] = bool(dicto['required']['instance_attr'])
|
||||
return dicto
|
||||
|
||||
@@ -1801,6 +1845,14 @@ class SubmissionTypeEquipmentRoleAssociation(BaseClass):
|
||||
equipment_role = relationship(EquipmentRole,
|
||||
back_populates="equipmentrole_submissiontype_associations") #: associated equipment
|
||||
|
||||
@hybrid_property
|
||||
def submissiontype(self):
|
||||
return self.submission_type
|
||||
|
||||
@hybrid_property
|
||||
def equipmentrole(self):
|
||||
return self.equipment_role
|
||||
|
||||
@validates('static')
|
||||
def validate_static(self, key, value):
|
||||
"""
|
||||
@@ -1847,6 +1899,8 @@ class Process(BaseClass):
|
||||
A Process is a method used by a piece of equipment.
|
||||
"""
|
||||
|
||||
level = 2
|
||||
|
||||
id = Column(INTEGER, primary_key=True) #: Process id, primary key
|
||||
name = Column(String(64), unique=True) #: Process name
|
||||
submission_types = relationship("SubmissionType", back_populates='processes',
|
||||
@@ -1869,14 +1923,24 @@ class Process(BaseClass):
|
||||
"""
|
||||
return f"<Process({self.name})>"
|
||||
|
||||
def set_attribute(self, key, value):
|
||||
match key:
|
||||
case "name":
|
||||
self.name = value
|
||||
case _:
|
||||
field = getattr(self, key)
|
||||
if value not in field:
|
||||
field.append(value)
|
||||
|
||||
|
||||
@classmethod
|
||||
@setup_lookup
|
||||
def query(cls,
|
||||
name: str | None = None,
|
||||
id: int | None = None,
|
||||
submission_type: str | SubmissionType | None = None,
|
||||
extraction_kit: str | KitType | None = None,
|
||||
equipment_role: str | KitType | None = None,
|
||||
submissiontype: str | SubmissionType | None = None,
|
||||
kittype: str | KitType | None = None,
|
||||
equipmentrole: str | KitType | None = None,
|
||||
limit: int = 0,
|
||||
**kwargs) -> Process | List[Process]:
|
||||
"""
|
||||
@@ -1891,28 +1955,28 @@ class Process(BaseClass):
|
||||
Process|List[Process]: Process(es) matching criteria
|
||||
"""
|
||||
query = cls.__database_session__.query(cls)
|
||||
match submission_type:
|
||||
match submissiontype:
|
||||
case str():
|
||||
submission_type = SubmissionType.query(name=submission_type)
|
||||
query = query.filter(cls.submission_types.contains(submission_type))
|
||||
submissiontype = SubmissionType.query(name=submissiontype)
|
||||
query = query.filter(cls.submission_types.contains(submissiontype))
|
||||
case SubmissionType():
|
||||
query = query.filter(cls.submission_types.contains(submission_type))
|
||||
query = query.filter(cls.submission_types.contains(submissiontype))
|
||||
case _:
|
||||
pass
|
||||
match extraction_kit:
|
||||
match kittype:
|
||||
case str():
|
||||
extraction_kit = KitType.query(name=extraction_kit)
|
||||
query = query.filter(cls.kit_types.contains(extraction_kit))
|
||||
kittype = KitType.query(name=kittype)
|
||||
query = query.filter(cls.kit_types.contains(kittype))
|
||||
case KitType():
|
||||
query = query.filter(cls.kit_types.contains(extraction_kit))
|
||||
query = query.filter(cls.kit_types.contains(kittype))
|
||||
case _:
|
||||
pass
|
||||
match equipment_role:
|
||||
match equipmentrole:
|
||||
case str():
|
||||
equipment_role = EquipmentRole.query(name=equipment_role)
|
||||
query = query.filter(cls.equipment_roles.contains(equipment_role))
|
||||
equipmentrole = EquipmentRole.query(name=equipmentrole)
|
||||
query = query.filter(cls.equipment_roles.contains(equipmentrole))
|
||||
case EquipmentRole():
|
||||
query = query.filter(cls.equipment_roles.contains(equipment_role))
|
||||
query = query.filter(cls.equipment_roles.contains(equipmentrole))
|
||||
case _:
|
||||
pass
|
||||
match name:
|
||||
@@ -1983,6 +2047,10 @@ class Tips(BaseClass, LogMixin):
|
||||
|
||||
submissions = association_proxy("tips_submission_associations", 'submission')
|
||||
|
||||
@hybrid_property
|
||||
def tiprole(self):
|
||||
return self.role
|
||||
|
||||
def __repr__(self):
|
||||
return f"<Tips({self.name})>"
|
||||
|
||||
@@ -2033,6 +2101,14 @@ class SubmissionTypeTipRoleAssociation(BaseClass):
|
||||
tip_role = relationship(TipRole,
|
||||
back_populates="tiprole_submissiontype_associations") #: associated equipment
|
||||
|
||||
@hybrid_property
|
||||
def submissiontype(self):
|
||||
return self.submission_type
|
||||
|
||||
@hybrid_property
|
||||
def tiprole(self):
|
||||
return self.tip_role
|
||||
|
||||
@check_authorization
|
||||
def save(self):
|
||||
super().save()
|
||||
|
||||
@@ -6,6 +6,7 @@ import json, yaml, logging
|
||||
from pathlib import Path
|
||||
from pprint import pformat
|
||||
from sqlalchemy import Column, String, INTEGER, ForeignKey, Table
|
||||
from sqlalchemy.ext.hybrid import hybrid_property
|
||||
from sqlalchemy.orm import relationship, Query
|
||||
from . import Base, BaseClass
|
||||
from tools import check_authorization, setup_lookup, yaml_regex_creator
|
||||
@@ -38,6 +39,10 @@ class Organization(BaseClass):
|
||||
contacts = relationship("Contact", back_populates="organization",
|
||||
secondary=orgs_contacts) #: contacts involved with this org
|
||||
|
||||
@hybrid_property
|
||||
def contact(self):
|
||||
return self.contacts
|
||||
|
||||
def __repr__(self) -> str:
|
||||
return f"<Organization({self.name})>"
|
||||
|
||||
|
||||
@@ -10,6 +10,9 @@ from zipfile import ZipFile, BadZipfile
|
||||
from tempfile import TemporaryDirectory, TemporaryFile
|
||||
from operator import itemgetter
|
||||
from pprint import pformat
|
||||
|
||||
from sqlalchemy.ext.hybrid import hybrid_property
|
||||
|
||||
from . import BaseClass, Reagent, SubmissionType, KitType, Organization, Contact, LogMixin, SubmissionReagentAssociation
|
||||
from sqlalchemy import Column, String, TIMESTAMP, INTEGER, ForeignKey, JSON, FLOAT, case, func
|
||||
from sqlalchemy.orm import relationship, validates, Query
|
||||
@@ -126,6 +129,14 @@ class BasicSubmission(BaseClass, LogMixin):
|
||||
def __repr__(self) -> str:
|
||||
return f"<Submission({self.rsl_plate_num})>"
|
||||
|
||||
@hybrid_property
|
||||
def kittype(self):
|
||||
return self.extraction_kit
|
||||
|
||||
@hybrid_property
|
||||
def organization(self):
|
||||
return self.submitting_lab
|
||||
|
||||
@classproperty
|
||||
def jsons(cls) -> List[str]:
|
||||
"""
|
||||
@@ -488,7 +499,7 @@ class BasicSubmission(BaseClass, LogMixin):
|
||||
"""
|
||||
# NOTE: use lookup function to create list of dicts
|
||||
subs = [item.to_dict() for item in
|
||||
cls.query(submission_type=submission_type, limit=limit, chronologic=chronologic, page=page,
|
||||
cls.query(submissiontype=submission_type, limit=limit, chronologic=chronologic, page=page,
|
||||
page_size=page_size)]
|
||||
df = pd.DataFrame.from_records(subs)
|
||||
# NOTE: Exclude sub information
|
||||
@@ -1056,7 +1067,7 @@ class BasicSubmission(BaseClass, LogMixin):
|
||||
@classmethod
|
||||
@setup_lookup
|
||||
def query(cls,
|
||||
submission_type: str | SubmissionType | None = None,
|
||||
submissiontype: str | SubmissionType | None = None,
|
||||
submission_type_name: str | None = None,
|
||||
id: int | str | None = None,
|
||||
rsl_plate_num: str | None = None,
|
||||
@@ -1087,8 +1098,8 @@ class BasicSubmission(BaseClass, LogMixin):
|
||||
"""
|
||||
from ... import SubmissionReagentAssociation
|
||||
# NOTE: if you go back to using 'model' change the appropriate cls to model in the query filters
|
||||
if submission_type is not None:
|
||||
model = cls.find_polymorphic_subclass(polymorphic_identity=submission_type)
|
||||
if submissiontype is not None:
|
||||
model = cls.find_polymorphic_subclass(polymorphic_identity=submissiontype)
|
||||
elif len(kwargs) > 0:
|
||||
# NOTE: find the subclass containing the relevant attributes
|
||||
model = cls.find_polymorphic_subclass(attrs=kwargs)
|
||||
@@ -1196,7 +1207,7 @@ class BasicSubmission(BaseClass, LogMixin):
|
||||
if kwargs == {}:
|
||||
raise ValueError("Need to narrow down query or the first available instance will be returned.")
|
||||
sanitized_kwargs = {k: v for k, v in kwargs.items() if k not in disallowed}
|
||||
instance = cls.query(submission_type=submission_type, limit=1, **sanitized_kwargs)
|
||||
instance = cls.query(submissiontype=submission_type, limit=1, **sanitized_kwargs)
|
||||
if instance is None:
|
||||
used_class = cls.find_polymorphic_subclass(attrs=kwargs, polymorphic_identity=submission_type)
|
||||
instance = used_class(**sanitized_kwargs)
|
||||
|
||||
@@ -157,7 +157,7 @@ class RSLNamer(object):
|
||||
if "rsl_plate_num" in data.keys():
|
||||
plate_number = data['rsl_plate_num'].split("-")[-1][0]
|
||||
else:
|
||||
previous = BasicSubmission.query(start_date=today, end_date=today, submission_type=data['submission_type'])
|
||||
previous = BasicSubmission.query(start_date=today, end_date=today, submissiontype=data['submission_type'])
|
||||
plate_number = len(previous) + 1
|
||||
return f"RSL-{data['abbreviation']}-{today.year}{str(today.month).zfill(2)}{str(today.day).zfill(2)}-{plate_number}"
|
||||
|
||||
@@ -191,5 +191,5 @@ class RSLNamer(object):
|
||||
return ""
|
||||
|
||||
|
||||
from .pydant import PydSubmission, PydKit, PydContact, PydOrganization, PydSample, PydReagent, PydReagentRole, \
|
||||
PydEquipment, PydEquipmentRole, PydTips, PydPCRControl, PydIridaControl
|
||||
from .pydant import PydSubmission, PydKitType, PydContact, PydOrganization, PydSample, PydReagent, PydReagentRole, \
|
||||
PydEquipment, PydEquipmentRole, PydTips, PydPCRControl, PydIridaControl, PydProcess, PydElastic
|
||||
|
||||
@@ -14,6 +14,8 @@ from pathlib import Path
|
||||
from tools import check_not_nan, convert_nans_to_nones, Report, Result, timezone
|
||||
from backend.db.models import *
|
||||
from sqlalchemy.exc import StatementError, IntegrityError
|
||||
from sqlalchemy.orm.properties import ColumnProperty
|
||||
from sqlalchemy.orm.relationships import _RelationshipDeclared
|
||||
from PyQt6.QtWidgets import QWidget
|
||||
|
||||
logger = logging.getLogger(f"submissions.{__name__}")
|
||||
@@ -203,6 +205,7 @@ class PydSample(BaseModel, extra='allow'):
|
||||
fields = list(self.model_fields.keys()) + list(self.model_extra.keys())
|
||||
return {k: getattr(self, k) for k in fields}
|
||||
|
||||
@report_result
|
||||
def to_sql(self, submission: BasicSubmission | str = None) -> Tuple[
|
||||
BasicSample, List[SubmissionSampleAssociation], Result | None]:
|
||||
"""
|
||||
@@ -270,6 +273,7 @@ class PydTips(BaseModel):
|
||||
value = value.name
|
||||
return value
|
||||
|
||||
@report_result
|
||||
def to_sql(self, submission: BasicSubmission) -> SubmissionTipsAssociation:
|
||||
"""
|
||||
Convert this object to the SQL version for database storage.
|
||||
@@ -280,12 +284,13 @@ class PydTips(BaseModel):
|
||||
Returns:
|
||||
SubmissionTipsAssociation: Association between queried tips and submission
|
||||
"""
|
||||
report = Report()
|
||||
tips = Tips.query(name=self.name, limit=1)
|
||||
# logger.debug(f"Tips query has yielded: {tips}")
|
||||
assoc = SubmissionTipsAssociation.query_or_create(tips=tips, submission=submission, role=self.role, limit=1)
|
||||
# if assoc is None:
|
||||
# assoc = SubmissionTipsAssociation(submission=submission, tips=tips, role_name=self.role)
|
||||
return assoc
|
||||
return assoc, report
|
||||
|
||||
|
||||
class PydEquipment(BaseModel, extra='ignore'):
|
||||
@@ -317,6 +322,7 @@ class PydEquipment(BaseModel, extra='ignore'):
|
||||
pass
|
||||
return value
|
||||
|
||||
@report_result
|
||||
def to_sql(self, submission: BasicSubmission | str = None, extraction_kit: KitType | str = None) -> Tuple[Equipment, SubmissionEquipmentAssociation]:
|
||||
"""
|
||||
Creates Equipment and SubmssionEquipmentAssociations for this PydEquipment
|
||||
@@ -327,6 +333,7 @@ class PydEquipment(BaseModel, extra='ignore'):
|
||||
Returns:
|
||||
Tuple[Equipment, SubmissionEquipmentAssociation]: SQL objects
|
||||
"""
|
||||
report = Report()
|
||||
if isinstance(submission, str):
|
||||
submission = BasicSubmission.query(rsl_plate_num=submission)
|
||||
if isinstance(extraction_kit, str):
|
||||
@@ -349,7 +356,7 @@ class PydEquipment(BaseModel, extra='ignore'):
|
||||
# NOTE: It looks like the way fetching the processes is done in the SQL model, this shouldn't be a problem, but I'll include a failsafe.
|
||||
# NOTE: I need to find a way to filter this by the kit involved.
|
||||
if len(self.processes) > 1:
|
||||
process = Process.query(submission_type=submission.get_submission_type(), extraction_kit=extraction_kit, equipment_role=self.role)
|
||||
process = Process.query(submissiontype=submission.get_submission_type(), kittype=extraction_kit, equipmentrole=self.role)
|
||||
else:
|
||||
process = Process.query(name=self.processes[0])
|
||||
if process is None:
|
||||
@@ -362,7 +369,7 @@ class PydEquipment(BaseModel, extra='ignore'):
|
||||
else:
|
||||
logger.warning(f"No submission found")
|
||||
assoc = None
|
||||
return equipment, assoc
|
||||
return equipment, assoc, report
|
||||
|
||||
def improved_dict(self) -> dict:
|
||||
"""
|
||||
@@ -762,6 +769,7 @@ class PydSubmission(BaseModel, extra='allow'):
|
||||
missing_reagents = [reagent for reagent in self.reagents if reagent.missing]
|
||||
return missing_info, missing_reagents
|
||||
|
||||
@report_result
|
||||
def to_sql(self) -> Tuple[BasicSubmission, Report]:
|
||||
"""
|
||||
Converts this instance into a backend.db.models.submissions.BasicSubmission instance
|
||||
@@ -867,7 +875,7 @@ class PydSubmission(BaseModel, extra='allow'):
|
||||
# NOTE: Apply any discounts that are applicable for client and kit.
|
||||
try:
|
||||
discounts = [item.amount for item in
|
||||
Discount.query(kit_type=instance.extraction_kit, organization=instance.submitting_lab)]
|
||||
Discount.query(kittype=instance.extraction_kit, organization=instance.submitting_lab)]
|
||||
if len(discounts) > 0:
|
||||
instance.run_cost = instance.run_cost - sum(discounts)
|
||||
except Exception as e:
|
||||
@@ -1047,7 +1055,7 @@ class PydOrganization(BaseModel):
|
||||
return None
|
||||
return value
|
||||
|
||||
|
||||
@report_result
|
||||
def to_sql(self) -> Organization:
|
||||
"""
|
||||
Converts this instance into a backend.db.models.organization.Organization instance.
|
||||
@@ -1055,6 +1063,7 @@ class PydOrganization(BaseModel):
|
||||
Returns:
|
||||
Organization: Organization instance
|
||||
"""
|
||||
report = Report()
|
||||
instance = Organization()
|
||||
for field in self.model_fields:
|
||||
match field:
|
||||
@@ -1067,7 +1076,7 @@ class PydOrganization(BaseModel):
|
||||
logger.debug(f"Setting {field} to {value}")
|
||||
if value:
|
||||
setattr(instance, field, value)
|
||||
return instance
|
||||
return instance, report
|
||||
|
||||
|
||||
class PydReagentRole(BaseModel):
|
||||
@@ -1083,6 +1092,7 @@ class PydReagentRole(BaseModel):
|
||||
return timedelta(days=value)
|
||||
return value
|
||||
|
||||
@report_result
|
||||
def to_sql(self, kit: KitType) -> ReagentRole:
|
||||
"""
|
||||
Converts this instance into a backend.db.models.ReagentType instance
|
||||
@@ -1093,23 +1103,25 @@ class PydReagentRole(BaseModel):
|
||||
Returns:
|
||||
ReagentRole: ReagentType instance
|
||||
"""
|
||||
report = Report()
|
||||
instance: ReagentRole = ReagentRole.query(name=self.name)
|
||||
if instance is None:
|
||||
instance = ReagentRole(name=self.name, eol_ext=self.eol_ext)
|
||||
try:
|
||||
assoc = KitTypeReagentRoleAssociation.query(reagent_role=instance, kit_type=kit)
|
||||
assoc = KitTypeReagentRoleAssociation.query(reagentrole=instance, kittype=kit)
|
||||
except StatementError:
|
||||
assoc = None
|
||||
if assoc is None:
|
||||
assoc = KitTypeReagentRoleAssociation(kit_type=kit, reagent_role=instance, uses=self.uses,
|
||||
required=self.required)
|
||||
return instance
|
||||
return instance, report
|
||||
|
||||
|
||||
class PydKit(BaseModel):
|
||||
class PydKitType(BaseModel):
|
||||
name: str
|
||||
reagent_roles: List[PydReagentRole] = []
|
||||
|
||||
@report_result
|
||||
def to_sql(self) -> Tuple[KitType, Report]:
|
||||
"""
|
||||
Converts this instance into a backend.db.models.kits.KitType instance
|
||||
@@ -1163,7 +1175,9 @@ class PydPCRControl(BaseModel):
|
||||
submission_id: int
|
||||
controltype_name: str
|
||||
|
||||
@report_result
|
||||
def to_sql(self):
|
||||
report = Report
|
||||
instance = PCRControl.query(name=self.name)
|
||||
if not instance:
|
||||
instance = PCRControl()
|
||||
@@ -1171,7 +1185,7 @@ class PydPCRControl(BaseModel):
|
||||
field_value = self.__getattribute__(key)
|
||||
if instance.__getattribute__(key) != field_value:
|
||||
instance.__setattr__(key, field_value)
|
||||
return instance
|
||||
return instance, report
|
||||
|
||||
|
||||
class PydIridaControl(BaseModel, extra='ignore'):
|
||||
@@ -1196,7 +1210,9 @@ class PydIridaControl(BaseModel, extra='ignore'):
|
||||
value = ""
|
||||
return value
|
||||
|
||||
@report_result
|
||||
def to_sql(self):
|
||||
report = Report()
|
||||
instance = IridaControl.query(name=self.name)
|
||||
if not instance:
|
||||
instance = IridaControl()
|
||||
@@ -1204,7 +1220,7 @@ class PydIridaControl(BaseModel, extra='ignore'):
|
||||
field_value = self.__getattribute__(key)
|
||||
if instance.__getattribute__(key) != field_value:
|
||||
instance.__setattr__(key, field_value)
|
||||
return instance
|
||||
return instance, report
|
||||
|
||||
|
||||
class PydProcess(BaseModel, extra="allow"):
|
||||
@@ -1222,16 +1238,62 @@ class PydProcess(BaseModel, extra="allow"):
|
||||
return [value]
|
||||
return value
|
||||
|
||||
@report_result
|
||||
def to_sql(self):
|
||||
report = Report()
|
||||
instance = Process.query(name=self.name)
|
||||
if not instance:
|
||||
instance = Process()
|
||||
dicto = instance.omnigui_dict
|
||||
for key in self.model_fields:
|
||||
# field_value = self.__getattribute__(key)
|
||||
# if instance.__getattribute__(key) != field_value:
|
||||
# instance.__setattr__(key, field_value)
|
||||
test = dicto[key]
|
||||
print(f"Attribute: {test['class_attr'].property}")
|
||||
# dicto = instance.omnigui_instance_dict
|
||||
fields = [item for item in self.model_fields]
|
||||
for field in fields:
|
||||
logger.debug(f"Field: {field}")
|
||||
try:
|
||||
field_type = getattr(instance.__class__, field).property
|
||||
except AttributeError:
|
||||
logger.error(f"No attribute: {field} in {instance.__class__}")
|
||||
continue
|
||||
match field_type:
|
||||
case _RelationshipDeclared():
|
||||
logger.debug(f"{field} is a relationship with {field_type.entity.class_}")
|
||||
query_str = getattr(self, field)
|
||||
if isinstance(query_str, list):
|
||||
query_str = query_str[0]
|
||||
if query_str in ["", " ", None]:
|
||||
continue
|
||||
logger.debug(f"Querying {field_type.entity.class_} with name {query_str}")
|
||||
field_value = field_type.entity.class_.query(name=query_str)
|
||||
logger.debug(f"{field} query result: {field_value}")
|
||||
case ColumnProperty():
|
||||
logger.debug(f"{field} is a property.")
|
||||
field_value = getattr(self, field)
|
||||
instance.set_attribute(key=field, value=field_value)
|
||||
return instance, report
|
||||
|
||||
|
||||
class PydElastic(BaseModel, extra="allow", arbitrary_types_allowed=True):
|
||||
"""Allows for creation of arbitrary pydantic models"""
|
||||
instance: BaseClass
|
||||
|
||||
@report_result
|
||||
def to_sql(self):
|
||||
print(self.instance)
|
||||
fields = [item for item in self.model_extra]
|
||||
for field in fields:
|
||||
try:
|
||||
field_type = getattr(self.instance.__class__, field).property
|
||||
except AttributeError:
|
||||
logger.error(f"No attribute: {field} in {self.instance.__class__}")
|
||||
continue
|
||||
match field_type:
|
||||
case _RelationshipDeclared():
|
||||
logger.debug(f"{field} is a relationship with {field_type.entity.class_}")
|
||||
field_value = field_type.entity.class_.argument.query(name=getattr(self, field))
|
||||
logger.debug(f"{field} query result: {field_value}")
|
||||
case ColumnProperty():
|
||||
logger.debug(f"{field} is a property.")
|
||||
field_value = getattr(self, field)
|
||||
self.instance.__setattr__(field, field_value)
|
||||
return self.instance
|
||||
|
||||
|
||||
return instance
|
||||
|
||||
Reference in New Issue
Block a user