changeset 44f3878d9266 in tryton:default
details: https://hg.tryton.org/tryton?cmd=changeset&node=44f3878d9266
description:
        Add icon of type URL

        issue10140
        review355171002
diffstat:

 CHANGELOG                                                 |   1 +
 tryton/common/common.py                                   |  32 ++++++++++++++-
 tryton/config.py                                          |   1 +
 tryton/gui/window/view_form/view/form_gtk/state_widget.py |   9 +++-
 tryton/gui/window/view_form/view/list_gtk/widget.py       |   7 ++-
 5 files changed, 46 insertions(+), 4 deletions(-)

diffs (104 lines):

diff -r 5850f1210e5d -r 44f3878d9266 CHANGELOG
--- a/CHANGELOG Sat Mar 20 12:33:09 2021 +0100
+++ b/CHANGELOG Sat Mar 20 12:38:16 2021 +0100
@@ -1,3 +1,4 @@
+* Support icon of type URL
 * Allow sending email with existing attachments
 * Add paths and model context to action data
 * Add indentation in CSV export of tree
diff -r 5850f1210e5d -r 44f3878d9266 tryton/common/common.py
--- a/tryton/common/common.py   Sat Mar 20 12:33:09 2021 +0100
+++ b/tryton/common/common.py   Sat Mar 20 12:38:16 2021 +0100
@@ -17,7 +17,7 @@
     from http import HTTPStatus
 except ImportError:
     from http import client as HTTPStatus
-from functools import wraps
+from functools import wraps, lru_cache
 from tryton.config import CONFIG
 from tryton.config import TRYTON_ICON, PIXMAPS_DIR
 import sys
@@ -169,6 +169,36 @@
             image.set_from_pixbuf(pixbuf)
         return image
 
+    @classmethod
+    def _convert_url(cls, value, size=16, size_param=None):
+        if not value:
+            return
+        parts = urllib.parse.urlsplit(value)
+        parts = list(parts)
+        if not parts[0]:
+            parts[0] = 'https' if rpc.CONNECTION.ssl else 'http'
+        if not parts[1]:
+            hostname = get_hostname(CONFIG['login.host'])
+            port = get_port(CONFIG['login.host'])
+            parts[1] = '%s:%s' % (hostname, port)
+        if size_param:
+            query = urllib.parse.parse_qsl(parts[4])
+            query.append((size_param, size))
+            parts[4] = urllib.parse.urlencode(query)
+        return urllib.parse.urlunsplit(parts)
+
+    @classmethod
+    @lru_cache(maxsize=CONFIG['image.cache_size'])
+    def get_pixbuf_url(cls, url, size=16, size_param=None):
+        if not url:
+            return
+        url = cls._convert_url(url, size, size_param=size_param)
+        try:
+            with urllib.request.urlopen(url) as response:
+                return data2pixbuf(response.read(), size, size)
+        except urllib.error.URLError:
+            logger.info("Can not fetch %s", url, exc_info=True)
+
 
 IconFactory.load_local_icons()
 
diff -r 5850f1210e5d -r 44f3878d9266 tryton/config.py
--- a/tryton/config.py  Sat Mar 20 12:33:09 2021 +0100
+++ b/tryton/config.py  Sat Mar 20 12:38:16 2021 +0100
@@ -60,6 +60,7 @@
             'calendar.colors': '#fff,#3465a4',
             'graph.color': '#3465a4',
             'image.max_size': 10 ** 6,
+            'image.cache_size': 1024,
             'bug.url': 'https://bugs.tryton.org/',
             'download.url': 'https://downloads-cdn.tryton.org/',
             'download.frequency': 60 * 60 * 8,
diff -r 5850f1210e5d -r 44f3878d9266 
tryton/gui/window/view_form/view/form_gtk/state_widget.py
--- a/tryton/gui/window/view_form/view/form_gtk/state_widget.py Sat Mar 20 
12:33:09 2021 +0100
+++ b/tryton/gui/window/view_form/view/form_gtk/state_widget.py Sat Mar 20 
12:38:16 2021 +0100
@@ -66,8 +66,13 @@
         if name in record.group.fields:
             field = record.group.fields[name]
             name = field.get(record)
-        self.set_from_pixbuf(common.IconFactory.get_pixbuf(
-                name, int(self.attrs.get('size', 48))))
+        size = int(self.attrs.get('size', 48))
+        if self.attrs.get('type') == 'url':
+            pixbuf = common.IconFactory.get_pixbuf_url(
+                name, size=size, size_param=self.attrs.get('url_size'))
+        else:
+            pixbuf = common.IconFactory.get_pixbuf(name, size)
+        self.set_from_pixbuf(pixbuf)
 
 
 class Frame(StateMixin, Gtk.Frame):
diff -r 5850f1210e5d -r 44f3878d9266 
tryton/gui/window/view_form/view/list_gtk/widget.py
--- a/tryton/gui/window/view_form/view/list_gtk/widget.py       Sat Mar 20 
12:33:09 2021 +0100
+++ b/tryton/gui/window/view_form/view/list_gtk/widget.py       Sat Mar 20 
12:38:16 2021 +0100
@@ -185,7 +185,12 @@
                 value = record[self.icon].get_client(record) or ''
             else:
                 value = self.icon
-            pixbuf = common.IconFactory.get_pixbuf(value, Gtk.IconSize.BUTTON)
+            if self.attrs.get('icon_type') == 'url':
+                pixbuf = common.IconFactory.get_pixbuf_url(
+                    value, size_param=self.attrs.get('url_size'))
+            else:
+                pixbuf = common.IconFactory.get_pixbuf(
+                    value, Gtk.IconSize.BUTTON)
             cell.set_property('pixbuf', pixbuf)
         else:
             text = self.attrs.get('string', '')

Reply via email to