Switching to pydantic as primary object for Omni-manager

This commit is contained in:
lwark
2025-02-05 10:06:04 -06:00
parent 05421cc1d4
commit f57aa3c3f0
6 changed files with 117 additions and 89 deletions

View File

@@ -53,6 +53,7 @@ class BaseClass(Base):
singles = ['id'] singles = ['id']
omni_removes = ["id", 'submissions', "omnigui_class_dict", "omnigui_instance_dict"] omni_removes = ["id", 'submissions', "omnigui_class_dict", "omnigui_instance_dict"]
omni_sort = ["name"] omni_sort = ["name"]
omni_inheritable = []
@classproperty @classproperty
def skip_on_edit(cls): def skip_on_edit(cls):
@@ -330,7 +331,15 @@ class BaseClass(Base):
query_kwargs = {relationship_instance.query_alias: relationship_instance} query_kwargs = {relationship_instance.query_alias: relationship_instance}
return cls.query(**query_kwargs) return cls.query(**query_kwargs)
def check_all_attributes(self, attributes: dict): def check_all_attributes(self, attributes: dict) -> bool:
"""
Args:
attributes (dict): A dictionary of attributes to be check for equivalence
Returns:
bool: If a single unequivocal value is found will be false, else true.
"""
logger.debug(f"Incoming attributes: {attributes}") logger.debug(f"Incoming attributes: {attributes}")
for key, value in attributes.items(): for key, value in attributes.items():
# print(getattr(self.__class__, key).property) # print(getattr(self.__class__, key).property)
@@ -365,6 +374,10 @@ class BaseClass(Base):
return True return True
def __setattr__(self, key, value): def __setattr__(self, key, value):
"""
Custom dunder method to handle potential list relationship issues.
"""
if key != "_sa_instance_state":
logger.debug(f"Attempting to set {key} to {pformat(value)}") logger.debug(f"Attempting to set {key} to {pformat(value)}")
try: try:
field_type = getattr(self.__class__, key) field_type = getattr(self.__class__, key)
@@ -395,8 +408,10 @@ class BaseClass(Base):
else: else:
raise ValueError("Object is too long to parse a single value.") raise ValueError("Object is too long to parse a single value.")
return super().__setattr__(key, value) return super().__setattr__(key, value)
case _:
return super().__setattr__(key, value)
else: else:
super().__setattr__(key, value) return super().__setattr__(key, value)
class ConfigItem(BaseClass): class ConfigItem(BaseClass):

View File

