Login

django easy_thumbnails AdminImageWidget

Author:
livskiy
Posted:
September 29, 2012
Language:
Python
Version:
1.4
Score:
0 (after 0 ratings)

AdminImageWidget and AdminImageMixin to use easy_thumbnails in admin site forms for models with ImageField field.

 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. Template tag - list punctuation for a list of items by shapiromatron 2 months, 2 weeks ago
  2. JSONRequestMiddleware adds a .json() method to your HttpRequests by cdcarter 2 months, 3 weeks ago
  3. Serializer factory with Django Rest Framework by julio 9 months, 2 weeks ago
  4. Image compression before saving the new model / work with JPG, PNG by Schleidens 10 months, 1 week ago
  5. Help text hyperlinks by sa2812 11 months ago

Comments

partizan (on December 5, 2012):

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

#

Please login first before commenting.