Commits

Simon Meers committed 1117c00

stuffing around with configurable thumbnailing; c.f. feincms.content.image

Comments (0)

Files changed (5)

 from django.contrib import admin
 from .models import Gallery, Photo
 
-ADMIN_THUMBNAIL_SIZE = (100, 100)
+ADMIN_THUMBNAIL_SIZE = '100x100'
 class PhotoAdmin(admin.ModelAdmin):
     list_display = ('admin_thumbnail', 'caption', 'gallery')
     list_filter = ('gallery',)

gallery/models.py

 from django.template.loader import render_to_string
 from django.utils.translation import ugettext_lazy as _
 
-from easy_thumbnails.files import get_thumbnailer
+from feincms import settings
+from feincms.templatetags import feincms_thumbnail
 
 
 class Gallery(models.Model):
     created = models.DateTimeField(auto_now_add=True)
     gallery = models.ForeignKey(Gallery, related_name='photos')
     photo = models.ImageField(
-        max_length=255, upload_to='uploads/gallery_photos')
+        max_length=255, upload_to=os.path.join(
+            settings.FEINCMS_UPLOAD_PREFIX, 'gallery_photos'))
     caption = models.CharField(max_length=255, blank=True)
 
     class Meta:
     def __unicode__(self):
         return self.caption or u'Image without caption'
 
-    def get_thumbnail(self, **kwargs):
-        options = dict(size=(100, 100), crop=True)
-        options.update(kwargs)
-        return get_thumbnailer(self.photo).get_thumbnail(options)
+    def get_thumbnail(self, type='cropscale', size=None):
+        if not size:
+            return self.photo.url
+
+        thumbnailer = {
+            'cropscale': feincms_thumbnail.CropscaleThumbnailer,
+            }.get(type, feincms_thumbnail.Thumbnailer)
+        return thumbnailer(self.photo, size)
 
 
 class GalleryContent(models.Model):
     def render(self, **kwargs):
         return render_to_string(
             'gallery/render.html', {
+                'gallery_content': self,
                 'gallery': self.gallery,
                 'show_title': self.show_title,
                 'show_captions': self.show_captions,})
+
+    @classmethod
+    def initialize_type(cls, LARGE_FORMATS=None, THUMBNAIL_FORMATS=None):
+        if LARGE_FORMATS:
+            models.CharField(
+                _('large format'),
+                max_length=64,
+                choices=LARGE_FORMATS,
+                default=LARGE_FORMATS[0][0]
+                ).contribute_to_class(cls, 'large_format')
+        if THUMBNAIL_FORMATS:
+            models.CharField(
+                _('thumbnail format'),
+                max_length=64,
+                choices=THUMBNAIL_FORMATS,
+                default=THUMBNAIL_FORMATS[0][0]
+                ).contribute_to_class(cls, 'thumbnail_format')
+
+    def get_small_version(self, photo):
+        type, separator, size = getattr(
+            self, 'thumbnail_format', '').partition(':')
+        return photo.get_thumbnail(type, size)
+
+    def get_large_version(self, photo):
+        type, separator, size = getattr(
+            self, 'large_format', '').partition(':')
+        return photo.get_thumbnail(type, size)

gallery/templates/gallery/render.html

-{% load thumbnail %}
-<div class="gallery">
+{% load gallery_tags %}
+<div class="gallery{% if gallery_content.thumbnail_format %} gallery-format-{{ gallery_content.thumbnail_format|slugify }}{% endif %}">
   {% if show_title and gallery.title %}
     <h3>{{ gallery.title }}</h3>
   {% endif %}
   {% for photo in gallery.photos.all %}
     <div class="gallery_photo">
-      <a href="{% thumbnail photo.photo 800x800 %}">
-        <img src="{% thumbnail photo.photo 200x200 upscale crop %}" title="{{ photo.caption }}" />
+      <a href="{{ gallery_content|get_large_version:photo }}">
+        <img src="{{ gallery_content|get_small_version:photo }}" title="{{ photo.caption }}" />
         {% if show_captions and photo.caption %}
           <span class="caption">{{ photo.caption }}</span>
         {% endif %}

gallery/templatetags/__init__.py

+

gallery/templatetags/gallery_tags.py

+from django import template
+register = template.Library()
+
+@register.filter
+def get_small_version(content, photo):
+    return content.get_small_version(photo)
+
+@register.filter
+def get_large_version(content, photo):
+    return content.get_large_version(photo)