Skip to content
This repository has been archived by the owner on Feb 13, 2019. It is now read-only.

Commit

Permalink
I think we should be clearing crops
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Sinchok committed Mar 16, 2015
1 parent ebec0a7 commit 3dca119
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
11 changes: 1 addition & 10 deletions betty/cropper/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,16 +106,7 @@ def update_selection(request, image_id, ratio_slug):
cache.delete(image.cache_key())
image.save()

ratio_path = os.path.join(image.path(), ratio_slug)
if os.path.exists(ratio_path):
if settings.BETTY_CACHE_FLUSHER:
for crop in os.listdir(ratio_path):
width, format = crop.split(".")
ratio = os.path.basename(ratio_path)
full_url = image.get_absolute_url(ratio=ratio, width=width, format=format)
settings.BETTY_CACHE_FLUSHER(full_url)

shutil.rmtree(ratio_path)
image.clear_crops(ratio_slug)

return HttpResponse(json.dumps(image.to_native()), content_type="application/json")

Expand Down
18 changes: 16 additions & 2 deletions betty/cropper/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,6 @@ def create_from_path(self, path, filename=None, name=None, credit=None):
except OSError as e:
if e.errno != errno.EEXIST:
raise


# Let's make sure we copy the temp file to the source location
source_path = source_upload_to(image, filename)
Expand Down Expand Up @@ -274,6 +273,22 @@ def get_selection(self, ratio):

return selection

def clear_crops(self, ratios=None):
if ratios is None:
ratios = settings.BETTY_RATIOS

for ratio_slug in ratios:
ratio_path = os.path.join(self.path(), ratio_slug)
if os.path.exists(ratio_path):
if settings.BETTY_CACHE_FLUSHER:
for crop in os.listdir(ratio_path):
width, format = crop.split(".")
ratio = os.path.basename(ratio_path)
full_url = self.get_absolute_url(ratio=ratio, width=width, format=format)
settings.BETTY_CACHE_FLUSHER(full_url)

shutil.rmtree(ratio_path)

def get_jpeg_quality(self, width):
quality = None

Expand Down Expand Up @@ -325,7 +340,6 @@ def crop(self, ratio, width, extension, fp=None):
img = img.convert("RGB")
pillow_kwargs = {"format": "jpeg"}


if self.get_jpeg_quality(width):
pillow_kwargs["quality"] = self.get_jpeg_quality(width)
elif img.format == "JPEG":
Expand Down
8 changes: 4 additions & 4 deletions betty/cropper/tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import absolute_import

import itertools
import os
import tempfile

Expand Down Expand Up @@ -39,9 +38,8 @@ def is_optimized(image):
if os.stat(image.source.path).st_size < os.stat(optimized_path).st_size:
# Looks like the original was already compressed, let's bail.
return True

return False

return False


@shared_task
Expand All @@ -52,7 +50,7 @@ def search_image_quality(image_id):
from betty.cropper.models import Image

image = Image.objects.get(id=image_id)

if is_optimized(image):
# If the image is already optimized, let's leave this alone...
return
Expand All @@ -62,4 +60,6 @@ def search_image_quality(image_id):
if width > 0:
quality = detect_optimal_quality(image.optimized.path, width)
image.jpeg_quality_settings[width] = quality

image.clear_crops()
image.save()

0 comments on commit 3dca119

Please sign in to comment.