Post code-cleanup

This commit is contained in:
lwark
2025-01-21 15:18:37 -06:00
parent bf711369c6
commit bc4af61f5f
26 changed files with 546 additions and 377 deletions

View File

@@ -180,9 +180,9 @@ class KitType(BaseClass):
pass
case _:
raise ValueError(f"Wrong variable type: {type(submission_type)} used!")
logger.debug(f"Submission type: {submission_type}, Kit: {self}")
# logger.debug(f"Submission type: {submission_type}, Kit: {self}")
assocs = [item for item in self.kit_reagentrole_associations if item.submission_type == submission_type]
logger.debug(f"Associations: {assocs}")
# logger.debug(f"Associations: {assocs}")
# NOTE: rescue with submission type's default kit.
if not assocs:
logger.error(
@@ -211,7 +211,7 @@ class KitType(BaseClass):
# except TypeError:
# continue
output = {assoc.reagent_role.name: assoc.uses for assoc in assocs}
logger.debug(f"Output: {output}")
# logger.debug(f"Output: {output}")
return output, new_kit
@classmethod
@@ -1718,7 +1718,7 @@ class SubmissionEquipmentAssociation(BaseClass):
@classmethod
@setup_lookup
def query(cls, equipment_id: int, submission_id: int, role: str | None = None, limit: int = 0, **kwargs) \
def query(cls, equipment_id: int|None=None, submission_id: int|None=None, role: str | None = None, limit: int = 0, **kwargs) \
-> Any | List[Any]:
query: Query = cls.__database_session__.query(cls)
query = query.filter(cls.equipment_id == equipment_id)
@@ -2013,7 +2013,8 @@ class SubmissionTipsAssociation(BaseClass):
@classmethod
def query_or_create(cls, tips, submission, role: str, **kwargs):
instance = cls.query(tip_id=tips.id, role=role, submission_id=submission.id, limit=1, **kwargs)
kwargs['limit'] = 1
instance = cls.query(tip_id=tips.id, role=role, submission_id=submission.id, **kwargs)
if instance is None:
instance = SubmissionTipsAssociation(submission=submission, tips=tips, role_name=role)
return instance

View File

@@ -185,3 +185,6 @@ class Contact(BaseClass):
pass
return cls.execute_query(query=query, limit=limit)
def to_pydantic(self) -> "PydContact":
from backend.validators import PydContact
return PydContact(name=self.name, email=self.email, phone=self.phone)

View File

@@ -279,7 +279,7 @@ class ReagentParser(object):
# submission_type = submission_type['value']
# if isinstance(submission_type, str):
# submission_type = SubmissionType.query(name=submission_type)
logger.debug("Running kit map")
# logger.debug("Running kit map")
associations, self.kit_object = self.kit_object.construct_xl_map_for_use(submission_type=self.submission_type_obj)
reagent_map = {k: v for k, v in associations.items() if k != 'info'}
try:

View File

@@ -158,12 +158,15 @@ class InfoWriter(object):
match k:
case "custom":
continue
# case "comment":
# NOTE: merge all comments to fit in single cell.
if k == "comment" and isinstance(v['value'], list):
json_join = [item['text'] for item in v['value'] if 'text' in item.keys()]
v['value'] = "\n".join(json_join)
case "comment":
# NOTE: merge all comments to fit in single cell.
if isinstance(v['value'], list):
json_join = [item['text'] for item in v['value'] if 'text' in item.keys()]
v['value'] = "\n".join(json_join)
case thing if thing in self.sub_object.timestamps:
v['value'] = v['value'].date()
case _:
pass
final_info[k] = v
try:
locations = v['locations']
@@ -252,6 +255,11 @@ class ReagentWriter(object):
for v in reagent.values():
if not isinstance(v, dict):
continue
match v['value']:
case datetime():
v['value'] = v['value'].date()
case _:
pass
sheet.cell(row=v['row'], column=v['column'], value=v['value'])
return self.xl

View File

@@ -641,7 +641,7 @@ class PydSubmission(BaseModel, extra='allow'):
@field_validator("contact")
@classmethod
def get_contact_from_org(cls, value, values):
logger.debug(f"Value coming in: {value}")
# logger.debug(f"Value coming in: {value}")
match value:
case dict():
if isinstance(value['value'], tuple):
@@ -650,12 +650,12 @@ class PydSubmission(BaseModel, extra='allow'):
value = dict(value=value[0], missing=False)
case _:
value = dict(value=value, missing=False)
logger.debug(f"Value after match: {value}")
# logger.debug(f"Value after match: {value}")
check = Contact.query(name=value['value'])
logger.debug(f"Check came back with {check}")
# logger.debug(f"Check came back with {check}")
if not isinstance(check, Contact):
org = values.data['submitting_lab']['value']
logger.debug(f"Checking organization: {org}")
# logger.debug(f"Checking organization: {org}")
if isinstance(org, str):
org = Organization.query(name=values.data['submitting_lab']['value'], limit=1)
if isinstance(org, Organization):
@@ -666,10 +666,10 @@ class PydSubmission(BaseModel, extra='allow'):
if isinstance(contact, tuple):
contact = contact[0]
value = dict(value=f"Defaulted to: {contact}", missing=False)
logger.debug(f"Value after query: {value}")
# logger.debug(f"Value after query: {value}")
return value
else:
logger.debug(f"Value after bypass check: {value}")
# logger.debug(f"Value after bypass check: {value}")
return value
def __init__(self, run_custom: bool = False, **data):
@@ -879,6 +879,7 @@ class PydSubmission(BaseModel, extra='allow'):
Converts this instance into a frontend.widgets.submission_widget.SubmissionFormWidget
Args:
disable (list, optional): a list of widgets to be disabled in the form. Defaults to None.
parent (QWidget): parent widget of the constructed object
Returns:
@@ -911,7 +912,7 @@ class PydSubmission(BaseModel, extra='allow'):
# @report_result
def check_kit_integrity(self, extraction_kit: str | dict | None = None, exempt: List[PydReagent] = []) -> Tuple[
List[PydReagent], Report]:
List[PydReagent], Report, List[PydReagent]]:
"""
Ensures all reagents expected in kit are listed in Submission
@@ -933,13 +934,11 @@ class PydSubmission(BaseModel, extra='allow'):
ext_kit.get_reagents(required=True, submission_type=self.submission_type['value'])]
# NOTE: Exclude any reagenttype found in this pyd not expected in kit.
expected_check = [item.role for item in ext_kit_rtypes]
logger.debug(self.reagents)
output_reagents = [rt for rt in self.reagents if rt.role in expected_check]
missing_check = [item.role for item in output_reagents]
missing_reagents = [rt for rt in ext_kit_rtypes if rt.role not in missing_check and rt.role not in exempt]
# logger.debug(f"Missing reagents: {missing_reagents}")
missing_reagents += [rt for rt in output_reagents if rt.missing]
logger.debug(pformat(missing_reagents))
output_reagents += [rt for rt in missing_reagents if rt not in output_reagents]
# NOTE: if lists are equal return no problem
if len(missing_reagents) == 0:
@@ -956,13 +955,13 @@ class PydSubmission(BaseModel, extra='allow'):
expired = []
for reagent in self.reagents:
if reagent not in exempt:
role_expiry = ReagentRole.query(name=reagent.role).eol_ext
role_eol = ReagentRole.query(name=reagent.role).eol_ext
try:
dt = datetime.combine(reagent.expiry, datetime.max.time())
except TypeError:
continue
if datetime.now() > dt + role_expiry:
expired.append(f"{reagent.role}, {reagent.lot}: {reagent.expiry} + {role_expiry.days}")
if datetime.now() > dt + role_eol:
expired.append(f"{reagent.role}, {reagent.lot}: {reagent.expiry.date()} + {role_eol.days}")
if expired:
output = '\n'.join(expired)
result = Result(status="Warning",
@@ -996,11 +995,12 @@ class PydContact(BaseModel):
area_regex = re.compile(r"^\(?(\d{3})\)?(-| )?")
if len(value) > 8:
match = area_regex.match(value)
logger.debug(f"Match: {match.group(1)}")
# logger.debug(f"Match: {match.group(1)}")
value = area_regex.sub(f"({match.group(1).strip()}) ", value)
logger.debug(f"Output phone: {value}")
# logger.debug(f"Output phone: {value}")
return value
@report_result
def to_sql(self) -> Tuple[Contact, Report]:
"""
Converts this instance into a backend.db.models.organization. Contact instance.
@@ -1036,6 +1036,18 @@ class PydOrganization(BaseModel):
cost_centre: str
contacts: List[PydContact] | None
@field_validator("contacts", mode="before")
@classmethod
def string_to_list(cls, value):
if isinstance(value, str):
value = Contact.query(name=value)
try:
value = [value.to_pydantic()]
except AttributeError:
return None
return value
def to_sql(self) -> Organization:
"""
Converts this instance into a backend.db.models.organization.Organization instance.
@@ -1047,10 +1059,14 @@ class PydOrganization(BaseModel):
for field in self.model_fields:
match field:
case "contacts":
value = [item.to_sql() for item in getattr(self, field)]
value = getattr(self, field)
if value:
value = [item.to_sql() for item in value if item]
case _:
value = getattr(self, field)
instance.__setattr__(name=field, value=value)
logger.debug(f"Setting {field} to {value}")
if value:
setattr(instance, field, value)
return instance
@@ -1105,7 +1121,8 @@ class PydKit(BaseModel):
instance = KitType.query(name=self.name)
if instance is None:
instance = KitType(name=self.name)
[item.to_sql(instance) for item in self.reagent_roles]
for role in self.reagent_roles:
role.to_sql(instance)
return instance, report
@@ -1162,7 +1179,8 @@ class PydIridaControl(BaseModel, extra='ignore'):
contains: list | dict #: unstructured hashes in contains.tsv for each organism
matches: list | dict #: unstructured hashes in matches.tsv for each organism
kraken: list | dict #: unstructured output from kraken_report
subtype: str #: EN-NOS, MCS-NOS, etc
# subtype: str #: EN-NOS, MCS-NOS, etc
subtype: Literal["ATCC49226", "ATCC49619", "EN-NOS", "EN-SSTI", "MCS-NOS", "MCS-SSTI", "SN-NOS", "SN-SSTI"]
refseq_version: str #: version of refseq used in fastq parsing
kraken2_version: str
kraken2_db_version: str
@@ -1171,6 +1189,13 @@ class PydIridaControl(BaseModel, extra='ignore'):
submission_id: int
controltype_name: str
@field_validator("refseq_version", "kraken2_version", "kraken2_db_version", mode='before')
@classmethod
def enforce_string(cls, value):
if not value:
value = ""
return value
def to_sql(self):
instance = IridaControl.query(name=self.name)
if not instance: