Browse Source

Allow disabling deletion of image files and thumbnails

Oscar by default deleted image files and thumbnails. That is usually
fine, but has been an issue when using remote storages like S3. This
change also makes the only remaining import from sorl optional, hence
paving the way to making it completely optional.

Fixes #1373.
master
Maik Hoepfel 11 years ago
parent
commit
0cc0518cc2
4 changed files with 52 additions and 33 deletions
  1. 10
    1
      docs/source/ref/settings.rst
  2. 5
    0
      docs/source/releases/v0.8.rst
  3. 36
    32
      oscar/apps/catalogue/receivers.py
  4. 1
    0
      oscar/defaults.py

+ 10
- 1
docs/source/ref/settings.rst View File

@@ -85,7 +85,7 @@ The number of products to paginate by.
85 85
 .. _oscar_search_facets:
86 86
 
87 87
 ``OSCAR_SEARCH_FACETS``
88
-------------------------------
88
+-----------------------
89 89
 
90 90
 A dictionary that specifies the facets to use with the search backend.  It
91 91
 needs to be a dict with keys ``fields`` and ``queries`` for field- and
@@ -405,6 +405,15 @@ The folder name can contain date format strings as described in the `Django Docs
405 405
 
406 406
 .. _`Django Docs`: https://docs.djangoproject.com/en/dev/ref/models/fields/#filefield
407 407
 
408
+``OSCAR_DELETE_IMAGE_FILES``
409
+----------------------------
410
+
411
+Default: ``True``
412
+
413
+If enabled, a ``post_delete`` hook will attempt to delete any image files and
414
+created thumbnails when a model with an ``ImageField`` is deleted. This is
415
+usually desired, but might not be what you want when using a remote storage.
416
+
408 417
 
409 418
 ``OSCAR_PROMOTION_FOLDER``
410 419
 --------------------------

+ 5
- 0
docs/source/releases/v0.8.rst View File

@@ -171,6 +171,11 @@ Minor changes
171 171
 * The forms for the order dashboard views are now loaded dynamically so they
172 172
   can be overridden.
173 173
 
174
+* Introduced a ``OSCAR_DELETE_IMAGE_FILES`` settings which makes deleting
175
+  image files and thumbnails upon deleting of a model with an ``ImageField``
176
+  optional. It usually is desired behaviour, but can slow down an app when
177
+  using a remote storage.
178
+
174 179
 .. _incompatible_changes_in_0.8:
175 180
 
176 181
 Backwards incompatible changes in 0.8

+ 36
- 32
oscar/apps/catalogue/receivers.py View File

@@ -1,33 +1,37 @@
1 1
 # -*- coding: utf-8 -*-
2
-from oscar.core.loading import get_model
3
-
4
-from django.db import models
5
-from django.db.models.signals import post_delete
6
-
7
-from sorl import thumbnail
8
-from sorl.thumbnail.helpers import ThumbnailError
9
-
10
-ProductImage = get_model('catalogue', 'ProductImage')
11
-Category = get_model('catalogue', 'Category')
12
-
13
-
14
-def delete_image_files(sender, instance, **kwargs):
15
-    """
16
-    Deletes the original image, created thumbnails, and any entries
17
-    in sorl's key-value store.
18
-    """
19
-    image_fields = (models.ImageField, thumbnail.ImageField)
20
-    for field in instance._meta.fields:
21
-        if isinstance(field, image_fields):
22
-            # Make Django return ImageFieldFile instead of ImageField
23
-            fieldfile = getattr(instance, field.name)
24
-            try:
25
-                thumbnail.delete(fieldfile)
26
-            except ThumbnailError:
27
-                pass
28
-
29
-
30
-# connect for all models with ImageFields - add as needed
31
-models_with_images = [ProductImage, Category]
32
-for sender in models_with_images:
33
-    post_delete.connect(delete_image_files, sender=sender)
2
+
3
+from django.conf import settings
4
+
5
+if settings.OSCAR_DELETE_IMAGE_FILES:
6
+
7
+    from oscar.core.loading import get_model
8
+
9
+    from django.db import models
10
+    from django.db.models.signals import post_delete
11
+
12
+    from sorl import thumbnail
13
+    from sorl.thumbnail.helpers import ThumbnailError
14
+
15
+    ProductImage = get_model('catalogue', 'ProductImage')
16
+    Category = get_model('catalogue', 'Category')
17
+
18
+    def delete_image_files(sender, instance, **kwargs):
19
+        """
20
+        Deletes the original image, created thumbnails, and any entries
21
+        in sorl's key-value store.
22
+        """
23
+        image_fields = (models.ImageField, thumbnail.ImageField)
24
+        for field in instance._meta.fields:
25
+            if isinstance(field, image_fields):
26
+                # Make Django return ImageFieldFile instead of ImageField
27
+                fieldfile = getattr(instance, field.name)
28
+                try:
29
+                    thumbnail.delete(fieldfile)
30
+                except ThumbnailError:
31
+                    pass
32
+
33
+
34
+    # connect for all models with ImageFields - add as needed
35
+    models_with_images = [ProductImage, Category]
36
+    for sender in models_with_images:
37
+        post_delete.connect(delete_image_files, sender=sender)

+ 1
- 0
oscar/defaults.py View File

@@ -22,6 +22,7 @@ OSCAR_DEFAULT_CURRENCY = 'GBP'
22 22
 # Paths
23 23
 OSCAR_IMAGE_FOLDER = 'images/products/%Y/%m/'
24 24
 OSCAR_PROMOTION_FOLDER = 'images/promotions/'
25
+OSCAR_DELETE_IMAGE_FILES = True
25 26
 
26 27
 # Copy this image from oscar/static/img to your MEDIA_ROOT folder.
27 28
 # It needs to be there so Sorl can resize it.

Loading…
Cancel
Save