Commits

b7w committed 6220524

Pre cache function

  • Participants
  • Parent commits e21b5c7

Comments (0)

Files changed (4)

bviewer/core/controllers.py

                 result.append(gallery)
         return result
 
+    def pre_cache(self):
+        ids = [self.get_object().id,]
+        ids.extend(i.id for i in self.get_all_sub_galleries())
+        images = Image.objects.filter(gallery__in=ids)
+        sizes = [i for i in settings.VIEWER_IMAGE_SIZE.keys() if i != 'full']
+        for size in sizes:
+            for image in images:
+                storage = ImageStorage(self.holder)
+                self._pre_cache_image(storage, image, size)
+
+    def _pre_cache_image(self, storage, image, size):
+        options = ImageOptions.from_settings(size)
+        image_path = storage.get_path(image.path, options)
+        if image_path.exists and not image_path.cache_exists:
+            image_async = CacheImage(image_path)
+            as_job(image_async.process, waite=False)
+
     def is_archiving_allowed(self):
         obj = self.get_object()
         return obj and obj.allow_archiving

bviewer/profile/admin.py

 
     search_fields = ('title', 'description',)
 
-    readonly_fields = ('images', 'thumbnails',)
+    readonly_fields = ('images', 'pre_cache', 'thumbnails',)
     fields = ('parent', 'title', 'visibility', 'gallery_sorting', 'allow_archiving',
-              'images', 'description', 'time', 'thumbnails', )
+              'images', 'pre_cache', 'description', 'time', 'thumbnails', )
 
     def images(self, obj):
         if Gallery.objects.safe_get(id=obj.id):
 
     images.allow_tags = True
 
+    def pre_cache(self, obj):
+        if Gallery.objects.safe_get(id=obj.id):
+            url = reverse('profile.gallery.pre-cache', kwargs=dict(uid=obj.id))
+            return smart_text('<b><a href="{url}">Run pre cache task</a></b>').format(url=url)
+        return smart_text('<b>Save gallery first</b>')
+
+    pre_cache.allow_tags = True
+
     def images_expected_path(self, gallery):
         """
         Get some gallery images

bviewer/profile/urls.py

 from bviewer.profile.admin import profile
 
 
-urlpatterns = patterns('',
-    url(r'^gallery/(?P<uid>\w+)/$', 'bviewer.profile.views.images_view', name='profile.gallery'),
-    url(r'^download/$', 'bviewer.profile.views.download_image', name='profile.download'),
+urlpatterns = patterns('bviewer.profile.views',
+    url(r'^gallery/(?P<uid>\w+)/$', 'images_view', name='profile.gallery'),
+    url(r'^gallery/(?P<uid>\w+)/pre-cache/$', 'gallery_pre_cache', name='profile.gallery.pre-cache'),
+    url(r'^download/$', 'download_image', name='profile.download'),
     url(r'^', include(profile.urls)),
 )

bviewer/profile/views.py

 # -*- coding: utf-8 -*-
 import logging
-
 from django.contrib.auth.decorators import login_required, permission_required
-from django.http import Http404
+from django.http import Http404, HttpResponseRedirect
 from django.shortcuts import render
 from django.utils.encoding import smart_text
 
 
 @login_required
 @permission_required('core.user_holder')
+def gallery_pre_cache(request, uid):
+    holder = get_gallery_user(request)
+    if not holder:
+        raise Http404()
+
+    controller = GalleryController(holder, request.user, uid)
+    main = controller.get_object()
+    if not main:
+        return message_view(request, message='No such gallery')
+
+    try:
+        controller.pre_cache()
+    except FileError as e:
+        logger.exception(e)
+        return message_view(request, message=smart_text(e))
+    return HttpResponseRedirect(request.META.get('HTTP_REFERER'))
+
+
+@login_required
+@permission_required('core.user_holder')
 def download_image(request):
     if request.GET.get('p', None):
         path = request.GET['p']