Backend

Backend responsibilities, models, API endpoints, and processing flow for Molecular.

👤 Mohammad Ashfaque Alam📅 Updated: May 19, 2026📁 Molecular

Backend

Architecture Overview

The Molecular feature spans two backend repositories:

  • livehealthapp: owns the Gene, Antibiotic, and Organism master data APIs and their models under the reporting app.
  • crelio-app: owns report-entry persistence and the organism-antibiotic results summary API under the report app.

Master data is managed through REST APIs in livehealthapp. Report values are submitted and persisted through crelio-app. The two systems share the same master record identifiers (gene ids, antibiotic ids, organism ids) referenced at report entry.

System Design

Storage and Models

All core models live in livehealthapp/reporting/models/.

Gene

FieldTypeNotes
idAuto PK
nameCharField (150)Unique within a lab: unique_together = (("name", "lab"),)
codeCharField (150)Gene code
gene_typeCharField (150)Gene type classification
cut_offFloatFieldNumeric cut-off; used by report-entry detection logic
descriptionTextFieldOptional
cpt_codeCharField (250)CPT billing code
labForeignKey to labsLab scope; null means system default
is_disabledBooleanFieldSoft disable without deleting the record
created_at / updated_atDateTimeFieldAuto-timestamps
antibiotics (M2M)via AntibioticResistanceLinks genes to antibiotics through the resistance table

db_table: Gene

Activity log categories: created = 537, updated = 538, enabled = 539, disabled = 540.

Antibiotic

FieldTypeNotes
idAuto PK
nameCharField (150)Unique within a lab
categoryCharField (200)e.g., Fluoroquinolones, Third-generation cephalosporins
codeCharField (150)Antibiotic code; commonly an ATC code
methodCharField (150)Optional testing method
unitCharField (150)Measurement unit
device_nameCharField (100)Optional device-specific name
dosageCharField (100)Optional dosage
descriptionTextFieldOptional
cpt_codeCharField (250)CPT billing code
sample_typeCharField (250)Optional; constrained to defined sample type choices
labForeignKey to labsLab scope; null means system default
is_disabledBooleanFieldSoft disable
created_at / updated_atDateTimeFieldAuto-timestamps

db_table: Antibiotic

Activity log categories: created = 432, updated = 433, enabled = 434, disabled = 435.

Valid report-entry fields: name, unit, method, code, dosage, category, device_name, description.

Organism

FieldTypeNotes
idAuto PK
nameCharField (150)Unique within a lab
categoryCharField (200)Organism category
codeCharField (150)Organism code
cut_offFloatFieldNumeric cut-off for detection logic
descriptionTextFieldOptional
cpt_codeCharField (250)CPT billing code
sample_typeCharField (250)Optional; constrained to defined sample type choices
labForeignKey to labsLab scope; null means system default
is_disabledBooleanFieldSoft disable
created_at / updated_atDateTimeFieldAuto-timestamps
antibiotics (M2M)via OrganismAntibioticsStandard antibiogram mappings

db_table: Organism

Activity log categories: created = 436, updated = 437, enabled = 438, disabled = 439.

Valid report-entry fields: name, result, cut_off, viral_load, interpration.

AntibioticResistance

Links a gene to an antibiotic. This is the through-table for the Gene ↔ Antibiotic M2M.

FieldTypeNotes
geneForeignKey → Generelated_name = "gene_antibiotics"
antibioticForeignKey → Antibioticrelated_name = "antibiotic_gene"
created_at / updated_atDateTimeFieldAuto-timestamps

db_table: AntibioticResistance

After save, gene.update_cache() and antibiotic.update_cache() are called so the in-memory cache reflects the new mapping.

Bulk create deletes all existing gene-scoped mappings and recreates them in one pass, ensuring a clean state.

OrganismAntibiotics

Standard antibiogram junction table linking an organism to antibiotics.

FieldTypeNotes
organismForeignKey → Organism
antibioticForeignKey → Antibioticrelated_name = "antibiotic_organisms"

db_table: OrganismAntibiotics

MolecularOrganismAntibiotics

Molecular-specific antibiogram junction table linking an organism to antibiotics with ordering and active state.

FieldTypeNotes
idAutoField PK
labForeignKey → labs
organismForeignKey → Organism
antibioticForeignKey → Antibiotic
is_activeBooleanFieldWhether this mapping is currently active
sequencePositiveSmallIntegerFieldDisplay ordering
created_byForeignKey → labUserrelated_name = "moa_created_by"
updated_byForeignKey → labUserrelated_name = "moa_updated_by"
created_at / updated_atBigIntegerFieldUnix timestamps

