Updated JSON output after crop to refresh thumbnail
authorThierry Florac <thierry.florac@onf.fr>
Thu, 20 Sep 2018 15:03:18 +0200
changeset 133 9336930326d9
parent 132 9e4d0291d102
child 134 5de35ed04ebe
Updated JSON output after crop to refresh thumbnail
src/pyams_file/zmi/image.py
--- a/src/pyams_file/zmi/image.py	Tue Sep 18 16:18:27 2018 +0200
+++ b/src/pyams_file/zmi/image.py	Thu Sep 20 15:03:18 2018 +0200
@@ -12,48 +12,43 @@
 
 __docformat__ = 'restructuredtext'
 
-
-# import standard library
 import random
 import sys
-import transaction
-
 from collections import OrderedDict
 
-# import interfaces
-from pyams_file.interfaces import IImage, IThumbnails, IResponsiveImage, IFileModifierForm, IThumbnailer, \
-    IFileInfo, IThumbnailForm, IMediaWidget, ISVGImage
-from pyams_form.interfaces.form import IWidgetsPrefixViewletsManager, IFormHelp
-from pyams_i18n.interfaces import II18n
-from pyams_skin.interfaces.viewlet import IContextActions
-from pyams_skin.layer import IPyAMSLayer
-from pyams_utils.interfaces import VIEW_PERMISSION, ICacheKeyValue
-from pyams_zmi.layer import IAdminLayer
-from transaction.interfaces import ITransactionManager
-from zope.dublincore.interfaces import IZopeDublinCore
-
-# import packages
-from pyams_file.image import ThumbnailGeometry
-from pyams_file.zmi import FileModifierAction
-from pyams_form.form import DialogDisplayForm, ajax_config
-from pyams_form.help import FormHelp
-from pyams_form.schema import CloseButton
-from pyams_pagelet.pagelet import pagelet_config
-from pyams_skin.viewlet.toolbar import ToolbarActionItem
-from pyams_template.template import template_config
-from pyams_utils.adapter import adapter_config
-from pyams_utils.request import check_request
-from pyams_utils.url import absolute_url
-from pyams_viewlet.viewlet import viewlet_config, Viewlet
-from pyams_zmi.form import AdminDialogEditForm
+import transaction
 from pyramid.renderers import render
 from pyramid.view import view_config
+from transaction.interfaces import ITransactionManager
 from z3c.form import field, button
+from zope.dublincore.interfaces import IZopeDublinCore
 from zope.interface import implementer, Interface
 from zope.schema import Int, Bool
 from zope.schema.fieldproperty import FieldProperty
 
 from pyams_file import _
+from pyams_file.image import ThumbnailGeometry
+from pyams_file.interfaces import IImage, IThumbnails, IResponsiveImage, IFileModifierForm, IThumbnailer, \
+    IFileInfo, IThumbnailForm, IMediaWidget, ISVGImage
+from pyams_file.zmi import FileModifierAction
+from pyams_form.form import DialogDisplayForm, ajax_config
+from pyams_form.help import FormHelp
+from pyams_form.interfaces.form import IWidgetsPrefixViewletsManager, IFormHelp
+from pyams_form.schema import CloseButton
+from pyams_i18n.interfaces import II18n
+from pyams_pagelet.pagelet import pagelet_config
+from pyams_skin.event import get_json_image_refresh_event
+from pyams_skin.interfaces.viewlet import IContextActions
+from pyams_skin.layer import IPyAMSLayer
+from pyams_skin.viewlet.toolbar import ToolbarActionItem
+from pyams_template.template import template_config
+from pyams_utils.adapter import adapter_config
+from pyams_utils.interfaces import VIEW_PERMISSION, ICacheKeyValue
+from pyams_utils.request import check_request
+from pyams_utils.url import absolute_url
+from pyams_viewlet.viewlet import viewlet_config, Viewlet
+from pyams_zmi.form import AdminDialogEditForm
+from pyams_zmi.layer import IAdminLayer
 
 
 #
@@ -219,9 +214,14 @@
         self.context.crop(x1, y1, x2, y2)
 
     def get_ajax_output(self, changes):
+        ITransactionManager(self.context).commit()
+        thumbnail = IThumbnails(self.context).get_thumbnail('128x128')
         return {
-            'status': 'reload',
-            'message': self.request.localizer.translate(self.successMessage)
+            'status': 'success',
+            'message': self.request.localizer.translate(self.successMessage),
+            'events': [
+                get_json_image_refresh_event(thumbnail, self.request, self.context)
+            ]
         }
 
 
@@ -640,7 +640,7 @@
         registry = self.request.registry
         translate = self.request.localizer.translate
         thumbnailers = OrderedDict()
-        adapters = sorted(registry.getAdapters((self.context, ), IThumbnailer),
+        adapters = sorted(registry.getAdapters((self.context,), IThumbnailer),
                           key=lambda x: x[1].weight)
         for name, adapter in adapters:
             thumbnails = IThumbnails(self.context)