Skip to content

Commit

Permalink
Have get_crop play nicer with prefetch_related.
Browse files Browse the repository at this point in the history
  • Loading branch information
ctbarna committed Apr 2, 2015
1 parent 6707f54 commit 72e263e
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions cropduster/templatetags/cropduster_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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


Expand Down Expand Up @@ -33,23 +33,24 @@ def get_crop(image, crop_name, **kwargs):
<img src="{{ img.url }}">
"""

if not image or not image.related_object:
if not image:
return None

if len(kwargs) > 0:
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,
Expand Down

0 comments on commit 72e263e

Please sign in to comment.