switch thumbnail system from homegrown to sorl-thumbnail
authorFrédéric Péters <fpeters@0d.be>
Mon, 25 May 2015 08:45:39 +0000 (10:45 +0200)
committerFrédéric Péters <fpeters@0d.be>
Mon, 25 May 2015 08:45:39 +0000 (10:45 +0200)
22 files changed:
panikweb/paniktags/templatetags/thumbnails.py [deleted file]
panikweb/settings.py
panikweb_templates/templates/emissions/emission_detail.html
panikweb_templates/templates/emissions/episode_detail.html
panikweb_templates/templates/emissions/newsitem_detail.html
panikweb_templates/templates/emissions/resume.html
panikweb_templates/templates/episodes/detail.html
panikweb_templates/templates/episodes/inline.html
panikweb_templates/templates/episodes/resume.html
panikweb_templates/templates/feed/newsitem.html
panikweb_templates/templates/feed/soundfile.html
panikweb_templates/templates/home.html
panikweb_templates/templates/listen.html
panikweb_templates/templates/news/archives.html
panikweb_templates/templates/news/inline.html
panikweb_templates/templates/news/roll.html
panikweb_templates/templates/panikombo/audio.html
panikweb_templates/templates/panikombo/episode.html
panikweb_templates/templates/party.html
panikweb_templates/templates/soundfiles/resume.html
requirements.txt
setup.py

