From 2a32ba47b891fa5a421c12798571452528db7990 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 18 Oct 2022 13:48:03 +0200 Subject: [PATCH] fix components placeholder in unbinding --- ereuse_devicehub/inventory/views.py | 11 ++++++++--- tests/test_render_2_0.py | 3 +++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/ereuse_devicehub/inventory/views.py b/ereuse_devicehub/inventory/views.py index 0267db00..9682e0c1 100644 --- a/ereuse_devicehub/inventory/views.py +++ b/ereuse_devicehub/inventory/views.py @@ -382,11 +382,13 @@ class UnBindingView(GenericMixin): return flask.render_template(self.template_name, **self.context) def clone_device(self, device): - if device.binding.is_abstract: + if device.binding and device.binding.is_abstract: return - kangaroo = device.binding.kangaroo - device.binding.kangaroo = False + kangaroo = False + if device.binding: + kangaroo = device.binding.kangaroo + device.binding.kangaroo = False dict_device = copy.copy(device.__dict__) dict_device.pop('_sa_instance_state') @@ -406,6 +408,9 @@ class UnBindingView(GenericMixin): for c in device.components: if c.binding: c.binding.device.parent = new_device + else: + new_c = self.clone_device(c) + new_c.parent = new_device placeholder = Placeholder( device=new_device, binding=device, is_abstract=True, kangaroo=kangaroo diff --git a/tests/test_render_2_0.py b/tests/test_render_2_0.py index dbe3c851..a2332842 100644 --- a/tests/test_render_2_0.py +++ b/tests/test_render_2_0.py @@ -2247,9 +2247,11 @@ def test_unbinding(user3: UserClientFlask): user3.get(uri) # action binding + assert Placeholder.query.count() == 11 assert dev.placeholder.binding is None user3.post(uri, data={}) assert dev.placeholder.binding == dev_wb + assert Placeholder.query.count() == 1 dhid = dev.dhid # action unbinding @@ -2273,6 +2275,7 @@ def test_unbinding(user3: UserClientFlask): assert Device.query.filter_by(id=dev_wb.binding.device.id).first() assert Device.query.filter_by(id=dev.id).first() assert Placeholder.query.filter_by(id=dev.placeholder.id).first() + assert Placeholder.query.count() == 11 @pytest.mark.mvp