allow merging of examinations
This commit is contained in:
+54
-3
@@ -1,7 +1,7 @@
|
||||
from collections import defaultdict
|
||||
import json
|
||||
import os
|
||||
from typing import Tuple
|
||||
from typing import Self, Tuple
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from django.db import models
|
||||
from django.http import HttpRequest
|
||||
@@ -72,15 +72,66 @@ class Contrast(models.Model):
|
||||
|
||||
|
||||
class Examination(models.Model):
|
||||
"""
|
||||
|
||||
Currently used for
|
||||
- Atlas series
|
||||
- Longs series
|
||||
- Anatomy questions
|
||||
|
||||
Not used for rapids - yet??
|
||||
"""
|
||||
|
||||
examination = models.CharField(max_length=200, unique=True)
|
||||
|
||||
modality = models.ForeignKey(Modality, on_delete=models.SET_NULL, null=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ("examination",)
|
||||
|
||||
def __str__(self):
|
||||
return self.examination
|
||||
|
||||
class Meta:
|
||||
ordering = ("examination",)
|
||||
def merge_into(self, examination_to_merge_into: Self, delete: bool=True) -> bool:
|
||||
"""Merges the current examination into another
|
||||
|
||||
To do so it replaces all associations (many to many and foreign key relations)
|
||||
|
||||
Returns True if successful
|
||||
"""
|
||||
# Get all atlas series with the examination
|
||||
atlas_series = self.atlas_series_examination.all()
|
||||
longs_series = self.series_examination.all()
|
||||
anatomy_questions = self.anatomyquestion_set.all()
|
||||
|
||||
# Reassign them to the new object
|
||||
for series in atlas_series:
|
||||
series.examination = examination_to_merge_into
|
||||
series.save()
|
||||
|
||||
for series in longs_series:
|
||||
series.examination = examination_to_merge_into
|
||||
series.save()
|
||||
|
||||
for question in anatomy_questions:
|
||||
question.examination = examination_to_merge_into
|
||||
question.save()
|
||||
|
||||
# quick check to make sure nothing has gone wrong and we have hanging
|
||||
# associations
|
||||
querysets = (
|
||||
self.atlas_series_examination.all(),
|
||||
self.series_examination.all(),
|
||||
self.anatomyquestion_set.all(),
|
||||
)
|
||||
|
||||
for q in querysets:
|
||||
if q.count() > 0:
|
||||
return False
|
||||
|
||||
if delete:
|
||||
self.delete()
|
||||
return True
|
||||
|
||||
|
||||
class Site(models.Model):
|
||||
|
||||
Reference in New Issue
Block a user