Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 22 additions & 37 deletions cropduster/templatetags/cropduster_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand All @@ -31,45 +31,30 @@ def get_crop(image, crop_name, size=None, attribution=None, exact_size=False):
For use in an image tag or style block like:

<img src="{{ img.url }}">

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:
return
if not image or not image.related_object:
return None

if size:
warnings.warn("The size kwarg is deprecated.", DeprecationWarning)
if len(kwargs) > 0:
warnings.warn("All get_crop kwargs have been 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
thumbs = {thumb.name: thumb for thumb in image.related_object.thumbs.all()}
try:
thumb = thumbs[crop_name]
except KeyError:
if crop_name == "original":
thumb = image.related_object
else:
if size.width:
data['width'] = size.width

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)

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,
})
return None

data.update({
"url": Image.get_file_for_size(image=image, size_name=crop_name).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