From 6c70d277aa7dfc332209bdf41f2e09a356a159fa Mon Sep 17 00:00:00 2001 From: Chris Barna Date: Mon, 23 Mar 2015 12:16:27 -0400 Subject: [PATCH 1/5] Refactor get_crop to always lookup the thumb from the database. --- cropduster/templatetags/cropduster_tags.py | 30 ++++++++++------------ 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/cropduster/templatetags/cropduster_tags.py b/cropduster/templatetags/cropduster_tags.py index 1ea7c385..44430ea4 100644 --- a/cropduster/templatetags/cropduster_tags.py +++ b/cropduster/templatetags/cropduster_tags.py @@ -3,7 +3,7 @@ import six from django import template -from cropduster.models import Image +from cropduster.models import Image, Thumb from cropduster.resizing import Size @@ -42,29 +42,27 @@ def get_crop(image, crop_name, size=None, attribution=None, exact_size=False): values so `exact_size` gives you access to the actual size of an image. """ - if not image: + if not image or not image.related_object: return if size: warnings.warn("The size kwarg is deprecated.", DeprecationWarning) + if exact_size: + warnings.warn("The exact_size kwarg is deprecated.", DeprecationWarning) + data = {} - data['url'] = getattr(Image.get_file_for_size(image, crop_name), 'url', None) - - if not exact_size: - sizes = Size.flatten(image.sizes) - try: - size = six.next(size_obj for size_obj in sizes if size_obj.name == crop_name) - except StopIteration: - pass + try: + thumb = image.related_object.thumbs.get(name=crop_name) + except Thumb.DoesNotExist: + if crop_name == "original": + thumb = image.related_object else: - if size.width: - data['width'] = size.width + return data - if size.height: - data['height'] = size.height - elif image.related_object: - data['width'], data['height'] = image.related_object.get_image_size(size_name=crop_name) + data["url"] = thumb.url + data["width"] = thumb.width + data["height"] = thumb.height if attribution and image.related_object: data.update({ From a3001cfd7bbc637ee95c3dd1dc7c1a94895d3bc9 Mon Sep 17 00:00:00 2001 From: Chris Barna Date: Mon, 23 Mar 2015 13:13:21 -0400 Subject: [PATCH 2/5] Remove all kwargs from get_crop. --- cropduster/templatetags/cropduster_tags.py | 38 +++++++--------------- 1 file changed, 12 insertions(+), 26 deletions(-) diff --git a/cropduster/templatetags/cropduster_tags.py b/cropduster/templatetags/cropduster_tags.py index 44430ea4..cadc2153 100644 --- a/cropduster/templatetags/cropduster_tags.py +++ b/cropduster/templatetags/cropduster_tags.py @@ -11,11 +11,11 @@ @register.assignment_tag -def get_crop(image, crop_name, size=None, attribution=None, exact_size=False): +def get_crop(image, crop_name, **kwargs): """ Get the crop of an image. Usage: - {% get_crop article.image 'square_thumbnail' attribution=1 exact_size=1 as img %} + {% get_crop article.image 'square_thumbnail' as img %} will assign to `img` a dictionary that looks like: @@ -31,25 +31,13 @@ def get_crop(image, crop_name, size=None, attribution=None, exact_size=False): For use in an image tag or style block like: - - The `size` kwarg is deprecated. - - Omitting the `attribution` kwarg will omit the attribution, attribution_link, - and caption. - - Omitting the `exact_size` kwarg will return the width and/or the height of - the crop size that was passed in. Crop sizes do not always require both - values so `exact_size` gives you access to the actual size of an image. """ if not image or not image.related_object: return - if size: - warnings.warn("The size kwarg is deprecated.", DeprecationWarning) - - if exact_size: - warnings.warn("The exact_size kwarg is deprecated.", DeprecationWarning) + if len(kwargs) > 0: + warnings.warn("All get_crop kwargs have been deprecated", DeprecationWarning) data = {} try: @@ -60,14 +48,12 @@ def get_crop(image, crop_name, size=None, attribution=None, exact_size=False): else: return data - data["url"] = thumb.url - data["width"] = thumb.width - data["height"] = thumb.height - - if attribution and image.related_object: - data.update({ - "attribution": image.related_object.attribution, - "attribution_link": image.related_object.attribution_link, - "caption": image.related_object.caption, - }) + data.update({ + "url": thumb.url, + "width": thumb.width, + "height": thumb.height, + "attribution": image.related_object.attribution, + "attribution_link": image.related_object.attribution_link, + "caption": image.related_object.caption, + }) return data From 36715eccf0d24792a2f39f6ddc0348661e9f5e0b Mon Sep 17 00:00:00 2001 From: Chris Barna Date: Mon, 23 Mar 2015 13:48:08 -0400 Subject: [PATCH 3/5] get_crop: Return None if something goes wrong. --- cropduster/templatetags/cropduster_tags.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cropduster/templatetags/cropduster_tags.py b/cropduster/templatetags/cropduster_tags.py index cadc2153..6da8c219 100644 --- a/cropduster/templatetags/cropduster_tags.py +++ b/cropduster/templatetags/cropduster_tags.py @@ -34,7 +34,7 @@ def get_crop(image, crop_name, **kwargs): """ if not image or not image.related_object: - return + return None if len(kwargs) > 0: warnings.warn("All get_crop kwargs have been deprecated", DeprecationWarning) @@ -46,7 +46,7 @@ def get_crop(image, crop_name, **kwargs): if crop_name == "original": thumb = image.related_object else: - return data + return None data.update({ "url": thumb.url, From d73e7252e9418e7cef5a3edb374890c790008965 Mon Sep 17 00:00:00 2001 From: Chris Barna Date: Wed, 1 Apr 2015 21:00:02 -0400 Subject: [PATCH 4/5] Have get_crop play nicer with prefetch_related. --- cropduster/templatetags/cropduster_tags.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/cropduster/templatetags/cropduster_tags.py b/cropduster/templatetags/cropduster_tags.py index 6da8c219..bb33186e 100644 --- a/cropduster/templatetags/cropduster_tags.py +++ b/cropduster/templatetags/cropduster_tags.py @@ -3,7 +3,7 @@ import six from django import template -from cropduster.models import Image, Thumb +from cropduster.models import Image from cropduster.resizing import Size @@ -40,16 +40,17 @@ def get_crop(image, crop_name, **kwargs): warnings.warn("All get_crop kwargs have been deprecated", DeprecationWarning) data = {} + thumbs = {thumb.name: thumb for thumb in image.related_object.thumbs.all()} try: - thumb = image.related_object.thumbs.get(name=crop_name) - except Thumb.DoesNotExist: + thumb = thumbs[crop_name] + except KeyError: if crop_name == "original": thumb = image.related_object else: return None data.update({ - "url": thumb.url, + "url": Image.get_file_for_size(image=image, size_name=crop_name), "width": thumb.width, "height": thumb.height, "attribution": image.related_object.attribution, From a3f54cead5c90bc8b3fbc17f6d06681c7b57f291 Mon Sep 17 00:00:00 2001 From: Chris Barna Date: Wed, 1 Apr 2015 21:32:31 -0400 Subject: [PATCH 5/5] Actually return url with get_crop. --- cropduster/templatetags/cropduster_tags.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cropduster/templatetags/cropduster_tags.py b/cropduster/templatetags/cropduster_tags.py index bb33186e..d2a36c81 100644 --- a/cropduster/templatetags/cropduster_tags.py +++ b/cropduster/templatetags/cropduster_tags.py @@ -50,7 +50,7 @@ def get_crop(image, crop_name, **kwargs): return None data.update({ - "url": Image.get_file_for_size(image=image, size_name=crop_name), + "url": Image.get_file_for_size(image=image, size_name=crop_name).url, "width": thumb.width, "height": thumb.height, "attribution": image.related_object.attribution,