diff --git a/rapids/migrations/0037_auto_20211121_1020.py b/rapids/migrations/0037_auto_20211121_1020.py new file mode 100644 index 00000000..869b06e1 --- /dev/null +++ b/rapids/migrations/0037_auto_20211121_1020.py @@ -0,0 +1,23 @@ +# Generated by Django 3.2.4 on 2021-11-21 10:20 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('rapids', '0036_alter_exam_open_access'), + ] + + operations = [ + migrations.AddField( + model_name='rapidimage', + name='image_md5_hash', + field=models.CharField(max_length=32, null=True), + ), + migrations.AddField( + model_name='rapidimage', + name='is_dicom', + field=models.BooleanField(default=False), + ), + ] diff --git a/rapids/models.py b/rapids/models.py index 3f1f9d75..00b5d394 100644 --- a/rapids/models.py +++ b/rapids/models.py @@ -29,6 +29,11 @@ import reversion import json +import hashlib + +import pydicom +import pydicom.errors + image_storage = FileSystemStorage( # Physical file location ROOT location=u"{0}".format(settings.MEDIA_ROOT), @@ -432,6 +437,10 @@ class RapidImage(models.Model): feedback_image = models.BooleanField(default=False) + image_md5_hash = models.CharField(max_length=32, null=True) + + is_dicom = models.BooleanField(default=False) + def image_tag(self): if self.image: return mark_safe( @@ -442,6 +451,19 @@ class RapidImage(models.Model): image_tag.short_description = 'Image' + def save(self, *args, **kwargs): + """Override save method to add image hash""" + if self.image: + # Try and read the file as a dicom + try: + hash = ",".join(pydicom.dcmread(self.image).pixel_array.tostring()) + self.is_dicom = True + except pydicom.errors.InvalidImageFormatError: + hash = hashlib.md5(self.image).hexdigest() + + self.image_md5_hash = hash + super().save(*args, **kwargs) # Call the "real" save() method. + class RapidCreationDefault(models.Model): #author = models.OneToOneField(User, diff --git a/requirements.txt b/requirements.txt index a766e7f9..05206e9d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -22,3 +22,4 @@ django-tinymce django-zipview pymemcache django-autocomplete-light +django-querysetsequence \ No newline at end of file