Added support to access file preview from input or display widget
authorThierry Florac <thierry.florac@onf.fr>
Wed, 20 Sep 2017 14:04:03 +0200
changeset 59 6e6aa46a5d87
parent 58 881d1aa66fbc
child 60 999277853005
Added support to access file preview from input or display widget
src/pyams_file/widget/__init__.py
src/pyams_file/widget/templates/file-display.pt
src/pyams_file/widget/templates/file-input.pt
--- a/src/pyams_file/widget/__init__.py	Tue Sep 19 11:31:15 2017 +0200
+++ b/src/pyams_file/widget/__init__.py	Wed Sep 20 14:04:03 2017 +0200
@@ -9,6 +9,7 @@
 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
 # FOR A PARTICULAR PURPOSE.
 #
+from pyramid.interfaces import IView
 
 __docformat__ = 'restructuredtext'
 
@@ -103,6 +104,14 @@
         name, ext = os.path.splitext(self.current_value.filename)
         return '/--static--/pyams_skin/img/mimetypes/{0}'.format(EXTENSIONS_THUMBNAILS.get(ext, 'unknown.png'))
 
+    def get_thumbnail_target(self):
+        value = self.current_value
+        if value is not None:
+            registry = self.request.registry
+            view = registry.queryMultiAdapter((value, self.request), IView, name='preview.html')
+            if view is not None:
+                return absolute_url(value, self.request, 'preview.html')
+
 
 @adapter_config(context=(IFileField, IFormLayer), provides=IFieldWidget)
 def FileFieldWidget(field, request):
--- a/src/pyams_file/widget/templates/file-display.pt	Tue Sep 19 11:31:15 2017 +0200
+++ b/src/pyams_file/widget/templates/file-display.pt	Wed Sep 20 14:04:03 2017 +0200
@@ -3,8 +3,12 @@
 		 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" />
+			<a tal:define="target view/get_thumbnail_target"
+			   tal:omit-tag="not:target"
+			   tal:attributes="href target" data-toggle="modal">
+				<img class="thumbnail no-border"
+					 tal:attributes="src view/get_thumbnail" />
+			</a>
 		</div>
 		<div class="margin-top-5">
 			<span i18n:translate="">Current value: </span>
--- a/src/pyams_file/widget/templates/file-input.pt	Tue Sep 19 11:31:15 2017 +0200
+++ b/src/pyams_file/widget/templates/file-input.pt	Wed Sep 20 14:04:03 2017 +0200
@@ -20,8 +20,12 @@
 		 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" />
+			<a tal:define="target view/get_thumbnail_target"
+			   tal:omit-tag="not:target"
+			   tal:attributes="href target" data-toggle="modal">
+				<img class="thumbnail no-border"
+					 tal:attributes="src view/get_thumbnail" />
+			</a>
 		</div>
 		<div class="margin-top-5">
 			<span i18n:translate="">Current value: </span>