Opened 10 years ago

Closed 9 years ago

Last modified 9 years ago

#674 closed defect (fixed)

ImageField display use undocumented thumbnails for change_list display

Reported by: nesh <nesh [at] studioquattro [dot] co [dot] yu> Owned by: adrian
Component: contrib.admin Version:
Severity: normal Keywords:
Cc: nesh@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: UI/UX:

Description

I'm hunting this almost a hour :)

When displayed in admin interface ImageField use some sort of thumbnail (like xxx_t120.jpg). AFAIK this is not documented anywhere.

Also because thumbnail is not auto-generated I'm only got broken links in admin.

First solution, probably, will be some sort of auto-generated thumbnails or something like this (no thumbs, and I'm using i18n branch):

Index: /store/django/django/contrib/admin/views/main.py
===================================================================
--- /store/django/django/contrib/admin/views/main.py    (revision 987)
+++ /store/django/django/contrib/admin/views/main.py    (working copy)
@@ -414,8 +414,7 @@
                         result_repr = '<img src="%simg/admin/icon-%s.gif" alt="%s" />' % (ADMIN_MEDIA_PREFIX, BOOLEAN_MAPPING[field_val], field_val)
                     # ImageFields are special: Use a thumbnail.
                     elif isinstance(f, meta.ImageField):
-                        from django.parts.media.photos import get_thumbnail_url
-                        result_repr = '<img src="%s" alt="%s" title="%s" />' % (get_thumbnail_url(getattr(result, 'get_%s_url' % f.name)(), '120'), field_val, field_val)
+                        result_repr = '<img src="%s" alt="%s" title="%s" width="120" />' % (getattr(result, 'get_%s_url' % f.name)(), field_val, field_val)
                     # FloatFields are special: Zero-pad the decimals.
                     elif isinstance(f, meta.FloatField):
                         if field_val is not None:

Attachments (1)

img.diff (1.5 KB) - added by Nebojša Đorđević - nesh <nesh@…> 10 years ago.
patch

Download all attachments as: .zip

Change History (6)

comment:1 Changed 10 years ago by Esaj

Yes, auto-generated thumbnails would be really useful. Support for several different-sized thumbnails being created at once would be great, too.

comment:2 Changed 10 years ago by hugo

Feel free to use the code from my images module in the stuff project. The code gives your scaleImage and cropImage, two different ways to produce thumbnails from images, using just the python imaging library (that is already needed for some stuff in the ImageField handling, so it's not a new dependency).

comment:3 Changed 10 years ago by Nebojša Đorđević - nesh <nesh@…>

Update for new admin merge:

  • added width="120px" for img tag
  • changed get_thumbnail_url to return original path if no thumbnail is found
    Index: django/contrib/admin/templatetags/admin_list.py
    ===================================================================
    --- django/contrib/admin/templatetags/admin_list.py	(revision 1440)
    +++ django/contrib/admin/templatetags/admin_list.py	(working copy)
    @@ -149,7 +149,7 @@
                 # ImageFields are special: Use a thumbnail.
                 elif isinstance(f, meta.ImageField):
                     from django.parts.media.photos import get_thumbnail_url
    -                result_repr = '<img src="%s" alt="%s" title="%s" />' % (get_thumbnail_url(getattr(result, 'get_%s_url' % f.name)(), '120'), field_val, field_val)
    +                result_repr = '<img src="%s" alt="%s" title="%s" width="120px" />' % (get_thumbnail_url(getattr(result, 'get_%s_url' % f.name)(), '120'), field_val, field_val)
                 # FloatFields are special: Zero-pad the decimals.
                 elif isinstance(f, meta.FloatField):
                     if field_val is not None:
    Index: django/parts/media/photos.py
    ===================================================================
    --- django/parts/media/photos.py	(revision 1440)
    +++ django/parts/media/photos.py	(working copy)
    @@ -1,6 +1,10 @@
    -import re
    -
    +import re, os
    +from django.conf.settings import MEDIA_URL
     def get_thumbnail_url(photo_url, width):
         bits = photo_url.split('/')
         bits[-1] = re.sub(r'(?i)\.(gif|jpg)$', '_t%s.\\1' % width, bits[-1])
    -    return '/'.join(bits)
    +    # check if thumbnail exists
    +    if os.path.isfile(os.path.join(MEDIA_URL, *bits)):
    +        return '/'.join(bits)
    +    else:
    +        return photo_url
    

Changed 10 years ago by Nebojša Đorđević - nesh <nesh@…>

patch

comment:4 Changed 10 years ago by Nebojša Đorđević - nesh <nesh@…>

  • Cc nesh@… added

comment:5 Changed 9 years ago by adrian

  • Resolution set to fixed
  • Status changed from new to closed

This was fixed a few weeks ago.

Note: See TracTickets for help on using tickets.
Back to Top