db_table: MolecularOrganismAntibiotics

When an organism is saved, save_molecular_antibiotic_mappings(...) is called. It bulk-updates existing mappings, bulk-creates new ones, and marks removed mappings as is_active = False. Records are never hard-deleted.

Core Backend Responsibilities

ResponsibilityAppView / ModelNotes
Gene CRUDlivehealthappGenesViewCreate, list, retrieve, update
Gene enable / disablelivehealthappGenericEnableDisableViewSoft disable/enable
Antibiotic CRUDlivehealthappAntibioticsViewCreate, list, retrieve, update
Antibiotic enable / disablelivehealthappAntibioticOrganismEnableDisableViewSoft disable/enable with model=Antibiotic
Organism CRUDlivehealthappOrganismsViewCreate, list, retrieve, update
Organism enable / disablelivehealthappAntibioticOrganismEnableDisableViewSoft disable/enable with model=Organism
Organism ↔ Antibiotic mapping fetchlivehealthappOrganismAntibioticViewBidirectional mapping retrieval
Gene cache updatelivehealthappGene.update_cache()Called after save and after AntibioticResistance bulk create
Organism cache updatelivehealthappOrganism.update_cache()Called after save; also invalidates gene cache via update_gene_cache()
Molecular organism antibiotic mappinglivehealthappOrganism.save_molecular_antibiotic_mappings()Upserts MolecularOrganismAntibiotics records during organism save
Report value persistencecrelio-appDrugReportValuesUpdateView at report/<int:report_id>/drugs/updateGeneric report-entry save used for molecular component values
Organism antibiotic summarycrelio-appOrganismAntibioticSummaryView at report/organism-antibiotic-results/Produces antimicrobiogram summary from report values
Organism antibiotic summary repaircrelio-appOrganismAntibioticSummaryRepairView at report/organism-antibiotic-summary/repair/Repairs inconsistent antimicrobiogram records
Device gene mappinglivehealthappdevice_gene_mappingGET/POST/DELETE device-to-gene mappings for instrument integration
Device organism mappinglivehealthappdevice_organism_mappingGET/POST/DELETE device-to-organism mappings for instrument integration

Runtime Engine / Processing Flow

Gene master save flow

Organism save flow

Antibiotic disable flow

Gene cache invalidation cascade

Whenever a gene is saved, enabled, or disabled, Gene.update_organism_cache() fetches one organism belonging to the same lab and calls organism.update_cache(). This ensures that organism serializations that embed gene-mapping data are refreshed whenever genes change.

Conversely, when an organism is saved, Organism.update_gene_cache() fetches one gene in the same lab and calls gene.update_cache(), refreshing gene serializations that embed organism-mapping data.

Organism antibiotic results (antimicrobiogram)

Source: crelio-app/report/views/organism_antibiotic_summary.py

OrganismAntibioticSummaryView at report/organism-antibiotic-results/ receives molecular report values and builds the organism-antibiotic result matrix from submitted report data. This is the backend powering the antimicrobiogram display in the Antibiotic Resistance component's result view.

Report value persistence

Source: crelio-app/report/views/drug_report_values.py

DrugReportValuesUpdateView at report/<int:report_id>/drugs/update and report/<int:report_id>/drugs/default receives and persists molecular component values (Gene results, Organism results, Antibiotic Resistance values) in the same way toxicology drug report values are persisted. Molecular component payloads share this generic endpoint.

API / URL Touchpoints

All molecular master data APIs are mounted under /reporting/ in livehealthapp.

Gene endpoints

MethodEndpointViewAction
GET/reporting/genes/GenesViewList active genes
GET/reporting/genes/?is_disabled=0GenesViewList active genes (frontend initial load)
GET/reporting/genes/?is_disabled=1GenesViewList disabled genes
GET/reporting/genes/{gene_id}GenesViewFetch single gene
POST/reporting/genes/new/GenesView(is_new=True)Create gene
POST/reporting/genes/{gene_id}/update/GenesViewUpdate gene
POST/reporting/genes/{instance_id}/disable/GenericEnableDisableView(disable=True, ModelClass=Gene)Disable gene
POST/reporting/genes/{instance_id}/enable/GenericEnableDisableView(disable=False, ModelClass=Gene)Enable gene

