adding event instead of reuse create_code function

This commit is contained in:
Cayo Puigdefabregas 2021-10-22 19:26:27 +02:00
parent 2ade5a14d4
commit 6091f31c17
1 changed files with 20 additions and 5 deletions

View File

@ -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)