Added "img_class" argument to images rendering functions
authorThierry Florac <thierry.florac@onf.fr>
Mon, 09 Jul 2018 17:14:09 +0200
changeset 120 bd9bce89e937
parent 119 3a9bd68b950c
child 121 31c780a74151
Added "img_class" argument to images rendering functions
src/pyams_file/thumbnail.py
src/pyams_file/zmi/image.py
--- a/src/pyams_file/thumbnail.py	Mon Jul 09 13:40:03 2018 +0200
+++ b/src/pyams_file/thumbnail.py	Mon Jul 09 17:14:09 2018 +0200
@@ -265,7 +265,7 @@
     If image in an SVG image, a "div" is returned containing whole SVG data of given image.
     """
 
-    def render(self, context=None, width=None, height=None, css_class=''):
+    def render(self, context=None, width=None, height=None, css_class='', img_class=''):
         if context is None:
             context = self.context
-        return render_image(context, width, height, self.request, css_class)
+        return render_image(context, width, height, self.request, css_class, img_class, True)
--- a/src/pyams_file/zmi/image.py	Mon Jul 09 13:40:03 2018 +0200
+++ b/src/pyams_file/zmi/image.py	Mon Jul 09 17:14:09 2018 +0200
@@ -60,17 +60,22 @@
 # SVG utilities
 #
 
-def render_svg(image, width=None, height=None, request=None, css_class=''):
+def render_svg(image, width=None, height=None, request=None, css_class='', img_class=''):
     """Render SVG file"""
     options = {'svg': image}
     if width or height:
         options['style'] = 'width: {0}{1}; height: {2}{3};'.format(width, 'px' if isinstance(width, int) else '',
                                                                    height, 'px' if isinstance(height, int) else '')
+    else:
+        options['style'] = ''
     options['css_class'] = css_class
-    return render('templates/svg-render.pt', options, request)
+    result = render('templates/svg-render.pt', options, request)
+    if img_class:
+        result = result.replace('<svg ', '<svg class="{0}" '.format(img_class))
+    return result
 
 
-def render_img(image, width=None, height=None, request=None, css_class='', timestamp=False):
+def render_img(image, width=None, height=None, request=None, css_class='', img_class='', timestamp=False):
     """Render image thumbnail"""
     thumbnail = None
     thumbnails = IThumbnails(image, None)
@@ -89,18 +94,18 @@
         else:
             timestamp = dc.modified.timestamp()
         url += '?_={0}'.format(timestamp)
-    result = '<img src="{0}" />'.format(url)
+    result = '<img src="{0}" class="{1}" />'.format(url, img_class)
     if css_class:
         result = '<div class="{0}">{1}</div>'.format(css_class, result)
     return result
 
 
-def render_image(image, width=None, height=None, request=None, css_class='', timestamp=False):
+def render_image(image, width=None, height=None, request=None, css_class='', img_class='', timestamp=False):
     """Render image"""
     if ISVGImage.providedBy(image):
-        return render_svg(image, width, height, request, css_class)
+        return render_svg(image, width, height, request, css_class, img_class)
     else:
-        return render_img(image, width, height, request, css_class, timestamp)
+        return render_img(image, width, height, request, css_class, img_class, timestamp)
 
 
 #