continue building examcollections

This commit is contained in:
Ross
2024-02-05 18:30:35 +00:00
parent 269a86c25b
commit b1da429036
30 changed files with 546 additions and 70 deletions
+82 -20
View File
@@ -39,6 +39,7 @@ from easy_thumbnails.exceptions import InvalidImageFormatError
import pydicom
import dicognito.anonymizer
from django.contrib.auth.models import User
from django.forms.models import model_to_dict
def findMiddle(input_list):
@@ -49,8 +50,6 @@ def findMiddle(input_list):
return input_list[int(middle)]
class Modality(models.Model):
modality = models.CharField(max_length=200, unique=True)
short_code = models.CharField(max_length=5, unique=True, null=True)
@@ -223,7 +222,6 @@ class SeriesImageBase(models.Model):
help_text="Set to true if the file this object refers to has been removed (either from series truncation or merging)",
)
class Meta:
ordering = ["position"]
abstract = True
@@ -244,7 +242,9 @@ class SeriesImageBase(models.Model):
def generate_md5_hash(self):
if not self.removed:
image_hash, is_dicom = get_image_hash(self.image, hash_type="md5", direct_pixel_data=False)
image_hash, is_dicom = get_image_hash(
self.image, hash_type="md5", direct_pixel_data=False
)
self.is_dicom = is_dicom
self.image_md5_hash = image_hash
@@ -253,7 +253,9 @@ class SeriesImageBase(models.Model):
def generate_blake3_hash(self):
if not self.removed:
image_blake3_hash, is_dicom = get_image_hash(self.image, hash_type="blake3", direct_pixel_data=True)
image_blake3_hash, is_dicom = get_image_hash(
self.image, hash_type="blake3", direct_pixel_data=True
)
self.is_dicom = is_dicom
self.image_blake3_hash = image_blake3_hash
@@ -262,19 +264,21 @@ class SeriesImageBase(models.Model):
def save(self, *args, **kwargs):
"""Override save method to add image hash"""
if self.image:
#if not self.image_md5_hash:
# if not self.image_md5_hash:
# image_hash, is_dicom = get_image_hash(self.image, hash_type="md5", direct_pixel_data=False)
# self.is_dicom = is_dicom
# self.image_md5_hash = image_hash
if not self.image_blake3_hash:
image_blake3_hash, is_dicom = get_image_hash(self.image, hash_type="blake3", direct_pixel_data=True)
image_blake3_hash, is_dicom = get_image_hash(
self.image, hash_type="blake3", direct_pixel_data=True
)
self.is_dicom = is_dicom
self.image_blake3_hash = image_blake3_hash
## Hack for tests
#if image_hash != "12345ABCD":
# if image_hash != "12345ABCD":
# super().save(*args, **kwargs) # Call the "real" save() method.
super().save(*args, **kwargs) # Call the "real" save() method.
@@ -301,12 +305,24 @@ class SeriesBase(models.Model):
modified_date = models.DateTimeField(auto_now=True)
class SeriesModifiedChocies(models.TextChoices):
NO = "NO", "Not modified",
TR = "TR", "Truncated",
RE = "RE", "Resliced/resampled",
NO = (
"NO",
"Not modified",
)
TR = (
"TR",
"Truncated",
)
RE = (
"RE",
"Resliced/resampled",
)
modified = models.CharField(max_length=2, default=SeriesModifiedChocies.NO, choices=SeriesModifiedChocies.choices)
modified = models.CharField(
max_length=2,
default=SeriesModifiedChocies.NO,
choices=SeriesModifiedChocies.choices,
)
class Meta:
abstract = True
@@ -317,7 +333,7 @@ class SeriesBase(models.Model):
def check_user_can_edit(self, user: User):
if user.is_superuser:
return True
if user in self.get_author_objects():
return True
@@ -558,7 +574,7 @@ class ExamOrCollectionGenericBase(models.Model, AuthorMixin):
def check_user_can_edit(self, user: User):
if user.is_superuser:
return True
if user in self.get_author_objects():
return True
@@ -720,6 +736,48 @@ class ExamOrCollectionGenericBase(models.Model, AuthorMixin):
content_type=content_type, object_id=self.pk, user_user=user_user
)
def clone_model(self):
M2M_fields = (
"exam_questions",
"author",
"valid_cid_users",
"valid_user_users",
"cid_user_groups",
"user_user_groups",
)
FK_fields = (
"examcollection",
)
model_dict = model_to_dict(self)
m2m_to_update = {}
for field in M2M_fields:
m2m_to_update[field] = model_dict[field]
del model_dict[field]
fk_to_update = {}
for field in FK_fields:
fk_to_update[field] = model_dict[field]
del model_dict[field]
cloned_model = self.__class__(**model_dict)
cloned_model.pk = None
for field in fk_to_update:
setattr(cloned_model, f"{field}_id", fk_to_update[field])
cloned_model.save()
for field in m2m_to_update:
rel = getattr(cloned_model, field)
rel.clear()
for item in m2m_to_update[field]:
rel.add(item)
return cloned_model
class ExamBase(ExamOrCollectionGenericBase):
exam_mode = models.BooleanField(
@@ -831,7 +889,7 @@ class ExamBase(ExamOrCollectionGenericBase):
def get_question_index(self, question):
return list(self.exam_questions.all()).index(question)
def get_question_by_index(self, index:int):
def get_question_by_index(self, index: int):
# There must be a better way to do this
return list(self.exam_questions.all())[index]
@@ -890,7 +948,7 @@ class ExamBase(ExamOrCollectionGenericBase):
"""
if self.app_name == "rapids":
html_msg =f"{html_msg}<br/>{rapids_extra}"
html_msg = f"{html_msg}<br/>{rapids_extra}"
return msg, html_msg
@@ -914,7 +972,6 @@ class ExamBase(ExamOrCollectionGenericBase):
else:
extra = "No supervisor"
if additional_emails is not None:
emails.extend(additional_emails)
@@ -1505,10 +1562,13 @@ def create_user_profile(sender, instance, created, **kwargs):
def save_user_profile(sender, instance, **kwargs):
instance.userprofile.save()
class ExamCollection(models.Model, AuthorMixin):
name = models.CharField(max_length=255)
date = models.DateField(blank=True,null=True)
date = models.DateField(blank=True, null=True)
archive = models.BooleanField(default=False)
author = models.ManyToManyField(
settings.AUTH_USER_MODEL,
@@ -1518,4 +1578,6 @@ class ExamCollection(models.Model, AuthorMixin):
def __str__(self):
return f"{self.name} [{self.date}]"
def get_absolute_url(self):
return reverse("generic:examcollection_detail", kwargs={"pk": self.pk})