From ae1ecb59e2642ec18ec905682d0104c000663f20 Mon Sep 17 00:00:00 2001 From: Cayo Puigdefabregas Date: Fri, 16 Oct 2020 16:25:13 +0200 Subject: [PATCH] add listener a event --- ereuse_devicehub/resources/models.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/ereuse_devicehub/resources/models.py b/ereuse_devicehub/resources/models.py index 721e9258..74e1e6b4 100644 --- a/ereuse_devicehub/resources/models.py +++ b/ereuse_devicehub/resources/models.py @@ -1,4 +1,5 @@ from datetime import datetime, timezone +from flask_sqlalchemy import event from ereuse_devicehub.db import db @@ -34,3 +35,9 @@ class Thing(db.Model): # to be able to use sorted containers self.created = kwargs.get('created', datetime.now(timezone.utc)) super().__init__(**kwargs) + +def on_update_time(mapper, connection, thing_obj): + thing_obj.updated = datetime.now() + +def update_timestamp(thing_obj): + event.listen(thing_obj, 'before_update', on_update_time)