Skip to content

Commit c170ece

Browse files
authored
Merge pull request #10 from KWB-R/feature/many-to-many-references
remove one2one fields to References in QMRATreatment
2 parents 3916820 + f135f27 commit c170ece

6 files changed

Lines changed: 53 additions & 22 deletions

File tree

infra/helm/qmra/templates/deployment.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ spec:
3737
- name: qmra-default
3838
mountPath: {{ .Values.qmra_default.mount_path }}
3939
command: [ python, manage.py, migrate, --database, qmra ]
40+
- name: export-default
41+
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
42+
envFrom:
43+
- configMapRef:
44+
name: {{ .Values.configmap_name }}
45+
volumeMounts:
46+
- name: qmra-default
47+
mountPath: {{ .Values.qmra_default.mount_path }}
48+
command: [ python, manage.py, export_default ]
4049
- name: move-static
4150
image: "{{ .Values.image.repository }}:{{ .Values.image.tag }}"
4251
envFrom:

qmra/risk_assessment/admin.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,10 @@ class QMRATreatmentAdmin(admin.ModelAdmin):
6363
fields = [
6464
("name", "group"),
6565
("bacteria_min", "bacteria_max"),
66-
"bacteria_reference",
6766
"bacteria_references",
6867
("viruses_min", "viruses_max"),
69-
"viruses_reference",
7068
"viruses_references",
7169
("protozoa_min", "protozoa_max"),
72-
"protozoa_reference",
7370
"protozoa_references"
7471
]
7572
filter_horizontal = ["bacteria_references", "viruses_references", "protozoa_references"]
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Generated by Django 5.0.6 on 2026-01-22 09:07
2+
3+
from django.db import migrations
4+
5+
6+
class Migration(migrations.Migration):
7+
8+
dependencies = [
9+
('risk_assessment', '0009_auto_20260121_1454'),
10+
]
11+
12+
operations = [
13+
migrations.RemoveField(
14+
model_name='qmratreatment',
15+
name='bacteria_reference',
16+
),
17+
migrations.RemoveField(
18+
model_name='qmratreatment',
19+
name='protozoa_reference',
20+
),
21+
migrations.RemoveField(
22+
model_name='qmratreatment',
23+
name='viruses_reference',
24+
),
25+
]

qmra/risk_assessment/qmra_models.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -268,54 +268,54 @@ class QMRATreatment(models.Model):
268268
description: str = models.CharField(max_length=512)
269269
bacteria_min: Optional[float] = models.FloatField(blank=True, null=True)
270270
bacteria_max: Optional[float] = models.FloatField(blank=True, null=True)
271-
bacteria_reference = models.ForeignKey(QMRAReference, blank=True, null=True, on_delete=models.CASCADE,
272-
related_name="bacteria_lrv")
273271
bacteria_references = models.ManyToManyField(QMRAReference, related_name="bacteria_lrvs")
272+
_bacteria_references_tmp: list[str]
274273

275274
viruses_min: Optional[float] = models.FloatField(blank=True, null=True)
276275
viruses_max: Optional[float] = models.FloatField(blank=True, null=True)
277-
viruses_reference = models.ForeignKey(QMRAReference, blank=True, null=True, on_delete=models.CASCADE,
278-
related_name="viruses_lrv")
279276
viruses_references = models.ManyToManyField(QMRAReference, related_name="viruses_lrvs")
277+
_viruses_references_tmp: list[str]
280278

281279
protozoa_min: Optional[float] = models.FloatField(blank=True, null=True)
282280
protozoa_max: Optional[float] = models.FloatField(blank=True, null=True)
283-
protozoa_reference = models.ForeignKey(QMRAReference, blank=True, null=True, on_delete=models.CASCADE,
284-
related_name="protozoa_lrv")
285281
protozoa_references = models.ManyToManyField(QMRAReference, related_name="protozoa_lrvs")
282+
_protozoa_references_tmp: list[str]
286283

287284
@classmethod
288285
def from_dict(cls, data):
289-
return QMRATreatment(
286+
t = QMRATreatment(
290287
pk=data["id"],
291288
name=data['name'],
292289
group=data['group'],
293290
description=data['description'],
294291
bacteria_min=data['bacteria_min'],
295292
bacteria_max=data['bacteria_max'],
296-
# TODO: *_referenceS (m2m fields...)
297-
bacteria_reference_id=int(data["bacteria_reference"]) \
298-
if data["bacteria_reference"] is not None else None,
299293
viruses_min=data['viruses_min'],
300294
viruses_max=data['viruses_max'],
301-
viruses_reference_id=int(data["viruses_reference"]) \
302-
if data["viruses_reference"] is not None else None,
303295
protozoa_min=data['protozoa_min'],
304296
protozoa_max=data['protozoa_max'],
305-
protozoa_reference_id=int(data["protozoa_reference"]) \
306-
if data["protozoa_reference"] is not None else None,
307297
)
298+
t._bacteria_references_tmp = data.get("bacteria_references", [])
299+
t._viruses_references_tmp = data.get("viruses_references", [])
300+
t._protozoa_references_tmp = data.get("protozoa_references", [])
301+
return t
308302

309303
def to_dict(self):
310304
data = model_to_dict(self)
311-
data["bacteria_reference"] = str(self.bacteria_reference.pk) if self.bacteria_reference is not None else None
312305
data["bacteria_references"] = [str(ref.pk) for ref in self.bacteria_references.all()]
313-
data["viruses_reference"] = str(self.viruses_reference.pk) if self.viruses_reference is not None else None
314306
data["viruses_references"] = [str(ref.pk) for ref in self.viruses_references.all()]
315-
data["protozoa_reference"] = str(self.protozoa_reference.pk) if self.protozoa_reference is not None else None
316307
data["protozoa_references"] = [str(ref.pk) for ref in self.protozoa_references.all()]
317308
return data
318309

310+
def save(
311+
self, force_insert=False, force_update=False, using=None, update_fields=None
312+
):
313+
super().save(force_insert, force_update, using, update_fields)
314+
self.bacteria_references.set(self._bacteria_references_tmp)
315+
self.viruses_references.set(self._viruses_references_tmp)
316+
self.protozoa_references.set(self._protozoa_references_tmp)
317+
return self
318+
319319

320320
class QMRATreatments(StaticEntity):
321321
source = "qmra/static/data/default-treatments.json"

qmra/risk_assessment/templates/treatments-form-js.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@
389389
for (const treatment of this.treatments){
390390
if (!uniques.has(treatment.name)){
391391
const all_refs_id = new Set();
392-
for (const r of [...treatment.viruses_references, ...treatment.bacteria_references, ...treatment.protozoa_references]){
392+
for (const r of [...treatment?.viruses_references, ...treatment?.bacteria_references, ...treatment?.protozoa_references]){
393393
all_refs_id.add(r);
394394
}
395395
const superscripts = [...all_refs_id].map((r, i) => { return `<sup>[${i+1}]</sup>` }).join('');

0 commit comments

Comments
 (0)