Updated files and images widgets
authorThierry Florac <thierry.florac@onf.fr>
Mon, 11 Sep 2017 13:28:59 +0200
changeset 50 3b2cb5046d86
parent 49 79afa48cc2aa
child 51 83ef19bfaa63
Updated files and images widgets
src/pyams_file/widget/__init__.py
src/pyams_file/widget/templates/file-display.pt
src/pyams_file/widget/templates/file-input.pt
src/pyams_file/widget/templates/image-display.pt
src/pyams_file/widget/templates/image-input.pt
--- a/src/pyams_file/widget/__init__.py	Mon Sep 11 13:28:17 2017 +0200
+++ b/src/pyams_file/widget/__init__.py	Mon Sep 11 13:28:59 2017 +0200
@@ -14,17 +14,21 @@
 
 
 # import standard library
+import os
 from cgi import FieldStorage
 
 # import interfaces
 from pyams_file.interfaces import IFileField, IFileWidget, IImageField, IImageWidget, \
-    IThumbnailImageWidget, IThumbnailImageField, DELETED_FILE
+    IThumbnailImageWidget, IThumbnailImageField, DELETED_FILE, IThumbnail
 from pyams_form.interfaces.form import IFormLayer
 from z3c.form.interfaces import NOT_CHANGED, IFieldWidget, IDataConverter
 
 # import packages
+from pyams_file.file import EXTENSIONS_THUMBNAILS
 from pyams_form.widget import widgettemplate_config
 from pyams_utils.adapter import adapter_config
+from pyams_utils.size import get_human_size
+from pyams_utils.url import absolute_url
 from z3c.form.browser.file import FileWidget as BaseFileWidget
 from z3c.form.converter import BaseDataConverter
 from z3c.form.util import toBytes
@@ -54,6 +58,10 @@
             return toBytes(value)
 
 
+#
+# File widgets
+#
+
 @widgettemplate_config(mode='input', template='templates/file-input.pt', widget=IFileWidget, layer=IFormLayer)
 @widgettemplate_config(mode='display', template='templates/file-display.pt', widget=IFileWidget, layer=IFormLayer)
 @implementer_only(IFileWidget)
@@ -83,12 +91,28 @@
             value = self.value
         return bool(value)
 
+    def get_human_size(self, value):
+        return get_human_size(value, self.request)
+
+    def get_thumbnail(self):
+        thumbnails = IThumbnail(self.current_value, None)
+        if thumbnails is not None:
+            display = thumbnails.get_thumbnail('128x128', 'jpeg')
+            if display is not None:
+                return absolute_url(display, self.request)
+        name, ext = os.path.splitext(self.current_value.filename)
+        return '/--static--/pyams_skin/img/mimetypes/{0}'.format(EXTENSIONS_THUMBNAILS.get(ext, 'unknown.png'))
+
 
 @adapter_config(context=(IFileField, IFormLayer), provides=IFieldWidget)
 def FileFieldWidget(field, request):
     return FieldWidget(field, FileWidget(request))
 
 
+#
+# Image widgets
+#
+
 @widgettemplate_config(mode='input', template='templates/image-input.pt', widget=IImageWidget, layer=IFormLayer)
 @widgettemplate_config(mode='display', template='templates/image-display.pt', widget=IImageWidget, layer=IFormLayer)
 @implementer_only(IImageWidget)
--- a/src/pyams_file/widget/templates/file-display.pt	Mon Sep 11 13:28:17 2017 +0200
+++ b/src/pyams_file/widget/templates/file-display.pt	Mon Sep 11 13:28:59 2017 +0200
@@ -2,11 +2,15 @@
 	<div class="current-value"
 		 tal:define="value view/current_value"
 		 tal:condition="value">
+		<div class="pull-left margin-5 margin-right-10">
+			<img class="thumbnail no-border"
+				 tal:attributes="src string:/--static--/pyams_skin/img/mimetypes/${view/get_thumbnail}" />
+		</div>
 		<div class="margin-top-5">
 			<span i18n:translate="">Current value: </span>
 			<span tal:content="value/content_type"></span>
 			&nbsp;&ndash;&nbsp;
-			<i18n:var i18n:translate=""><span i18n:name="size" tal:content="python:round(value.get_size() / 1024)"></span> Kb</i18n:var>
+			<span tal:content="python:view.get_human_size(value.get_size())"></span>
 			<a class="btn btn-xs btn-default" target="download_window"
 			   tal:define="href extension:absolute_url(value)"
 			   tal:attributes="href string:${href}?download=1"