diff --git a/panikweb/paniktags/templatetags/thumbnails.py b/panikweb/paniktags/templatetags/thumbnails.py
deleted file mode 100644 (file)
index 75ec6ff..0000000
+++ /dev/null
@@ -1,79 +0,0 @@
-# initiated from http://djangosnippets.org/snippets/2145/
-
-import os
-import re
-from PIL import Image
-
-from django.conf import settings
-
-from django.db.models.signals import post_save, pre_delete
-from django.template import Library
-
-register = Library()
-
-def thumbnail(image, size='100x100'):
-    # defining the size
-    x, y = [int(x) for x in size.split('x')]
-    # defining the filename and the miniature filename
-    try:
-        filehead, filetail = os.path.split(image.path)
-    except (AttributeError, ValueError):
-        # return transparent pixel if the image doesn't actually exist
-        return 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABAQMAAAAl21bKAAAAA1BMVEUAAACnej3aAAAAAXRSTlMAQObYZgAAAApJREFUCNdjYAAAAAIAAeIhvDMAAAAASUVORK5CYII='
-    basename, format = os.path.splitext(filetail)
-    if format.lower() not in ('.jpg', '.jpeg'):
-        format = '.png'
-    miniature = basename + '__' + size + format
-    filename = image.path
-    miniature_filename = os.path.join(filehead, miniature)
-    filehead, filetail = os.path.split(image.url)
-    miniature_url = filehead + '/' + miniature
-    if os.path.exists(miniature_filename) and \
-            os.path.getmtime(filename) > os.path.getmtime(miniature_filename):
-        os.unlink(miniature_filename)
-    # if the image wasn't already resized, resize it
-    if not os.path.exists(miniature_filename) and os.path.exists(filename):
-        image = Image.open(filename)
-        if abs( (1.0*x/y) - (1.0*image.size[0]/image.size[1]) ) > 0.1:
-            # aspect ratio change, crop the image first
-            box = [0, 0, image.size[0], int(image.size[0] * (1.0*y/x))]
-
-            if box[2] > image.size[0]:
-                box = [int(t*(1.0*image.size[0]/box[2])) for t in box]
-            if box[3] > image.size[1]:
-                box = [int(t*(1.0*image.size[1]/box[3])) for t in box]
-
-            if image.size[0] > image.size[1]: # landscape
-                box[0] = (image.size[0] - box[2]) / 2 # keep the middle
-                box[2] += box[0]
-            else:
-                box[1] = (image.size[1] - box[3]) / 4 # keep mostly the top
-                box[3] += box[1]
-
-            image = image.crop(box)
-        image = image.resize([x, y], Image.ANTIALIAS)
-        try:
-            image.save(miniature_filename, image.format, quality=90, optimize=1)
-        except:
-            image.save(miniature_filename, image.format, quality=90)
-
-    return miniature_url
-
-
-register.filter(thumbnail)
-
-
-def clean_thumb(sender, instance, **kwargs):
-    if not hasattr(instance, 'image'):
-        return
-    if not instance.image:
-        return
-    name, ext = os.path.splitext(os.path.basename(instance.image.name))
-    exp = '^%s__\d+x\d+x[0-1]{1}\%s' % (name, ext)
-    for file_path in os.listdir(settings.MEDIA_ROOT):
-        if re.search(exp, file_path):
-            os.unlink(settings.MEDIA_ROOT + file_path)
-
-
-post_save.connect(clean_thumb)
-pre_delete.connect(clean_thumb)
index b7ac0ca..2e72faa 100644 (file)
@@ -145,6 +145,7 @@ INSTALLED_APPS = (
     'panikweb.paniktags',
     'mptt',
     'compressor',
+    'sorl.thumbnail',
     'jquery',
     'ckeditor',
     'emissions',
index 0d8a0cb..386068c 100644 (file)
@@ -1,5 +1,5 @@
 {% extends "emissions.html" %}
-{% load paniktags thumbnails staticfiles i18n %}
+{% load paniktags staticfiles i18n %}
 {% block bodyID %}Emissions{% endblock %}
 {% block title %}{{ emission.title }}{% endblock %}
 
index ac2f4f7..bb047d8 100644 (file)
@@ -1,5 +1,5 @@
 {% extends "emissions/emission_detail.html" %}
-{% load paniktags thumbnails staticfiles soundfiles %}
+{% load paniktags staticfiles soundfiles %}
 {% block title %}{{ episode.title }} - {{ episode.emission.title }} {% endblock %}
 
 {% block head %}
index d1f2896..e59d039 100644 (file)
@@ -1,5 +1,5 @@
 {% extends "news.html" %}
-{% load i18n thumbnails paniktags %}
+{% load i18n paniktags %}
 {% block title %}{{ newsitem.title }}{% endblock %}
 
 {% block toptitle %}
index 2cfa9f4..0a18678 100644 (file)
@@ -1,10 +1,12 @@
-{% load thumbnails staticfiles %}
+{% load thumbnail staticfiles %}
 <div class="emission emission-resume resume cf">
        <div class="{% if emission.archived %}archived{% endif %}">
                <a class="block" href="{% url 'emission-view' slug=emission.slug %}">
                        <div class="logo left">
                                {% if emission.image %}
-                                       <img width="60" height="60" src="{{ emission.image|thumbnail:'60x60' }}"/>
+                                       {% thumbnail emission.image "60x60" crop="50% 25%" as im %}
+                                       <img width="60" height="60" src="{{im.url}}"/>
+                                       {% endthumbnail %}
                                {% else %}
                                        <img class="smooth"  style="width:60px;" src="{% static "img/emission.png" %}"/>
                                {% endif %}
index d6730a4..99043eb 100644 (file)
@@ -1,4 +1,4 @@
-{% load thumbnails paniktags %}
+{% load thumbnail paniktags %}
 <div class="episode detail episode-detail cf">
        {% if episode.first_diffusion %}
        <div class="dateBloc">
@@ -45,7 +45,9 @@
        {% endif %}
        <div class="content userContent marged">
                {% if episode.image %}
-               <img class="logo right button" data-toggle-img="/media/{{episode.image}}" src="{{ episode.image|thumbnail:'640x480' }}"/>
+               {% thumbnail emission.image "640x480" crop="50% 25%" as im %}
+               <img class="logo right button" data-toggle-img="/media/{{episode.image}}" src="{{im.url}}"/>
+               {% endthumbnail %}
                {% endif %}
                {% if episode.text %}
                <article class="text">
index 5fb6548..0fa4534 100644 (file)
@@ -1,10 +1,14 @@
-{% load thumbnails paniktags staticfiles %}
+{% load thumbnail paniktags staticfiles %}
 <div class="episode inline episode-inline">
        <div class="logo">
                {% if episode.image %}
-                       <img src="{{ episode.image|thumbnail:'60x60' }}"/>
+                       {% thumbnail episode.image "60x60" crop="50% 25%" as im %}
+                       <img src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% elif episode.emission.image %}
-                       <img src="{{ episode.emission.image|thumbnail:'60x60' }}"/>
+                       {% thumbnail episode.emission.image "60x60" crop="50% 25%" as im %}
+                       <img src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% else %}
                        <img class="smooth"  style="width:60px;" src="{% static "img/emission.png" %}"/>
                {% endif %}
