fix image hashes (possible)

This commit is contained in:
Ross
2023-08-21 14:05:54 +01:00
parent fcd77514eb
commit 48cba9353e
6 changed files with 119 additions and 42 deletions
+27 -12
View File
@@ -32,7 +32,7 @@ from sortedm2m.fields import SortedManyToManyField
import string
from collections import defaultdict
from helpers.images import get_image_hash, image_as_base64, pretty_print_dicom
from helpers.images import get_image_dicom_hash, get_image_hash, image_as_base64, pretty_print_dicom
from generic.models import (
@@ -801,10 +801,9 @@ class SelfReview(models.Model):
class UncategorisedDicom(models.Model):
#We use image to maintain consitency across apps
# We use image to maintain consitency across apps
image = models.FileField(upload_to=uncategorised_dicom_directory_path)
user = models.ForeignKey(
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE,
@@ -816,24 +815,39 @@ class UncategorisedDicom(models.Model):
)
image_md5_hash = models.CharField(max_length=32, null=True, blank=True)
is_dicom = models.BooleanField(default=False)
def check_for_duplicates(self, image_hash):
duplicate = None
if obj := UncategorisedDicom.objects.filter(image_md5_hash=image_hash).first():
duplicate = obj
print(image_hash)
if obj := SeriesImage.objects.filter(image_md5_hash=image_hash).first():
duplicate = obj
return duplicate
def save(self, *args, **kwargs):
"""Override save method to add image hash"""
if self.image:
image_hash, is_dicom = get_image_hash(self.image)
# if self.check_for_duplicates():
# return DuplicateDicom
image_hash = get_image_dicom_hash(self.image)
self.is_dicom = is_dicom
self.image_md5_hash = image_hash
if UncategorisedDicom.objects.filter(image_md5_hash=image_hash):
duplicate = self.check_for_duplicates(image_hash)
if duplicate is not None:
raise DuplicateDicom
if SeriesImage.objects.filter(image_md5_hash=image_hash):
raise DuplicateDicom
# Hack for tests
if image_hash != "12345ABCD":
if image_hash != "1234":
super().save(*args, **kwargs) # Call the "real" save() method.
def get_dicom_info(self):
@@ -869,5 +883,6 @@ class UncategorisedDicom(models.Model):
except pydicom.errors.InvalidDicomError:
return None
class DuplicateDicom(Exception):
pass
pass