Commits

Viliam Segeda committed 855e20d

thumbnailing in background

  • Participants
  • Parent commits 830ce22

Comments (0)

Files changed (9)

File reports/management/__init__.py

Empty file added.

File reports/management/commands/__init__.py

Empty file added.

File reports/management/commands/check_thumbs.py

+from easy_thumbnails.files import get_thumbnailer
+from easy_thumbnails.alias import aliases
+import django_rq
+from django.core.management.base import BaseCommand
+from reports.models import ReportImage
+from reports.tasks import generate_missing_thumbnail
+
+
+class Command(BaseCommand):
+    help = "Check if all report image thumbnails exist or recreate them"
+
+    def handle(self, *args, **options):
+        for ri in ReportImage.objects.all():
+            if ri.image:
+                path = ri.image.path                
+                all_options = aliases.all(path, include_global=True)
+                thumbnailer = get_thumbnailer(path)
+                if thumbnailer.source_storage.exists(path) and all_options:
+                    for options in all_options.values():
+                        if not thumbnailer.get_existing_thumbnail(options):
+                            print 'missing: ', path, options                        
+                            django_rq.enqueue(generate_missing_thumbnail, path, options, high_resolution=False)
+                        
+                    

File reports/models.py

 from collections import Counter
 from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
 from django.db import models
+from django.db.models.signals import post_save
+from django.dispatch import receiver
 from django.utils.dateformat import format
 from django.utils.translation import ugettext_lazy as _
+import django_rq
+from easy_thumbnails.signals import thumbnail_missed
 from mezzanine.core.fields import FileField
 from mezzanine.utils.models import upload_to
 from mezzanine.utils.urls import admin_url
 from conf import DISTANCE_UNIT_CHOICES, TEAM_CHOICES, NOTEAM
 from utils import make_title, make_stamp
 from reports.utils import make_location_uuid
+from tasks import generate_report_thumbnails, generate_missing_thumbnail
 
 
 LOCAL_NEWS_COUNT = 5
         if self.position == 0:
             self.position = ReportImage.objects.next_position(self.report, self.team)
 
-        super(ReportImage, self).save(*args, **kwargs)
+        super(ReportImage, self).save(*args, **kwargs)
+
+
+@receiver(post_save, sender=ReportImage)
+def async_thumbnails(sender, instance, **kwargs):
+    django_rq.enqueue(generate_report_thumbnails, image=instance.image)
+    
+@receiver(thumbnail_missed)
+def missing_thumbnails(sender, options, high_resolution, **kwargs):    
+    django_rq.enqueue(generate_missing_thumbnail, sender.name, options, high_resolution)
+    
+
+    

File reports/tasks.py

+from django_rq import job
+from easy_thumbnails.files import get_thumbnailer, generate_all_aliases
+
+
+@job
+def generate_report_thumbnails(image):
+    """ generate all thumbnails specified in global settings for given image """
+    generate_all_aliases(image.path, include_global=True)
+    
+@job
+def generate_missing_thumbnail(path, options, high_resolution):
+    """ generate thumbnail if source path exists """
+    thumbnailer = get_thumbnailer(path)
+    if thumbnailer.source_storage.exists(path):
+        if high_resolution:
+            options['HIGH_RESOLUTION'] = high_resolution
+        thumbnailer.get_thumbnail(options, generate=True)
+    else:
+        #TODO: announce missing file
+        pass
+

File reports/views.py

 import datetime
-from django.core.exceptions import ObjectDoesNotExist
-from django.http import Http404, HttpResponseForbidden
+from django.conf import settings
+from django.http import HttpResponseForbidden
 from django.views.generic import ListView, DetailView, RedirectView
 from models import Report, ReportImage
 from forms import ReportForm
         ctx = super(ReportGallery, self).get_context_data(**kwargs)
         report = ctx.get('report', None)
         ctx['report_items'] = ReportImage.objects.reported(report=report)
-
-        sourceSizes = [320, 640, 1000, 1600]
-        ctx['source_sizes'] = sourceSizes
+        
+        ctx['source_sizes'] = settings.GALLERY_SCREEN_SIZES
 
         ctx['url_detail'] = report.get_absolute_url()
         
 # are displayed for error pages. Should always be set to ``False`` in
 # production. Best set to ``True`` in local_settings.py
 DEBUG = False
+TEMPLATE_DEBUG = DEBUG
 
 # Whether a user's session cookie expires when the Web browser is closed.
 SESSION_EXPIRE_AT_BROWSER_CLOSE = True
     'post_office',
     'geoposition',
     'dbbackup',
-    "django_rq",
+    'django_rq',
+    'easy_thumbnails',
     #'taggit'
 )
 
 )
 
 ###################
