Too many changes :(
This commit is contained in:
+46
-1
@@ -31,6 +31,9 @@ from rapids.models import Abnormality, Examination, Region
|
||||
|
||||
from helpers.images import get_image_hash, image_as_base64
|
||||
from django.utils.html import format_html
|
||||
from helpers.images import combine_dicom_images_side_by_side
|
||||
from django.core.files import File
|
||||
import tempfile
|
||||
|
||||
|
||||
def image_directory_path(instance, filename):
|
||||
@@ -108,6 +111,43 @@ class Question(QuestionBase):
|
||||
except pydicom.errors.InvalidDicomError:
|
||||
pass
|
||||
|
||||
def combine_images_side_by_side(self, image1_id, image2_id):
|
||||
"""
|
||||
Combines two DICOM images side by side and saves the new image.
|
||||
Marks the original images as feedback images.
|
||||
"""
|
||||
|
||||
img1 = self.images.get(id=image1_id)
|
||||
img2 = self.images.get(id=image2_id)
|
||||
|
||||
dicom_path1 = os.path.join(settings.MEDIA_ROOT, img1.image.name)
|
||||
dicom_path2 = os.path.join(settings.MEDIA_ROOT, img2.image.name)
|
||||
|
||||
# Combine images (returns a FileDataset)
|
||||
combined_dataset = combine_dicom_images_side_by_side(dicom_path1, dicom_path2)
|
||||
|
||||
# Save new image directly to storage
|
||||
combined_image_name = f"combined_{img1.filename}_{img2.filename}.dcm"
|
||||
combined_image_path = os.path.join("shorts/picture", combined_image_name)
|
||||
full_path = os.path.join(settings.MEDIA_ROOT, combined_image_path)
|
||||
combined_dataset.save_as(full_path)
|
||||
with open(full_path, "rb") as f:
|
||||
new_image = QuestionImage(
|
||||
question=self,
|
||||
image=File(f, name=combined_image_path),
|
||||
feedback_image=False,
|
||||
is_dicom=True,
|
||||
)
|
||||
new_image.save()
|
||||
|
||||
# Mark originals as feedback images
|
||||
img1.feedback_image = True
|
||||
img1.save()
|
||||
img2.feedback_image = True
|
||||
img2.save()
|
||||
|
||||
return new_image
|
||||
|
||||
def check_user_can_edit(self, user):
|
||||
if user.is_superuser:
|
||||
return True
|
||||
@@ -169,6 +209,11 @@ class Question(QuestionBase):
|
||||
user_answers = set([i.answer for i in queryset])
|
||||
|
||||
return user_answers
|
||||
|
||||
def get_examination_string(self):
|
||||
"""Returns a string of the examinations associated with the question."""
|
||||
examinations = self.examination.all().values_list("examination", flat=True)
|
||||
return ", ".join(examinations)
|
||||
|
||||
|
||||
class QuestionImage(models.Model):
|
||||
@@ -358,7 +403,7 @@ class Exam(ExamBase):
|
||||
if i.description:
|
||||
image_titles.append(i.description)
|
||||
else:
|
||||
image_titles.append("")
|
||||
image_titles.append(q.get_examination_string())
|
||||
|
||||
exam_questions[q.id] = {
|
||||
"images": images,
|
||||
|
||||
Reference in New Issue
Block a user