index 929e612..b004c46 100644 (file)
@@ -1,4 +1,4 @@
-{% load thumbnails paniktags staticfiles %}
+{% load thumbnail paniktags staticfiles %}
 <div class="episode {% if model %}{{ model }}{% else %}resume{% endif %} cf {{ class }}">
        {% if date != False %}
        <div class="dateBloc">
        <div class="logo">
                <a href="{% url 'episode-view' emission_slug=episode.emission.slug slug=episode.slug %}">
                {% if model = "inline" and episode.image %}
-                       <img width="60" height="60" src="{{ episode.image|thumbnail:'60x60' }}"/>
+                       {% thumbnail episode.image "60x60" crop="50% 25%" as im %}
+                       <img width="60" height="60" src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% elif model = "inline" and episode.emission.image %}
-                       <img width="60" height="60" src="{{ episode.emission.image|thumbnail:'60x60' }}"/>
+                       {% thumbnail emission.episode.image "60x60" crop="50% 25%" as im %}
+                       <img width="60" height="60" src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% elif model = "inline" %}
                        <img class="smooth"  style="width:60px;" src="{% static "img/emission.png" %}"/>
                {% elif episode.image %}
-                       <img src="{{ episode.image|thumbnail:'150x150' }}"/>
+                       {% thumbnail episode.image "150x150" crop="50% 25%" as im %}
+                       <img width="150" height="150" src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% elif episode.emission.image %}
-                       <img src="{{ episode.emission.image|thumbnail:'150x150' }}"/>
+                       {% thumbnail episode.emission.image "150x150" crop="50% 25%" as im %}
+                       <img width="150" height="150" src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% else %}
                        <img class="smooth" style="width:150px;" src="{% static "img/emission.png" %}"/>
                {% endif %}
index 497a0ed..b1c0612 100644 (file)
@@ -1,6 +1,8 @@
-{% load thumbnails %}
+{% load thumbnail %}
 {% if obj.image %}
-<img src="{{ obj.image|thumbnail:'320x240' }}"/>
+{% thumbnail obj.image "320x240" crop="50% 25%" as im %}
+<img src="{{im.url}}"/>
+{% endthumbnail %}
 {% endif %}
 
 {% autoescape off %}
index 116c3de..f14fdab 100644 (file)
@@ -1,6 +1,8 @@
-{% load thumbnails %}
+{% load thumbnail %}
 {% if obj.episode.image %}
-<img src="{{ obj.episode.image|thumbnail:'320x240' }}"/>
+{% thumbnail obj.episode.image "320x240" crop="50% 25%" as im %}
+<img src="{{im.url}}"/>
+{% endthumbnail %}
 {% endif %}
 
 {% autoescape off %}
index 68d7b1f..3c64356 100644 (file)
@@ -1,5 +1,5 @@
 {% extends "base.html" %}
-{% load thumbnails paniktags staticfiles i18n %}
+{% load paniktags staticfiles i18n %}
 {% block bodyID %}Home{% endblock %}
 {% block title %}{% trans 'Home' %}{% endblock %}
 
index 986cd0a..a368cbb 100644 (file)
@@ -1,5 +1,5 @@
 {% extends "base.html" %}
-{% load paniktags staticfiles thumbnails i18n %}
+{% load paniktags staticfiles thumbnail i18n %}
 {% block title %}{% trans 'Sounds' %}{% endblock %}
 {% block toptitle %}
 <h1 class="top"><a href="{% url 'listen' %}">{% trans 'Sounds' %}</a></h1>
                 </div>
                 <div class="logo">
                   {% if soundfile.episode.image %}
