From 5861d2fd6ff79920ff2e6d727cf0f953baab720e Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Tue, 26 Oct 2021 16:21:05 +0200 Subject: [PATCH] fixing session in listener --- ereuse_devicehub/resources/device/models.py | 31 +++++++++++++++------ 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 473ca640..6cccc8b9 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -1167,14 +1167,27 @@ class Manufacturer(db.Model): listener_reset_field_updated_in_actual_time(Device) +# def create_code_tag(mapper, connection, device): +# """ +# This function create a new tag every time than one device is create. +# this tag is the same of devicehub_id. +# """ +# from ereuse_devicehub.resources.tag.model import Tag +# tag = Tag(device_id=device.id, id=device.devicehub_id) +# import pdb; pdb.set_trace() +# db.session.add(tag) + + +# event.listen(Device, 'after_insert', create_code_tag, propagate=True) + + +@event.listens_for(Device, 'after_insert') def create_code_tag(mapper, connection, device): - """ - This function create a new tag every time than one device is create. - this tag is the same of devicehub_id. - """ - from ereuse_devicehub.resources.tag.model import Tag - tag = Tag(device_id=device.id, id=device.devicehub_id) - db.session.add(tag) + print(device.devicehub_id) - -event.listen(Device, 'after_insert', create_code_tag, propagate=True) + @event.listens_for(Session, 'after_flush', one=True) + def create_code_tag_after_fush(session, context): + from ereuse_devicehub.resources.tag.model import Tag + tag = Tag(device_id=device.id, id=device.devicehub_id) + # import pdb; pdb.set_trace() + session.add(tag)