Commits

Viliam Segeda  committed 31fd60e Merge

delete thumbnails in background when deleting report images

  • Participants
  • Parent commits 2ff0ba9, 6ec0f50

Comments (0)

Files changed (2)

File reports/models.py

 from collections import Counter
 from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
 from django.db import models
-from django.db.models.signals import post_save
+from django.db.models.signals import post_save, pre_delete
 from django.dispatch import receiver
 from django.utils.dateformat import format
 from django.utils.translation import ugettext_lazy as _
 from conf import DISTANCE_UNIT_CHOICES, TEAM_CHOICES, NOTEAM
 from utils import make_title, make_stamp
 from reports.utils import make_location_uuid
-from tasks import generate_report_thumbnails, generate_missing_thumbnail
+from tasks import generate_report_thumbnails, generate_missing_thumbnail, delete_report_thumbnails
 
 
 LOCAL_NEWS_COUNT = 5
             if old.team != self.team:
                 error['team'] = [old.team]
             raise ValidationError(error)
-        
+
 
 class ReportImageManager(models.Manager):
 
 
 @receiver(post_save, sender=ReportImage)
 def async_thumbnails(sender, instance, **kwargs):
-    django_rq.enqueue(generate_report_thumbnails, image=instance.image)
+    if instance.image:
+        django_rq.enqueue(generate_report_thumbnails, path=instance.image.path)
     
 @receiver(thumbnail_missed)
 def missing_thumbnails(sender, options, high_resolution, **kwargs):    
     django_rq.enqueue(generate_missing_thumbnail, sender.name, options, high_resolution)
     
-
+@receiver(pre_delete, sender=ReportImage)
+def delete_thumbnails(sender, instance, **kwargs):
+    if instance.image:
+        django_rq.enqueue(delete_report_thumbnails, path=instance.image.path)
     

File reports/tasks.py

 from django_rq import job
+from easy_thumbnails.alias import aliases
 from easy_thumbnails.files import get_thumbnailer, generate_all_aliases
 
 
 @job
-def generate_report_thumbnails(image):
+def generate_report_thumbnails(path):
     """ generate all thumbnails specified in global settings for given image """
-    generate_all_aliases(image.path, include_global=True)
+    generate_all_aliases(path, include_global=True)
     
 @job
 def generate_missing_thumbnail(path, options, high_resolution):
         #TODO: announce missing file
         pass
 
+
+@job
+def delete_report_thumbnails(path):
+    thumbnailer = get_thumbnailer(path)
+    all_options = aliases.all(path, include_global=True)
+    if all_options:
+        for options in all_options.values():
+            thumb = thumbnailer.get_existing_thumbnail(options)
+            if thumb:
+                thumb.storage.delete(thumb.name)