--- a/src/pyams_file/widget/templates/file-input.pt	Mon Sep 11 13:28:17 2017 +0200
+++ b/src/pyams_file/widget/templates/file-input.pt	Mon Sep 11 13:28:59 2017 +0200
@@ -19,11 +19,15 @@
 	<div class="current-value"
 		 tal:define="value view/current_value"
 		 tal:condition="value">
+		<div class="pull-left margin-5 margin-right-10">
+			<img class="thumbnail no-border"
+				 tal:attributes="src view/get_thumbnail" />
+		</div>
 		<div class="margin-top-5">
 			<span i18n:translate="">Current value: </span>
 			<span tal:content="value/content_type"></span>
 			&nbsp;&ndash;&nbsp;
-			<i18n:var i18n:translate=""><span i18n:name="size" tal:content="python:round(value.get_size() / 1024)"></span> Kb</i18n:var>
+			<span tal:content="python:view.get_human_size(value.get_size())"></span>
 			<br />
 			<div class="btn-group dropup"
 				 tal:define="actions extension:context_actions(value)"
--- a/src/pyams_file/widget/templates/image-display.pt	Mon Sep 11 13:28:17 2017 +0200
+++ b/src/pyams_file/widget/templates/image-display.pt	Mon Sep 11 13:28:59 2017 +0200
@@ -5,10 +5,11 @@
 		<a class="fancybox hint pull-left margin-left-5 margin-right-10" data-toggle
 		   data-ams-fancybox-type="image"
 		   data-ams-hint-gravity="e"
+		   data-ams-hint-offset="5"
 		   tal:define="thumbnails extension:thumbnails(value);
 					   target python:thumbnails.get_thumbnail('800x600', 'jpeg');"
-		   tal:attributes="href extension:absolute_url(target);
-						   title python:value.title or value.filename;">
+		   tal:attributes="href extension:absolute_url(target);"
+		   title="Zoom image" i18n:attributes="title">
 			<img class="thumbnail"
 				 tal:define="thumbnail python:thumbnails.get_thumbnail('128x128', 'jpeg');"
 				 tal:attributes="src extension:absolute_url(thumbnail)" src="" alt="" />
@@ -19,7 +20,7 @@
 			&nbsp;&ndash;&nbsp;
 			<span tal:content="python:'%d x %d' % value.get_image_size()"></span>
 			&nbsp;&ndash;&nbsp;
-			<i18n:var i18n:translate=""><span i18n:name="size" tal:content="python:round(value.get_size() / 1024)"></span> Kb</i18n:var>
+			<span tal:content="python:view.get_human_size(value.get_size())"></span>
 			<br />
 			<a class="btn btn-xs btn-default" target="download_window"
 			   tal:define="href extension:absolute_url(value)"
--- a/src/pyams_file/widget/templates/image-input.pt	Mon Sep 11 13:28:17 2017 +0200
+++ b/src/pyams_file/widget/templates/image-input.pt	Mon Sep 11 13:28:59 2017 +0200
@@ -19,13 +19,14 @@
 	<div class="current-value"
 		 tal:define="value view/current_value"
 		 tal:condition="value">
-		<a class="fancybox hint pull-left margin-left-5 margin-right-10" data-toggle
+		<a class="fancybox hint pull-left margin-5 margin-right-10" data-toggle
 		   data-ams-fancybox-type="image"
 		   data-ams-hint-gravity="e"
+		   data-ams-hint-offset="5"
 		   tal:define="thumbnails extension:thumbnails(value);
 					   target python:thumbnails.get_thumbnail('800x600', 'jpeg');"
-		   tal:attributes="href extension:absolute_url(target);
-						   title python:value.title or value.filename;">
+		   tal:attributes="href extension:absolute_url(target);"
+		   title="Zoom image" i18n:attributes="title">
 			<img class="thumbnail"
 				 tal:define="thumbnail python:thumbnails.get_thumbnail('128x128', 'jpeg');"
 				 tal:attributes="src extension:absolute_url(thumbnail)" src="" alt="" />
@@ -36,7 +37,7 @@
 			&nbsp;&ndash;&nbsp;
 			<span tal:content="python:'%d x %d' % value.get_image_size()"></span>
 			&nbsp;&ndash;&nbsp;
-			<i18n:var i18n:translate=""><span i18n:name="size" tal:content="python:round(value.get_size() / 1024)"></span> Kb</i18n:var>
+			<span tal:content="python:view.get_human_size(value.get_size())"></span>
 			<br />
 			<div class="btn-group dropup"
 				 tal:define="actions extension:context_actions(value)"