+# THUMBNAILS      #
+###################
+
+SOUTH_MIGRATION_MODULES = {
+    'easy_thumbnails': 'easy_thumbnails.south_migrations',
+}
+
+THUMBNAIL_ALIASES = {
+    '': {
+        'report_main' : {'size': (450, 0),  'crop': False },
+        'gallery_thumb':{'size': (70, 70),  'crop': True },
+        'report_admin_thumb': {'size': (100, 100),  'crop': True },
+        'screen_cga' : {'size': (320, 320),  'crop': False },
+        'screen_vga' : {'size': (640, 640),  'crop': False },
+        'screen_xga' : {'size': (1024, 1024),  'crop': False },
+        'screen_uxga': {'size': (1600, 1600),  'crop': False },
+    },    
+}
+THUMBNAIL_BASEDIR = 'thumbs'
+GALLERY_SCREEN_SIZES = [THUMBNAIL_ALIASES[''][k]['size'][0] for k in THUMBNAIL_ALIASES[''] if k.startswith('screen')]
+GALLERY_SCREEN_SIZES.sort()
+#THUMBNAIL_SUBDIR = '.thumbnails'
+    
+###################
 # EMAIL SETTINGS  #
 ###################
 
         'HOST': 'localhost',
         'PORT': 6379,
         'DB': 0,
-        #'PASSWORD': '',
+        'ASYNC' : True
     },
 }
+
+RQ_SHOW_ADMIN_LINK = True
+
+        
 ###################
 # DEPLOY SETTINGS #
 ###################

File templates/reports/report_detail.html

 {% extends "base.html" %}
-{% load i18n mezzanine_tags markup report_tags %}
+{% load i18n thumbnail markup report_tags %}
 
 {% block extra_css %}
 <link rel="stylesheet" href="//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css">
     {% for ri in report_items %}
       {% with desc=ri.description|default:""|markdown:"safe" local_desc=ri.description_local|default:""|markdown:"safe" %}
         <div class="report-item">
-          {% if ri.image %}
+          {% if ri.image %}{% with thumbnailer=ri.image.path|thumbnailer_passive %}
           <div class="image">
             <a href="{{ report.gallery_url }}#{{ forloop.counter }}">
-              <img src="{{ MEDIA_URL }}{% thumbnail ri.image 450 0 %}" />
+              <img src="{{ thumbnailer.report_main.url }}" alt="" />
             </a>
           </div>
-          {% endif %}
+          {% endwith %}{% endif %}
           {% if ri.description %}
           <div class="description">{{ desc }}</div>
           {% endif %}

File templates/reports/report_gallery.html

 <!DOCTYPE html>
-{% load mezzanine_tags markup block_tags i18n future %}
+{% load i18n mezzanine_tags markup block_tags thumbnail future %}
 <html>
 <head>
 
       {% with desc=ri.description|default:""|markdown:"safe"|escapejs local_desc=ri.description_local|default:""|markdown:"safe"|escapejs %}
       {
         {% if ri.image %}isImage: true,{% else %}isImage: false,{% endif %}
-        images: [
-          {% for si in source_sizes %}
-          "{% if ri.image %}{{ MEDIA_URL }}{% thumbnail ri.image si 0 %}{% endif %}",
-          {% endfor %}
-        ],
+        images: [ {% if ri.image %}{% with thumbnailer=ri.image.path|thumbnailer_passive %}
+          "{{ thumbnailer.screen_cga.url }}",
+          "{{ thumbnailer.screen_vga.url }}",
+          "{{ thumbnailer.screen_xga.url }}",
+          "{{ thumbnailer.screen_uxga.url }}",
+        {% endwith %}{% endif %}],
         imageOriginal: "{% if ri.image %}{{ MEDIA_URL }}{{ ri.image }}{% endif %}",
         desc: "{{ desc }}",
         localDesc: "{{ local_desc }}"
     <div id="gallery-thumbs-content">
       <div id="gallery-thumbs">
       {% for ri in report_items %}
-        <img class="gallery-thumb gallery-clicklink" id="gallery-thumb{{ forloop.counter0 }}" onclick="selectImage({{ forloop.counter0 }});" src="{% if ri.image %}{{ MEDIA_URL }}{% thumbnail ri.image 70 70 %}{% else %}{{ STATIC_URL }}images/gallery-text-thumb.png{% endif %}">
+        <img class="gallery-thumb gallery-clicklink" id="gallery-thumb{{ forloop.counter0 }}" onclick="selectImage({{ forloop.counter0 }});" alt="" 
+              src="{% if ri.image %}{% with thumbnailer=ri.image.path|thumbnailer_passive %}{{ thumbnailer.gallery_thumb.url }}{% endwith %}{% else %}{{ STATIC_URL }}images/gallery-text-thumb.png{% endif %}">
       {% endfor %}
       </div>
     </div>