django easy_thumbnails AdminImageWidget

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import os
from PIL import Image

from django.db import models
from django.conf import settings
from django.contrib.admin.widgets import AdminFileWidget
from django.utils.translation import ugettext as _
from django.utils.safestring import mark_safe


__all__ = ['AdminImageWidget', 'AdminImageMixin']


get_thumbnail = None

try:
    from easy_thumbnails.files import get_thumbnailer
except ImportError:
    def thumbnail1(image_path):
        absolute_url = os.path.join(settings.MEDIA_URL, image_path)
        return u'<img src="%s" alt="" width="160" />' % absolute_url
    get_thumbnail = thumbnail1
else:
    def thumbnail2(image_path):
        thumbnailer = get_thumbnailer(image_path)
        thumbnail_options = {
            'size': (160, 120), 'autocrop': True}
        t = thumbnailer.get_thumbnail(thumbnail_options)
        media_url = settings.MEDIA_URL
        return u'<img src="%s%s" alt=""/>' % (
            media_url, t)
    get_thumbnail = thumbnail2


class AdminImageWidget(AdminFileWidget):
    """
    A FileField Widget that displays an image instead of a file path
    if the current file is an image.
    """
    def render(self, name, value, attrs=None):
        output = []
        file_name = str(value)
        if file_name:
            file_path = ''.join((settings.MEDIA_URL, file_name))
            try:
                Image.open(os.path.join(settings.MEDIA_ROOT, file_name))
            except IOError:
                # Not an image
                output.append('%s <a target="_blank" href="%s">%s</a> '
                              '<br />%s ' % (_('Currently:'), file_path,
                                             file_name, _('Change:')))
            else:
                output.append('<a target="_blank" href="%s">%s</a>' % (
                              file_path, get_thumbnail(file_name)))
        output.append(super(AdminFileWidget, self).render(name, value, attrs))
        return mark_safe(u''.join(output))


class AdminImageMixin(object):
    """
    This is a mix-in for ModelAdmin subclasses to make ``ImageField``
    show nicer form widget
    """
    def formfield_for_dbfield(self, db_field, **kwargs):
        if isinstance(db_field, models.ImageField):
            return db_field.formfield(widget=AdminImageWidget)
        sup = super(AdminImageMixin, self)
        return sup.formfield_for_dbfield(db_field, **kwargs)

More like this

  1. merge action in django admin by yeago 3 years, 6 months ago
  2. Searching and Deleting Cache Entries in Admin tool by shootclub 2 years, 9 months ago
  3. Add delete button in admin cp by cschand 6 years ago
  4. Render TextField in Admin by xavierlesa 3 years, 7 months ago
  5. A allow_tags link generator in the Django Admin by ikeikeikeike 1 year, 9 months ago

Comments

partizan (on December 5, 2012):

Thanks, good job :) Тема ;)

#

(Forgotten your password?)