-                  <img class="normal" src="{{ soundfile.episode.image|thumbnail:'480x320' }}"/>
+                  {% thumbnail soundfile.episode.image "480x320" crop="50% 25%" as im %}
+                  <img class="normal" src="{{im.url}}"/>
+                  {% endthumbnail %}
                   {% elif soundfile.episode.emission.image %}
-                  <img class="normal" src="{{ soundfile.episode.emission.image|thumbnail:'480x320' }}"/>
+                  {% thumbnail soundfile.episode.emission.image "480x320" crop="50% 25%" as im %}
+                  <img class="normal" src="{{im.url}}"/>
+                  {% endthumbnail %}
                   {% else %}
                   <img class="normal" src="{% static "img/sound.png" %}"/>
                   {% endif %}
index c330e44..fc444fd 100644 (file)
@@ -1,5 +1,5 @@
 {% extends "news.html" %}
-{% load thumbnails paniktags i18n %}
+{% load paniktags i18n %}
 {% block title %}{% trans 'News' %} - Archives{% endblock %}
 {% block nav %}
        <div class="search-filters">
index b85bf52..677a43b 100644 (file)
@@ -1,4 +1,4 @@
-{% load thumbnails staticfiles %}
+{% load thumbnail staticfiles %}
 <div class="content content-inline {% if class != "special" %}inline{% endif %} cf {{ class }}">
        <a class="block cf" href="{% url 'newsitem-view' slug=content.slug %}">
                {% if class == "special" and content.category %}
@@ -9,11 +9,17 @@
 
                <div class="logo">
                {% if class == "special" and content.image %}
-                       <img class="normal" src="{{ content.image|thumbnail:'480x320' }}"/>
+                       {% thumbnail content.image "480x320" crop="50% 25%" as im %}
+                       <img class="normal" src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% elif content.image %}
-                       <img class="left" width="60" height="60" src="{{ content.image|thumbnail:'60x60' }}"/>
+                       {% thumbnail content.image "60x60" crop="50% 25%" as im %}
+                       <img class="left" width="60" height="60" src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% elif content.emission.image %}
-                       <img class="left" width="60" height="60" src="{{ content.emission.image|thumbnail:'60x60' }}"/>
+                       {% thumbnail content.emission.image "60x60" crop="50% 25%" as im %}
+                       <img class="left" width="60" height="60" src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% else %}
                        <img class="left" width="60" height="60" src="{% static "img/actu.png" %}"/>
                {% endif %}
index 3109996..dde2a92 100644 (file)
@@ -1,4 +1,4 @@
-{% load thumbnails i18n paniktags %}
+{% load thumbnail i18n paniktags %}
 <div id="newsRoll">
        <div class="newsRoll center cf">
                <ul id="ticker" class="custom bigNews marged" style="height:300px;overflow:hidden;">
@@ -7,7 +7,9 @@
                     id="newsRollId-{{ focus.id }}"
                     class="">
                                        <a 
-                        style="max-width:100%;height:300px;background: no-repeat 50% 50% url('{{ focus.content_image|thumbnail:'500x375' }}');"
+                   {% thumbnail focus.content_image "500x375" crop="50% 25%" as im %}
+                        style="max-width:100%;height:300px;background: no-repeat 50% 50% url('{{im.url}}');"
+                       {% endthumbnail %}
                         class="block news relative"
                        href="{{ focus|get_focus_url }}">
                                                {% if focus.content_category_title %}
@@ -26,7 +28,9 @@
                            {% for focus in news %}
                                    <li style="width:30%;" class="num-{{ forloop.counter }} padded">
                                            <button class="inBlock" data-about="#newsRollId-{{ focus.id }}">
-                                                   <img style="width:95%;" src="{{ focus.content_image|thumbnail:'160x120' }}" />
+                                                   {% thumbnail focus.content_image "160x120" crop="50% 25%" as im %}
+                                                   <img style="width:95%;" src="{{im.url}}" />
+                                                   {% endthumbnail %}
                                            </button>
                                    </li>
                            {% endfor %}
