From 6091f31c17cec694ced7921fc3081061d576af3a Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 22 Oct 2021 19:26:27 +0200 Subject: [PATCH] adding event instead of reuse create_code function --- ereuse_devicehub/resources/device/models.py | 25 ++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/ereuse_devicehub/resources/device/models.py b/ereuse_devicehub/resources/device/models.py index 38552e9a..ebf1d858 100644 --- a/ereuse_devicehub/resources/device/models.py +++ b/ereuse_devicehub/resources/device/models.py @@ -36,14 +36,15 @@ from ereuse_devicehub.resources.device.metrics import Metrics def create_code(context): - _id = Device.query.order_by(Device.id.desc()).first() or 1 - if not _id == 1: + # import pdb; pdb.set_trace() + _id = Device.query.order_by(Device.id.desc()).first() or 3 + if not _id == 3: _id = _id.id + 1 code = hashcode.encode(_id) - from ereuse_devicehub.resources.tag.model import Tag - tag = Tag(device_id=_id, id=code) - db.session.add(tag) + # from ereuse_devicehub.resources.tag.model import Tag + # tag = Tag(device_id=_id, id=code) + # db.session.add(tag) return code @@ -1169,3 +1170,17 @@ 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) + db.session.add(tag) + + +from flask_sqlalchemy import event +event.listen(Device, 'after_update', create_code_tag, propagate=True)