diff --git a/atlas/migrations/0065_series_total_image_size.py b/atlas/migrations/0065_series_total_image_size.py new file mode 100644 index 00000000..f9a4b4b7 --- /dev/null +++ b/atlas/migrations/0065_series_total_image_size.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.2 on 2024-12-18 23:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('atlas', '0064_casecollection_exam_open_access'), + ] + + operations = [ + migrations.AddField( + model_name='series', + name='total_image_size', + field=models.BigIntegerField(blank=True, help_text='Stores a (cached) total size of images within the series', null=True), + ), + ] diff --git a/atlas/migrations/0066_case_total_images_series_size.py b/atlas/migrations/0066_case_total_images_series_size.py new file mode 100644 index 00000000..d9c40c15 --- /dev/null +++ b/atlas/migrations/0066_case_total_images_series_size.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.2 on 2024-12-18 23:59 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('atlas', '0065_series_total_image_size'), + ] + + operations = [ + migrations.AddField( + model_name='case', + name='total_images_series_size', + field=models.BigIntegerField(blank=True, null=True), + ), + ] diff --git a/atlas/models.py b/atlas/models.py index edb848a1..80adf126 100644 --- a/atlas/models.py +++ b/atlas/models.py @@ -390,6 +390,8 @@ class Case(models.Model, AuthorMixin, QuestionMixin): resource = models.ManyToManyField("Resource", through="CaseResource") + total_images_series_size = models.BigIntegerField(null=True, blank=True) + def get_app_name(self): return "atlas" @@ -495,6 +497,17 @@ class Case(models.Model, AuthorMixin, QuestionMixin): def get_series(self): return self.series.all().prefetch_related("images", "examination", "plane") + def get_total_series_images_size(self, refresh=False): + if self.total_images_series_size is not None and not refresh: + return self.total_images_series_size + + series = self.series.all() + size = sum(s.get_total_image_size(refresh=refresh) for s in series) + + return size + + + def extract_image_dicom_json_from_ds(ds, url, image_index): to_keep = [ diff --git a/atlas/templates/atlas/case_display_block.html b/atlas/templates/atlas/case_display_block.html index c60420e7..7436858f 100755 --- a/atlas/templates/atlas/case_display_block.html +++ b/atlas/templates/atlas/case_display_block.html @@ -188,3 +188,6 @@
Author(s): {% for author in case.author.all %} {{author}}, {% endfor %}
+ + +Case size: {{ case.get_total_series_images_size | filesizeformat }}
diff --git a/generic/models.py b/generic/models.py index fe990d3d..d21dfb8c 100644 --- a/generic/models.py +++ b/generic/models.py @@ -336,6 +336,8 @@ class SeriesBase(models.Model): choices=SeriesModifiedChocies.choices, ) + total_image_size = models.BigIntegerField(null=True, blank=True, help_text="Stores a (cached) total size of images within the series") + class Meta: abstract = True @@ -531,7 +533,14 @@ class SeriesBase(models.Model): i.save() n = n + 1 - def get_total_image_size(self): + def get_total_image_size(self, refresh=False): + """Returns the total size of all images in the series. + + If refresh is set to True the size will be recalculated and saved to the database + (in the field 'total_image_size').""" + if not refresh and self.total_image_size: + return self.total_image_size + images = self.images.filter(removed=False) size = 0 @@ -539,6 +548,9 @@ class SeriesBase(models.Model): for i in images: size += i.get_file_size() + self.total_image_size = size + self.save() + return size def anonymise_images(self): diff --git a/longs/migrations/0032_longseries_total_image_size.py b/longs/migrations/0032_longseries_total_image_size.py new file mode 100644 index 00000000..48c4f869 --- /dev/null +++ b/longs/migrations/0032_longseries_total_image_size.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.2 on 2024-12-18 23:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('longs', '0031_exam_results_supervisor_visible'), + ] + + operations = [ + migrations.AddField( + model_name='longseries', + name='total_image_size', + field=models.BigIntegerField(blank=True, help_text='Stores a (cached) total size of images within the series', null=True), + ), + ]