This commit is contained in:
Ross
2023-06-12 10:59:36 +01:00
parent 43c984d5aa
commit 796ec1a072
6 changed files with 381 additions and 432 deletions
@@ -0,0 +1,18 @@
# Generated by Django 4.1.4 on 2023-06-12 09:55
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('longs', '0069_alter_long_feedback'),
]
operations = [
migrations.AlterField(
model_name='longseries',
name='open_access',
field=models.BooleanField(default=True, help_text='If a series should be freely available to browse'),
),
]
+3 -180
View File
@@ -32,13 +32,12 @@ from generic.models import (
CidUserGroup,
ExamUserStatus,
Examination,
# Condition,
# Sign,
ExamBase,
Plane,
Contrast,
QuestionBase,
QuestionNote,
SeriesBase,
UserAnswerBase,
UserUserGroup,
)
@@ -70,14 +69,6 @@ def image_directory_path(instance, filename):
return "longs/picture/{0}".format(filename)
def findMiddle(input_list):
middle = float(len(input_list)) / 2
if middle % 2 != 0:
return input_list[int(middle - 0.5)]
else:
return input_list[int(middle)]
return (input_list[int(middle)], input_list[int(middle - 1)])
@reversion.register
class Long(QuestionBase):
@@ -380,7 +371,7 @@ class LongSeriesImage(models.Model):
@reversion.register
class LongSeries(models.Model):
class LongSeries(SeriesBase):
modality = models.ForeignKey(
Modality, related_name="series_modality", on_delete=models.SET_NULL, null=True
)
@@ -407,16 +398,6 @@ class LongSeries(models.Model):
null=True,
blank=True,
)
# long = models.ManyToManyField(
# "Long",
# help_text="The question(s) this series should be associated with",
# related_name="series",
# blank=True,
# )
description = models.TextField(
blank=True,
help_text="Description of stack, for admin organisation, will not be visible when taking",
)
author = models.ManyToManyField(
settings.AUTH_USER_MODEL,
@@ -424,169 +405,11 @@ class LongSeries(models.Model):
related_name="long_series",
)
open_access = models.BooleanField(
help_text="If a question should be freely available to browse", default=True
)
def __str__(self):
# if self.long:
# long_id = ", ".format([long.pk for long in self.long.all()])
# # long_id = self.long.pk
# else:
# long_id = "None"
return "{}/{} : {}".format(self.pk, self.get_examination(), self.description)
def get_author_objects(self):
"""Returns a comma seperated text list of authors"""
if self.author:
return self.author.all()
else:
return ["None"]
if self.long:
authors = [i for i in self.long.author.all()]
else:
authors = []
return authors
def get_author_display(self):
return ", ".join([i.username for i in self.get_author_objects()])
def get_examination(self):
"""Returns a comma seperated text list of regions"""
return str(self.examination)
def get_examination_full(self):
examination = ""
plane = ""
contrast = ""
if self.examination:
examination = self.examination
if self.plane:
plane = " {}".format(self.plane)
if self.contrast:
contrast = " {}".format(self.contrast)
return "{}{}{}".format(examination, plane, contrast)
def get_absolute_url(self):
return reverse("longs:long_series_detail", kwargs={"pk": self.pk})
def get_image_urls(self, feedback=False):
images = [f"{REMOTE_URL}{i.image.url}" for i in self.images.all()]
return ",".join(images)
def get_image_url_array(self, feedback=False):
images = [f"{REMOTE_URL}{i.image.url}" for i in self.images.all()]
return json.dumps(images)
def get_thumbnail(self):
images = self.images.all()
if len(images) < 1:
return "No images", 0
img = findMiddle(images).image
try:
thumbnailer = get_thumbnailer(img)
thumbnail = thumbnailer["exam-list"]
except InvalidImageFormatError:
return format_html('<span title="{}">Invalid image url<span>', img), len(
images
)
return format_html('<img src="/media/{}" />', thumbnail), len(images)
def get_block(self):
examination = self.get_examination_full()
thumb, image_number = self.get_thumbnail()
return format_html(
"<div>{}<br/>{}<br/>Images: {}</div>", examination, thumb, image_number
)
def order_by_upload_filename(self):
images = self.images.all()
filenames = []
map = {}
for i in images:
filenames.append(i.upload_filename)
map[i.upload_filename] = i
filenames = sorted(filenames)
n = 1
for f in filenames:
i = map[f]
i.position = n
i.save()
n = n + 1
def order_by_dicom(self, field="SliceLocation"):
images = self.images.all()
files = []
for i in images:
files.append((i, pydicom.dcmread(i.image.path)))
# print("file count: {}".format(len(files)))
# skip files with no SliceLocation (eg scout views)
slices = []
map = {}
skipcount = 0
for i, f in files:
if hasattr(f, field):
slices.append(f)
# map[f.SliceLocation] = i
map[f[field].value] = i
else:
skipcount = skipcount + 1
print("skipped, no {}: {}".format(field, skipcount))
# ensure they are in the correct order
slices = sorted(slices, key=lambda s: s[field].value)
# print(slices)
n = 1
for f in slices:
i = map[f[field].value]
i.position = n
i.save()
n = n + 1
def get_total_image_size(self):
images = self.images.all()
size = 0
for i in images:
size += i.image.size
return size
# class LongImage(models.Model):
# long = models.ForeignKey(Long,
# related_name="images",
# on_delete=models.CASCADE)
# image = models.ImageField(upload_to=image_directory_path, storage=image_storage)
#
# feedback_image = models.BooleanField(default=False)
#
# def image_tag(self):
# if self.image:
# return mark_safe(
# '<img src="{}" class="admin-long-image" /><span class="admin-long-image-info">Click and hold to zoom<span>'
# .format(self.image.url))
# else:
# return ""
#
# image_tag.short_description = 'Image'
return reverse("longs:series_detail", kwargs={"pk": self.pk})
class LongCreationDefault(models.Model):