summaryrefslogtreecommitdiff
path: root/app/blog/widgets.py
diff options
context:
space:
mode:
Diffstat (limited to 'app/blog/widgets.py')
-rw-r--r--app/blog/widgets.py32
1 files changed, 32 insertions, 0 deletions
diff --git a/app/blog/widgets.py b/app/blog/widgets.py
new file mode 100644
index 0000000..a9451e7
--- /dev/null
+++ b/app/blog/widgets.py
@@ -0,0 +1,32 @@
+from django.contrib.admin.widgets import AdminFileWidget
+from django.utils.translation import ugettext as _
+from django.utils.safestring import mark_safe
+from django.conf import settings
+from PIL import Image
+import os
+
+try:
+ from sorl.thumbnail.main import DjangoThumbnail
+ def thumbnail(image_path):
+ t = DjangoThumbnail(relative_source=image_path, requested_size=(200,200))
+ return u'<img src="%s" alt="%s" />' % (t.absolute_url, image_path)
+except ImportError:
+ def thumbnail(image_path):
+ absolute_url = os.path.join(settings.IMAGES_URL, image_path)
+ return u'<img src="%s" alt="%s" />' % (absolute_url, image_path)
+
+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 = '%s' % (file_name)
+ output.append('<a target="_blank" href="%s">%s</a><br />%s <a target="_blank" href="%s">%s</a><br />%s ' % \
+ (file_path, thumbnail(file_name), _('Currently:'), file_path, file_name, _('Change:')))
+
+ output.append(super(AdminFileWidget, self).render(name, value, attrs))
+ return mark_safe(u''.join(output))