index ec68926..8af028e 100644 (file)
@@ -1,10 +1,14 @@
-{% load paniktags thumbnails staticfiles %}
+{% load paniktags thumbnail staticfiles %}
 <div class="wrapper extra-soundfiles">
        <div class="logo">
                {% if soundfile.episode.image %}
-                       <img src="{{ soundfile.episode.image|thumbnail:'60x60' }}"/>
+                       {% thumbnail soundfile.episode.image "60x60" crop="50% 25%" as im %}
+                       <img width="60" height="60" src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% elif soundfile.episode.emission.image %}
-                       <img src="{{ soundfile.episode.emission.image|thumbnail:'60x60' }}"/>
+                       {% thumbnail soundfile.episode.emission.image "60x60" crop="50% 25%" as im %}
+                       <img width="60" height="60" src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% else %}
                        <img class="smooth"  style="width:60px;" src="{% static "img/emission.png" %}"/>
                {% endif %}
index 04fd894..9701e39 100644 (file)
@@ -1,10 +1,14 @@
-{% load paniktags thumbnails staticfiles %}
+{% load paniktags thumbnail staticfiles %}
 <div class="wrapper extra-soundfiles">
        <div class="logo">
                {% if episode.image %}
-                       <img src="{{ episode.image|thumbnail:'60x60' }}"/>
+                       {% thumbnail episode.image "60x60" crop="50% 25%" as im %}
+                       <img width="60" height="60" src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% elif episode.emission.image %}
-                       <img src="{{ episode.emission.image|thumbnail:'60x60' }}"/>
+                       {% thumbnail episode.emission.image "60x60" crop="50% 25%" as im %}
+                       <img width="60" height="60" src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% else %}
                        <img class="smooth"  style="width:60px;" src="{% static "img/emission.png" %}"/>
                {% endif %}
index ccfb42d..1e6083b 100644 (file)
@@ -1,5 +1,5 @@
 {% extends "base.html" %}
-{% load thumbnails paniktags %}
+{% load thumbnail paniktags %}
 {% block bodyID %}Party{% endblock %}
 {% block title %}Party{% endblock %}
 
@@ -40,7 +40,9 @@ $(function() {
         </div>
       {% endif %}
       <div class="logo">
-        <img class="normal" src="{{ focus.content_image|thumbnail:'500x375' }}"/>
+        {% thumbnail focus.content_image "500x375" crop="50% 25%" as im %}
+        <img width="500" height="375" class="normal" src="{{im.url}}"/>
+        {% endthumbnail %}
       </div>
       <div class="title"><div>{{ focus.focus_title }}</div></div>
     </a>
index 1fb2e3f..d6cde2c 100644 (file)
@@ -1,10 +1,14 @@
-{% load thumbnails paniktags staticfiles %}
+{% load thumbnail paniktags staticfiles %}
 <div class="episode soundfile inline cf">
        <div class="logo">
                {% if soundfile.episode.image %}
-                       <img width="60" height="60" src="{{ soundfile.episode.image|thumbnail:'60x60' }}"/>
+                       {% thumbnail soundfile.episode.image "60x60" crop="50% 25%" as im %}
+                       <img width="60" height="60" src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% elif soundfile.episode.emission.image %}
-                       <img width="60" height="60" src="{{ soundfile.episode.emission.image|thumbnail:'60x60' }}"/>
+                       {% thumbnail soundfile.episode.emission.image "60x60" crop="50% 25%" as im %}
+                       <img width="60" height="60" src="{{im.url}}"/>
+                       {% endthumbnail %}
                {% else %}
                        <img width="60" height="60" src="{% static "img/sound.png" %}"/>
                {% endif %}
index 2def82d..51a67cb 100644 (file)
@@ -5,3 +5,4 @@ django-debug-toolbar<1.0.0
 django-jquery
 django-taggit
 http://pypi.python.org/packages/source/d/django-jsonresponse/django-jsonresponse-0.5.tar.gz
+sorl-thumbnail
index 10c777d..0db41f0 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -17,6 +17,7 @@ setup(name='panikweb',
           'django-jquery',
           'django-registration',
           'django-datetime-widget',
+          'sorl-thumbnail',
            ],
       license='GPL',
       classifiers=['Development Status :: 3 - Alpha',