Antibiotic endpoints

MethodEndpointViewAction
GET/reporting/antibiotics/AntibioticsViewList active antibiotics
GET/reporting/antibiotics/?is_disabled=0AntibioticsViewList active antibiotics (frontend initial load)
GET/reporting/antibiotics/?is_disabled=1AntibioticsViewList disabled antibiotics
GET/reporting/antibiotics/{antibiotic_id}AntibioticsViewFetch single antibiotic
POST/reporting/antibiotics/new/AntibioticsView(is_new=True)Create antibiotic
POST/reporting/antibiotics/{antibiotic_id}/update/AntibioticsViewUpdate antibiotic
POST/reporting/antibiotics/{instance_id}/disable/AntibioticOrganismEnableDisableView(should_disable=True, model=Antibiotic)Disable antibiotic
POST/reporting/antibiotics/{instance_id}/enable/AntibioticOrganismEnableDisableView(should_disable=False, model=Antibiotic)Enable antibiotic
GET/reporting/antibiotics/{instance_id}/organisms/OrganismAntibioticView(driver="antibiotic")Fetch organisms linked to an antibiotic

Organism endpoints

MethodEndpointViewAction
GET/reporting/organisms/OrganismsViewList active organisms
GET/reporting/organisms/?is_disabled=0OrganismsViewList active organisms (frontend initial load)
GET/reporting/organisms/?is_disabled=1OrganismsViewList disabled organisms
GET/reporting/organisms/{organism_id}OrganismsViewFetch single organism
POST/reporting/organisms/new/OrganismsView(is_new=True)Create organism
POST/reporting/organisms/{organism_id}/update/OrganismsViewUpdate organism
POST/reporting/organisms/{instance_id}/disable/AntibioticOrganismEnableDisableView(should_disable=True, model=Organism)Disable organism
POST/reporting/organisms/{instance_id}/enable/AntibioticOrganismEnableDisableView(should_disable=False, model=Organism)Enable organism
GET/reporting/organisms/{instance_id}/antibiotics/OrganismAntibioticView(driver="organism")Fetch antibiotics linked to an organism

Device mapping endpoints

MethodEndpointViewAction
GET(device molecular URL)device_gene_mappingFetch device-to-gene mappings
POST(device molecular URL)device_gene_mappingCreate device-to-gene mappings
DELETE(device molecular URL)device_gene_mappingDelete device-to-gene mappings
GET(device molecular URL)device_organism_mappingFetch device-to-organism mappings
POST(device molecular URL)device_organism_mappingCreate device-to-organism mappings
DELETE(device molecular URL)device_organism_mappingDelete device-to-organism mappings

crelio-app report endpoints

MethodEndpointViewAction
POSTreport/<int:report_id>/drugs/updateDrugReportValuesUpdateViewPersist molecular component report values at report entry
POSTreport/<int:report_id>/drugs/defaultDrugReportValuesUpdateViewApply default molecular values
POSTreport/organism-antibiotic-results/OrganismAntibioticSummaryViewBuild and persist organism-antibiotic result summary
GETreport/organism-antibiotic-summary/repair/OrganismAntibioticSummaryRepairViewRepair inconsistent antimicrobiogram data

Entity Creation and Side Effects

ActionSide effects
Create geneAntibioticResistance.bulk_create() saves gene-antibiotic mappings; activity log added; organism cache cleared; gene cache cleared
Update geneSame as create; existing AntibioticResistance mappings for the gene are deleted and recreated
Disable geneActivity log added; organism cache cleared; gene cache cleared; no mapping deletion
Enable geneActivity log added; organism cache cleared; gene cache cleared
Create antibioticOrganismAntibiotics.bulk_create() saves organism mappings; activity log added; antibiotic cache cleared
Update antibioticSame as create; existing organism mappings for the antibiotic are deleted and recreated
Disable antibioticAll OrganismAntibiotics for this antibiotic are cleared (empty mapping list); activity log added; cache cleared
Enable antibioticNo mapping change (after_save returns early on enabled); activity log added; cache cleared
Create organismOrganismAntibiotics.bulk_create() saves antibiotic mappings; save_molecular_antibiotic_mappings() upserts MolecularOrganismAntibiotics; activity log added; organism and gene caches cleared
Update organismSame as create
Disable organismSame mapping clear flow as create; activity log added; organism and gene caches cleared
Enable organismNo mapping change (after_save returns early on enabled); activity log added; cache cleared

On this page