Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add get_thumbs templatetag. #37

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
16 changes: 16 additions & 0 deletions cropduster/templatetags/cropduster_tags.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,3 +73,19 @@ def get_crop(image, crop_name, size=None, attribution=None, exact_size=False):
"caption": image.related_object.caption,
})
return data


@register.assignment_tag
def get_thumbs(image):
def _serialize_thumb(thumb):
return {
"url": Image.get_file_for_size(image, thumb.name).url,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be related_object.get_image_url(thumb.name),?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's the source for get_image_url:

    def get_image_url(self, size_name='original', tmp=False):
        converted = Image.get_file_for_size(self.image, size_name, tmp=tmp)
        return getattr(converted, 'url', None) or u''

It's doing the same thing as you are, but with a getattr that falls back to empty string. (I don't know if that would ever make a real world difference, but it's one less thing to blow up.) ISTM it's cleaner to use the method that gives you the url you want instead of doing it yourself and maybe having differences in how your code is implemented.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Serious bikeshedding for working code that uses a public API.

"width": thumb.width,
"height": thumb.height,
"attribution": image.related_object.attribution,
"attribution_link": image.related_object.attribution_link,
"caption": image.related_object.caption,
}

thumbs = image.related_object.thumbs.all()
return {thumb.name: _serialize_thumb(thumb) for thumb in thumbs}