@@ -380,6 +380,9 @@ class KitType(BaseClass):
new_process.equipment_roles.append(new_role) new_process.equipment_roles.append(new_role)
return new_kit return new_kit
def to_pydantic(self):
pass
class ReagentRole(BaseClass): class ReagentRole(BaseClass):
""" """
@@ -1232,6 +1235,7 @@ class KitTypeReagentRoleAssociation(BaseClass):
omni_removes = BaseClass.omni_removes + ["submission_type_id", "kits_id", "reagent_roles_id", "last_used"] omni_removes = BaseClass.omni_removes + ["submission_type_id", "kits_id", "reagent_roles_id", "last_used"]
omni_sort = ["submission_type", "kit_type", "reagent_role", "required", "uses"] omni_sort = ["submission_type", "kit_type", "reagent_role", "required", "uses"]
omni_inheritable = ["submission_type", "kit_type"]
reagent_roles_id = Column(INTEGER, ForeignKey("_reagentrole.id"), reagent_roles_id = Column(INTEGER, ForeignKey("_reagentrole.id"),
primary_key=True) #: id of associated reagent type primary_key=True) #: id of associated reagent type

View File

@@ -417,9 +417,11 @@ class BasicSubmission(BaseClass, LogMixin):
except Exception as e: except Exception as e:
logger.error(f"Column count error: {e}") logger.error(f"Column count error: {e}")
# NOTE: Get kit associated with this submission # NOTE: Get kit associated with this submission
logger.debug(f"Checking associations with submission type: {self.submission_type_name}")
assoc = next((item for item in self.extraction_kit.kit_submissiontype_associations if assoc = next((item for item in self.extraction_kit.kit_submissiontype_associations if
item.submission_type == self.submission_type), item.submission_type == self.submission_type),
None) None)
logger.debug(f"Got association: {assoc}")
# NOTE: If every individual cost is 0 this is probably an old plate. # NOTE: If every individual cost is 0 this is probably an old plate.
if all(item == 0.0 for item in [assoc.constant_cost, assoc.mutable_cost_column, assoc.mutable_cost_sample]): if all(item == 0.0 for item in [assoc.constant_cost, assoc.mutable_cost_column, assoc.mutable_cost_sample]):
try: try:
@@ -1325,7 +1327,10 @@ class BasicSubmission(BaseClass, LogMixin):
if dlg.exec(): if dlg.exec():
equipment = dlg.parse_form() equipment = dlg.parse_form()
for equip in equipment: for equip in equipment:
logger.debug(f"Parsed equipment: {equip}")
_, assoc = equip.to_sql(submission=self) _, assoc = equip.to_sql(submission=self)
logger.debug(f"Got equipment association: {assoc.__dict__}")
try: try:
assoc.save() assoc.save()
except AttributeError as e: except AttributeError as e:

View File

@@ -355,12 +355,14 @@ class PydEquipment(BaseModel, extra='ignore'):
# TODO: This seems precarious. What if there is more than one process? # TODO: This seems precarious. What if there is more than one process?
# 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: 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. # NOTE: I need to find a way to filter this by the kit involved.
if len(self.processes) > 1: if len(self.processes) > 1:
process = Process.query(submissiontype=submission.get_submission_type(), kittype=extraction_kit, equipmentrole=self.role) process = Process.query(submissiontype=submission.get_submission_type(), kittype=extraction_kit, equipmentrole=self.role)
else: else:
process = Process.query(name=self.processes[0]) process = Process.query(name=self.processes[0])
if process is None: if process is None:
logger.error(f"Found unknown process: {process}.") logger.error(f"Found unknown process: {process}.")
logger.debug(f"Using process: {process}")
assoc.process = process assoc.process = process
assoc.role = self.role assoc.role = self.role
else: else:
@@ -779,8 +781,10 @@ class PydSubmission(BaseModel, extra='allow'):
""" """
report = Report() report = Report()
dicto = self.improved_dict() dicto = self.improved_dict()
logger.debug(f"Pydantic submission type: {self.submission_type['value']}")
instance, result = BasicSubmission.query_or_create(submission_type=self.submission_type['value'], instance, result = BasicSubmission.query_or_create(submission_type=self.submission_type['value'],
rsl_plate_num=self.rsl_plate_num['value']) rsl_plate_num=self.rsl_plate_num['value'])
logger.debug(f"Created or queried instance: {instance}")
report.add_result(result) report.add_result(result)
self.handle_duplicate_samples() self.handle_duplicate_samples()
for key, value in dicto.items(): for key, value in dicto.items():
@@ -864,6 +868,7 @@ class PydSubmission(BaseModel, extra='allow'):
continue continue
else: else:
logger.warning(f"{key} already == {value} so no updating.") logger.warning(f"{key} already == {value} so no updating.")
logger.debug(f"Entering cost calculation for {instance}")
try: try:
instance.calculate_base_cost() instance.calculate_base_cost()
except (TypeError, AttributeError) as e: except (TypeError, AttributeError) as e:

View File

@@ -17,7 +17,7 @@ from sqlalchemy.orm.properties import ColumnProperty
from sqlalchemy.orm.relationships import _RelationshipDeclared from sqlalchemy.orm.relationships import _RelationshipDeclared
from pandas import DataFrame from pandas import DataFrame
from backend import db from backend import db
from tools import check_object_in_managers from tools import check_object_in_manager
from .omni_search import SearchBox from .omni_search import SearchBox
from frontend.widgets.submission_table import pandasModel from frontend.widgets.submission_table import pandasModel
@@ -32,18 +32,22 @@ class ManagerWindow(QDialog):
def __init__(self, parent, def __init__(self, parent,
object_type: Any, object_type: Any,
extras: List[str], extras: List[str],
managers: set = set(), manager: Any | None = None,
add_edit: Literal['add', 'edit'] = 'edit', add_edit: Literal['add', 'edit'] = 'edit',
**kwargs): **kwargs):
super().__init__(parent) super().__init__(parent)
self.object_type = self.original_type = object_type self.class_object = self.original_type = object_type
self.add_edit = add_edit self.add_edit = add_edit
# NOTE: Should I pass in an instance?
self.instance = None self.instance = None
self.managers = managers if manager is None:
try: try:
self.managers.add(self.parent().instance) self.manager = self.parent().instance
except AttributeError: except AttributeError:
pass self.manager = None
else:
self.manager = manager
# logger.debug(f"Managers: {managers}") # logger.debug(f"Managers: {managers}")
self.extras = extras self.extras = extras
self.context = kwargs self.context = kwargs
@@ -53,7 +57,7 @@ class ManagerWindow(QDialog):
self.buttonBox.accepted.connect(self.accept) self.buttonBox.accepted.connect(self.accept)
self.buttonBox.rejected.connect(self.reject) self.buttonBox.rejected.connect(self.reject)
self.setMinimumSize(600, 600) self.setMinimumSize(600, 600)
sub_classes = ["Any"] + [cls.__name__ for cls in self.object_type.__subclasses__()] sub_classes = ["Any"] + [cls.__name__ for cls in self.class_object.__subclasses__()]
if len(sub_classes) > 1: if len(sub_classes) > 1:
self.sub_class = QComboBox(self) self.sub_class = QComboBox(self)
self.sub_class.setObjectName("sub_class") self.sub_class.setObjectName("sub_class")
@@ -71,7 +75,7 @@ class ManagerWindow(QDialog):
else: else:
self.update_data(initial=True) self.update_data(initial=True)
self.setLayout(self.layout) self.setLayout(self.layout)
self.setWindowTitle(f"Manage {self.object_type.__name__} - Managers: {self.managers}") self.setWindowTitle(f"Manage {self.class_object.__name__} - Manager: {self.manager}")
def update_options(self) -> None: def update_options(self) -> None:
""" """
@@ -79,7 +83,7 @@ class ManagerWindow(QDialog):
""" """
# logger.debug(f"Instance: {self.instance}") # logger.debug(f"Instance: {self.instance}")
if self.sub_class: if self.sub_class:
self.object_type = getattr(db, self.sub_class.currentText()) self.class_object = getattr(db, self.sub_class.currentText())
# logger.debug(f"From update options, managers: {self.managers}") # logger.debug(f"From update options, managers: {self.managers}")
try: try:
query_kwargs = {self.parent().instance.query_alias: self.parent().instance} query_kwargs = {self.parent().instance.query_alias: self.parent().instance}
@@ -87,8 +91,8 @@ class ManagerWindow(QDialog):
# logger.debug(f"Couldn't set query kwargs due to: {e}") # logger.debug(f"Couldn't set query kwargs due to: {e}")
query_kwargs = {} query_kwargs = {}
# logger.debug(f"Query kwargs: {query_kwargs}") # logger.debug(f"Query kwargs: {query_kwargs}")
options = [item.name for item in self.object_type.query(**query_kwargs)] options = [item.name for item in self.class_object.query(**query_kwargs)]
# logger.debug(f"self.object_type: {self.object_type}") # logger.debug(f"self.class_object: {self.class_object}")
if self.instance: if self.instance:
try: try:
inserter = options.pop(options.index(self.instance.name)) inserter = options.pop(options.index(self.instance.name))
@@ -130,10 +134,10 @@ class ManagerWindow(QDialog):
# if check: # if check:
if self.add_edit == "edit" and initial: if self.add_edit == "edit" and initial:
# logger.debug(f"Querying with {self.options.currentText()}") # logger.debug(f"Querying with {self.options.currentText()}")
self.instance = self.object_type.query(name=self.options.currentText(), limit=1) self.instance = self.class_object.query(name=self.options.currentText(), limit=1)
# logger.debug(f"Instance: {self.instance}") # logger.debug(f"Instance: {self.instance}")
if not self.instance: if not self.instance:
self.instance = self.object_type() self.instance = self.class_object()
# logger.debug(f"self.instance: {self.instance}") # logger.debug(f"self.instance: {self.instance}")
fields = self.instance.omnigui_instance_dict fields = self.instance.omnigui_instance_dict
for key, field in fields.items(): for key, field in fields.items():
@@ -150,8 +154,8 @@ class ManagerWindow(QDialog):
# NOTE: RelationshipDeclareds will be given a list of existing related objects. # NOTE: RelationshipDeclareds will be given a list of existing related objects.
case _RelationshipDeclared(): case _RelationshipDeclared():
if key != "submissions": if key != "submissions":
# NOTE: field.comparator.entity.class_ gives the relationship class # NOTE: field.comparator.class_object.class_ gives the relationship class
widget = EditRelationship(self, key=key, entity=field['class_attr'].comparator.entity.class_, widget = EditRelationship(self, key=key, class_object=field['class_attr'].comparator.entity.class_,
value=value) value=value)
else: else:
continue continue
@@ -185,7 +189,7 @@ class ManagerWindow(QDialog):
return self.instance return self.instance
def add_new(self): def add_new(self):
new_instance = self.object_type() new_instance = self.class_object()
self.instance = new_instance self.instance = new_instance
self.update_options() self.update_options()
@@ -207,6 +211,7 @@ class ManagerWindow(QDialog):
else: else:
value = current_value + [data] value = current_value + [data]
setattr(self.instance, name, value) setattr(self.instance, name, value)
self.instance.save()
def toggle_textedit(self, caller_child=None): def toggle_textedit(self, caller_child=None):
already_exists = self.findChildren(LargeTextEdit) already_exists = self.findChildren(LargeTextEdit)
@@ -288,14 +293,15 @@ class EditProperty(QWidget):
class EditRelationship(QWidget): class EditRelationship(QWidget):
def __init__(self, parent, key: str, entity: Any, value): def __init__(self, parent, key: str, class_object: Any, value):
super().__init__(parent) super().__init__(parent)
self.entity = entity #: The class of interest self.class_object = class_object #: The class of interest
self.setParent(parent) self.setParent(parent)
# logger.debug(f"Edit relationship entity: {self.entity}") # logger.debug(f"Edit relationship class_object: {self.class_object}")
self.label = QLabel(key.title().replace("_", " ")) self.label = QLabel(key.title().replace("_", " "))
self.setObjectName(key) #: key is the name of the relationship this represents self.setObjectName(key) #: key is the name of the relationship this represents
self.relationship = getattr(self.parent().instance.__class__, key) #: relationship object for type differentiation self.relationship = getattr(self.parent().instance.__class__,
key) #: relationship object for type differentiation
# logger.debug(f"self.relationship: {self.relationship}") # logger.debug(f"self.relationship: {self.relationship}")
# logger.debug(f"Relationship uses list: {self.relationship.property.uselist}") # logger.debug(f"Relationship uses list: {self.relationship.property.uselist}")
# NOTE: value is a database object in this case. # NOTE: value is a database object in this case.
@@ -306,7 +312,7 @@ class EditRelationship(QWidget):
self.add_button.clicked.connect(self.add_new) self.add_button.clicked.connect(self.add_new)
self.existing_button = QPushButton("Add Existing") self.existing_button = QPushButton("Add Existing")
self.existing_button.clicked.connect(self.add_existing) self.existing_button.clicked.connect(self.add_existing)
# self.existing_button.setEnabled(self.entity.level == 1) # self.existing_button.setEnabled(self.class_object.level == 1)
if not isinstance(value, list): if not isinstance(value, list):
if value is not None: if value is not None:
value = [value] value = [value]
@@ -314,10 +320,12 @@ class EditRelationship(QWidget):
value = [] value = []
self.data = value self.data = value
# self.update_buttons() # self.update_buttons()
checked_manager, is_primary = check_object_in_managers(self.parent().managers, self.objectName()) checked_manager, is_primary = check_object_in_manager(self.parent().manager, self.objectName())
if checked_manager: if checked_manager:
logger.debug(f"Checked manager for {self.objectName()}: {checked_manager}") logger.debug(f"Checked manager for {self.objectName()}: {checked_manager}")
if checked_manager is not None and not self.data: logger.debug(f"Omni will inherit: {self.class_object.omni_inheritable} from {self.parent().class_object}")
if checked_manager is not None and not self.data and self.objectName() in self.parent().class_object.omni_inheritable:
logger.debug(f"Setting {checked_manager} in self.data")
self.data = [checked_manager] self.data = [checked_manager]
if not self.relationship.property.uselist: if not self.relationship.property.uselist:
self.add_button.setEnabled(False) self.add_button.setEnabled(False)
@@ -344,7 +352,7 @@ class EditRelationship(QWidget):
def parse_row(self, x): def parse_row(self, x):
context = {item: x.sibling(x.row(), self.df.columns.get_loc(item)).data() for item in self.df.columns} context = {item: x.sibling(x.row(), self.df.columns.get_loc(item)).data() for item in self.df.columns}
try: try:
object = self.entity.query(**context) object = self.class_object.query(**context)
except KeyError: except KeyError:
object = None object = None
self.widget.doubleClicked.disconnect() self.widget.doubleClicked.disconnect()
@@ -353,40 +361,29 @@ class EditRelationship(QWidget):
def add_new(self, instance: Any = None): def add_new(self, instance: Any = None):
# NOTE: if an existing instance is not being edited, create a new instance # NOTE: if an existing instance is not being edited, create a new instance
if not instance: if not instance:
instance = self.entity() instance = self.class_object()
managers = self.parent().managers manager = self.parent().manager
# logger.debug(f"Managers going into add new: {managers}") # logger.debug(f"Managers going into add new: {managers}")
dlg = ManagerWindow(self.parent(), object_type=instance.__class__, extras=[], managers=managers, add_edit="add") dlg = ManagerWindow(self.parent(), object_type=instance.__class__, extras=[], manager=manager, add_edit="add")
if dlg.exec(): if dlg.exec():
new_instance = dlg.parse_form() new_instance = dlg.parse_form()
self.parent().__setattr__(self.objectName(), new_instance) # NOTE: My custom __setattr__ should take care of any list problems.
# logger.debug(f"New instance before transformation attempt: {new_instance}") self.parent().instance.__setattr__(self.objectName(), new_instance)
# try: self.parent().instance.save()
# new_instance = new_instance.to_sql()
# except AttributeError as e:
# logger.error(f"Couldn't convert {new_instance} to sql due to {e}")
# # logger.debug(f"New instance after transformation attempt: {new_instance.__dict__}")
# setattr(self.parent().instance, self.objectName(), new_instance)
# # logger.debug(f"Parent instance after insert: {getattr(self.parent().instance, self.objectName())}")
# # NOTE: Saving currently disabled
# self.parent().instance.save()
# return new_instance
self.parent().update_data() self.parent().update_data()
def add_existing(self): def add_existing(self):
dlg = SearchBox(self, object_type=self.entity, returnable=True, extras=[]) dlg = SearchBox(self, object_type=self.class_object, returnable=True, extras=[])
if dlg.exec(): if dlg.exec():
rows = dlg.return_selected_rows() rows = dlg.return_selected_rows()
for row in rows: for row in rows:
# logger.debug(f"Querying with {row}") # logger.debug(f"Querying with {row}")
instance = self.entity.query(**row) instance = self.class_object.query(**row)
setattr(self.parent().instance, self.objectName(), instance) # NOTE: My custom __setattr__ should take care of any list problems.
# self.parent().instance.save() self.parent().instance.__setattr__(self.objectName(), instance)
self.parent().instance.save()
self.parent().update_data() self.parent().update_data()
def set_choices(self) -> None:
pass
def set_data(self) -> None: def set_data(self) -> None:
""" """
sets data in model sets data in model
@@ -457,7 +454,7 @@ class EditRelationship(QWidget):
logger.debug(f"Attempting to remove {object} from {self.parent().instance.__dict__}") logger.debug(f"Attempting to remove {object} from {self.parent().instance.__dict__}")
editor = getattr(self.parent().instance, self.objectName().lower()) editor = getattr(self.parent().instance, self.objectName().lower())
logger.debug(f"Editor: {editor}") logger.debug(f"Editor: {editor}")
if object in self.parent().managers: if object == self.parent().manager:
logger.error(f"Can't remove manager object.") logger.error(f"Can't remove manager object.")
return return
try: try:
@@ -473,7 +470,7 @@ class EditRelationship(QWidget):
setattr(self.parent().instance, self.objectName().lower(), None) setattr(self.parent().instance, self.objectName().lower(), None)
except ValueError as e: except ValueError as e:
logger.error(f"Remove failed for {self.objectName().lower()} due to {e}.") logger.error(f"Remove failed for {self.objectName().lower()} due to {e}.")
# self.parent().instance.save() self.parent().instance.save()
# self.parent().update_data() # self.parent().update_data()
self.set_data() self.set_data()
@@ -499,7 +496,7 @@ class JsonEditScreen(QDialog):
def __init__(self, parent, key: str): def __init__(self, parent, key: str):
super().__init__(parent) super().__init__(parent)
self.class_obj = parent.object_type self.class_obj = parent.class_object
self.layout = QGridLayout() self.layout = QGridLayout()
self.setWindowTitle(key) self.setWindowTitle(key)
self.json_field = self.class_obj.json_edit_fields self.json_field = self.class_obj.json_edit_fields

View File

@@ -817,8 +817,10 @@ def setup_lookup(func):
return wrapper return wrapper
def check_object_in_managers(managers: list, object_name: object) -> Tuple[Any, bool]: def check_object_in_manager(manager: list, object_name: object) -> Tuple[Any, bool]:
for manager in managers: # for manager in managers:
if manager is None:
return None, False
logger.debug(f"Manager: {manager}, aliases: {manager.aliases}, Key: {object_name}") logger.debug(f"Manager: {manager}, aliases: {manager.aliases}, Key: {object_name}")
if object_name in manager.aliases: if object_name in manager.aliases:
return manager, True return manager, True