From bc613e9fd877482e4293bbce7eebf37c79212ffd Mon Sep 17 00:00:00 2001 From: Julien Duponchelle Date: Wed, 22 Jun 2016 17:46:00 +0200 Subject: [PATCH] Send SVG in shape update only when change --- gns3server/controller/shape.py | 9 ++++++++- gns3server/schemas/shape.py | 2 +- tests/controller/test_shape.py | 18 ++++++++++++++++-- 3 files changed, 25 insertions(+), 4 deletions(-) diff --git a/gns3server/controller/shape.py b/gns3server/controller/shape.py index 71de96cf..e3e9f64b 100644 --- a/gns3server/controller/shape.py +++ b/gns3server/controller/shape.py @@ -89,10 +89,17 @@ class Shape: """ # Update node properties with additional elements + svg_changed = False for prop in kwargs: if getattr(self, prop) != kwargs[prop]: + if prop == "svg": + # To avoid spamming client with large data we don't send the svg if the SVG didn't change + svg_changed = True setattr(self, prop, kwargs[prop]) - self._project.controller.notification.emit("shape.updated", self.__json__()) + data = self.__json__() + if not svg_changed: + del data["svg"] + self._project.controller.notification.emit("shape.updated", data) self._project.dump() def __json__(self, topology_dump=False): diff --git a/gns3server/schemas/shape.py b/gns3server/schemas/shape.py index cac29d16..c4af2688 100644 --- a/gns3server/schemas/shape.py +++ b/gns3server/schemas/shape.py @@ -56,7 +56,7 @@ SHAPE_OBJECT_SCHEMA = { "svg": { "description": "SVG content of the shape", "type": "string", - "pattern": "^<.+>$" + "pattern": "^<(.|[\r\n])+>$" } }, "additionalProperties": False diff --git a/tests/controller/test_shape.py b/tests/controller/test_shape.py index aa9b069a..93b3c2a1 100644 --- a/tests/controller/test_shape.py +++ b/tests/controller/test_shape.py @@ -72,7 +72,21 @@ def test_update(shape, project, async_run, controller): controller._notification = AsyncioMagicMock() project.dump = MagicMock() - async_run(shape.update(x=42)) + async_run(shape.update(x=42, svg="")) assert shape.x == 42 - controller._notification.emit.assert_called_with("shape.updated", shape.__json__()) + args, kwargs = controller._notification.emit.call_args + assert args[0] == "shape.updated" + # JSON + assert args[1]["x"] == 42 + assert args[1]["svg"] == "" + + async_run(shape.update(x=12, svg="")) + assert shape.x == 12 + args, kwargs = controller._notification.emit.call_args + assert args[0] == "shape.updated" + # JSON + assert args[1]["x"] == 12 + # To avoid spamming client with large data we don't send the svg if the SVG didn't change + assert "svg" not in args[1] + assert project.dump.called