Beispiel #1
0
 def load_image_from_url(self, url):
     try:
         data = base64.b64encode(requests.get(
             url.strip()).content)  # .replace(b'\n', b'')
         image.base64_to_image(data)
     except Exception:
         data = False
     return data
Beispiel #2
0
    def image_shape(self, module, filename, img_key, **kwargs):
        svg = self._get_shape_svg(module, 'image_shapes', filename)
        _, _, image_base64 = request.env['ir.http'].binary_content(
            xmlid=img_key,
            model='ir.attachment',
            field='datas',
            default_mimetype='image/png')
        if not image_base64:
            image_base64 = b64encode(request.env['ir.http']._placeholder())
        image = base64_to_image(image_base64)
        width, height = tuple(str(size) for size in image.size)
        root = etree.fromstring(svg)
        root.attrib.update({'width': width, 'height': height})
        # Update default color palette on shape SVG.
        svg, _ = self._update_svg_colors(
            kwargs,
            etree.tostring(root, pretty_print=True).decode('utf-8'))
        # Add image in base64 inside the shape.
        uri = image_data_uri(image_base64)
        svg = svg.replace('<image xlink:href="', '<image xlink:href="%s' % uri)

        return request.make_response(svg, [
            ('Content-type', 'image/svg+xml'),
            ('Cache-control', 'max-age=%s' % http.STATIC_CACHE_LONG),
        ])